HI, im very new to box2d.
here is my first box2d app,
http://sasky.org/SandBox/spelling/spelling.html
My big problem that is how can I
pick up a box with the mouse and not have the box rotate at all,
So when the box is under mouse control it may not rotate. Is this Possible?
heres my code thats relevant, the class to make the box, it extends an Actor class, the same as the box 2d vid tuits from http://www.kerp.net/box2d/index.php
Any help would be very appreciated
here is my first box2d app,
http://sasky.org/SandBox/spelling/spelling.html
My big problem that is how can I
pick up a box with the mouse and not have the box rotate at all,
So when the box is under mouse control it may not rotate. Is this Possible?
heres my code thats relevant, the class to make the box, it extends an Actor class, the same as the box 2d vid tuits from http://www.kerp.net/box2d/index.php
Code:
package
{
import Box2D.Collision.Shapes.b2PolygonDef;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2World;
import Box2D.Dynamics.Joints.b2MouseJoint;
import Box2D.Dynamics.Joints.b2MouseJointDef;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
public class SpellingBoxActor extends Actor
{
public static const WIDTH:Number = 199;
public static const HEIGHT:Number = 86;
private var mouseDown:Boolean;
private var _inInterActiveArea:Boolean;
private var _parent:DisplayObjectContainer;
private var _hitArea1:Rectangle;
private var _hitArea2:Rectangle;
private var _mouseJoint:b2MouseJoint;
public function SpellingBoxActor(parent:DisplayObjectContainer, location:Point ,costume:Sprite = null):void
{
_inInterActiveArea = true;
_parent = parent;
mouseDown = false;
makehitAreaRectsngle();
// 1. Create Custome
var costume:Sprite = (costume == null) ? makeCustome() : applyCustome();
parent.addChild(costume);
// 2. Create the shape def
var box_SD:b2PolygonDef = new b2PolygonDef();
box_SD.SetAsBox((WIDTH / 2) / PhysiVals.RATIO, (HEIGHT / 2) / PhysiVals.RATIO);
box_SD.density = 1;
box_SD.friction = 0.3;
box_SD.restitution = 0.2;
// 2. Create the body def
var box_BD:b2BodyDef = new b2BodyDef();
box_BD.position.Set((location.x + WIDTH / 2) / PhysiVals.RATIO, (location.y + HEIGHT / 2) / PhysiVals.RATIO);
box_BD.isBullet = true;
// 3. Create the body
var box_D:b2Body = PhysiVals.world.CreateBody(box_BD);
// 3. Create the shape
box_D.CreateShape(box_SD);
box_D.SetMassFromShapes();
super(box_D, costume);
costume.addEventListener(MouseEvent.MOUSE_DOWN, onDownClick);
parent.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
private function makehitAreaRectsngle():void
{
_hitArea1 = new Rectangle(521, 0, 279, 262);
_hitArea2 = new Rectangle(800, 0, 226, 720);
}
private function onDownClick(e:MouseEvent):void
{
mouseDown = true;
if (_inInterActiveArea)
{
var mouseJointDef:b2MouseJointDef=new b2MouseJointDef();
mouseJointDef.body1=PhysiVals.world.GetGroundBody();
mouseJointDef.body2=_body;
mouseJointDef.target.Set(_parent.mouseX/PhysiVals.RATIO, _parent.mouseY/PhysiVals.RATIO);
mouseJointDef.maxForce=3000;
mouseJointDef.timeStep = 1 / PhysiVals.FRAME_RATE;
_mouseJoint = PhysiVals.world.CreateJoint(mouseJointDef) as b2MouseJoint;
}
}
private function onMouseUp(e:MouseEvent):void
{
mouseDown = false
}
private function makeCustome():Sprite
{
var box:Sprite = new Sprite();
box.graphics.lineStyle(1, 0x000000);
box.graphics.beginFill(0xC5C5C5, 1);
box.graphics.drawRect( -WIDTH / 2 , -HEIGHT / 2, WIDTH , HEIGHT );
box.graphics.endFill();
return box;
}
private function applyCustome():Sprite
{
return null;
}
override protected function childSpecificUpdating():void
{
var mouse:Point = new Point(_parent.mouseX , _parent.mouseY);
if ( mouse.x > _hitArea1.left && mouse.y < _hitArea1.right &&
mouse.y > _hitArea1.top && mouse.y < _hitArea1.bottom
||
mouse.x > _hitArea2.left && mouse.y < _hitArea2.right &&
mouse.y > _hitArea2.top && mouse.y < _hitArea2.bottom)
{
_inInterActiveArea = true;
_costume.buttonMode = true;
} else {
_inInterActiveArea = false;
_costume.buttonMode = false;
}
if (mouseDown && _inInterActiveArea)
{
var mouseXWorldPhys=_parent.mouseX/PhysiVals.RATIO;
var mouseYWorldPhys=_parent.mouseY/PhysiVals.RATIO;
var p2:b2Vec2=new b2Vec2(mouseXWorldPhys,mouseYWorldPhys);
_mouseJoint.SetTarget(p2);
}
if (!mouseDown)
{
if (_mouseJoint)
{
PhysiVals.world.DestroyJoint(_mouseJoint);
_mouseJoint=null;
}
}
//var v:Number = _body.GetAngularVelocity();
// ????
_body.SetAngularVelocity (0);
}
}
}
{
import Box2D.Collision.Shapes.b2PolygonDef;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2World;
import Box2D.Dynamics.Joints.b2MouseJoint;
import Box2D.Dynamics.Joints.b2MouseJointDef;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
public class SpellingBoxActor extends Actor
{
public static const WIDTH:Number = 199;
public static const HEIGHT:Number = 86;
private var mouseDown:Boolean;
private var _inInterActiveArea:Boolean;
private var _parent:DisplayObjectContainer;
private var _hitArea1:Rectangle;
private var _hitArea2:Rectangle;
private var _mouseJoint:b2MouseJoint;
public function SpellingBoxActor(parent:DisplayObjectContainer, location:Point ,costume:Sprite = null):void
{
_inInterActiveArea = true;
_parent = parent;
mouseDown = false;
makehitAreaRectsngle();
// 1. Create Custome
var costume:Sprite = (costume == null) ? makeCustome() : applyCustome();
parent.addChild(costume);
// 2. Create the shape def
var box_SD:b2PolygonDef = new b2PolygonDef();
box_SD.SetAsBox((WIDTH / 2) / PhysiVals.RATIO, (HEIGHT / 2) / PhysiVals.RATIO);
box_SD.density = 1;
box_SD.friction = 0.3;
box_SD.restitution = 0.2;
// 2. Create the body def
var box_BD:b2BodyDef = new b2BodyDef();
box_BD.position.Set((location.x + WIDTH / 2) / PhysiVals.RATIO, (location.y + HEIGHT / 2) / PhysiVals.RATIO);
box_BD.isBullet = true;
// 3. Create the body
var box_D:b2Body = PhysiVals.world.CreateBody(box_BD);
// 3. Create the shape
box_D.CreateShape(box_SD);
box_D.SetMassFromShapes();
super(box_D, costume);
costume.addEventListener(MouseEvent.MOUSE_DOWN, onDownClick);
parent.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
private function makehitAreaRectsngle():void
{
_hitArea1 = new Rectangle(521, 0, 279, 262);
_hitArea2 = new Rectangle(800, 0, 226, 720);
}
private function onDownClick(e:MouseEvent):void
{
mouseDown = true;
if (_inInterActiveArea)
{
var mouseJointDef:b2MouseJointDef=new b2MouseJointDef();
mouseJointDef.body1=PhysiVals.world.GetGroundBody();
mouseJointDef.body2=_body;
mouseJointDef.target.Set(_parent.mouseX/PhysiVals.RATIO, _parent.mouseY/PhysiVals.RATIO);
mouseJointDef.maxForce=3000;
mouseJointDef.timeStep = 1 / PhysiVals.FRAME_RATE;
_mouseJoint = PhysiVals.world.CreateJoint(mouseJointDef) as b2MouseJoint;
}
}
private function onMouseUp(e:MouseEvent):void
{
mouseDown = false
}
private function makeCustome():Sprite
{
var box:Sprite = new Sprite();
box.graphics.lineStyle(1, 0x000000);
box.graphics.beginFill(0xC5C5C5, 1);
box.graphics.drawRect( -WIDTH / 2 , -HEIGHT / 2, WIDTH , HEIGHT );
box.graphics.endFill();
return box;
}
private function applyCustome():Sprite
{
return null;
}
override protected function childSpecificUpdating():void
{
var mouse:Point = new Point(_parent.mouseX , _parent.mouseY);
if ( mouse.x > _hitArea1.left && mouse.y < _hitArea1.right &&
mouse.y > _hitArea1.top && mouse.y < _hitArea1.bottom
||
mouse.x > _hitArea2.left && mouse.y < _hitArea2.right &&
mouse.y > _hitArea2.top && mouse.y < _hitArea2.bottom)
{
_inInterActiveArea = true;
_costume.buttonMode = true;
} else {
_inInterActiveArea = false;
_costume.buttonMode = false;
}
if (mouseDown && _inInterActiveArea)
{
var mouseXWorldPhys=_parent.mouseX/PhysiVals.RATIO;
var mouseYWorldPhys=_parent.mouseY/PhysiVals.RATIO;
var p2:b2Vec2=new b2Vec2(mouseXWorldPhys,mouseYWorldPhys);
_mouseJoint.SetTarget(p2);
}
if (!mouseDown)
{
if (_mouseJoint)
{
PhysiVals.world.DestroyJoint(_mouseJoint);
_mouseJoint=null;
}
}
//var v:Number = _body.GetAngularVelocity();
// ????
_body.SetAngularVelocity (0);
}
}
}
Any help would be very appreciated