I think i learned how to create Mouse Joints.
This is the code exemple:
Code:
var mouseJointDef:b2MouseJointDef=new b2MouseJointDef();
mouseJointDef.body1 = Vals.world.GetGroundBody();
mouseJointDef.body2 = metalBody;
var bodypos:b2Vec2 = metalBody.GetWorldCenter();
mouseJointDef.target.Set(bodypos.x, bodypos.y);
mouseJointDef.collideConnected=true;
mouseJointDef.maxForce = 300 * metalBody.GetMass();
var mouseJoint:b2MouseJoint;
mouseJoint=Vals.world.CreateJoint(mouseJointDef) as b2MouseJoint;
// And in the update i add this:
var currentmousepos:b2Vec2 = new b2Vec2(mouseX / Vals.RATIO, mouseY / Vals.RATIO);
mouseJoint.SetTarget(currentmousepos);
But im not sure where to add it. To learn box2d i have been following some tutorials from this person here:
http://www.youtube.com/watch?v=MGX8GOmz ... plpp_videoSo its like this, I spawn objects in the world from this objectActor class which is liek this:
Code:
public class MetalActor extends Actor
{
public function MetalActor(parent:DisplayObjectContainer, location:Point, initVel:Point)
{
var metalSprite=new Metal();
parent.addChild(metalSprite);
var metalShapeDef:b2PolygonDef=new b2PolygonDef();
metalShapeDef.vertexCount=3;
b2Vec2(metalShapeDef.vertices[0]).Set(0/Vals.RATIO, 0/Vals.RATIO);
b2Vec2(metalShapeDef.vertices[1]).Set(0/Vals.RATIO, -100/Vals.RATIO);
b2Vec2(metalShapeDef.vertices[2]).Set(100/Vals.RATIO, 0/Vals.RATIO);
metalShapeDef.density=1.7;
metalShapeDef.friction=0.9;
metalShapeDef.restitution=0.1;
var metalBodyDef:b2BodyDef=new b2BodyDef();
metalBodyDef.position.Set(location.x/Vals.RATIO, location.y/Vals.RATIO);
metalBodyDef.massData.mass=30;
var metalBody:b2Body;
metalBody=Vals.world.CreateBody(metalBodyDef);
metalBody.CreateShape(metalShapeDef);
metalBody.SetMassFromShapes();
var velocityVector:b2Vec2=new b2Vec2(initVel.x/Vals.RATIO, initVel.y/Vals.RATIO);
metalBody.SetLinearVelocity(velocityVector);
super(metalBody, metalSprite);
}
}
And the the Actor class is this:
Code:
public class Actor extends EventDispatcher
{
protected var _body:b2Body;
protected var _costume:DisplayObject;
public function Actor(myBody:b2Body, myCostume:DisplayObject)
{
_body=myBody;
_costume=myCostume;
updateMyLook();
}
public function updateNow():void
{
updateMyLook();
childSpecificUpdating();
}
protected function childSpecificUpdating():void
{
if(_costume.y>_costume.stage.stageHeight)
{
dispatchEvent(new ObjectEvent(ObjectEvent.OBJECT_OFF_SCREEN));
}
}
public function destroy():void
{
cleanUpBeforeRemoving();
_costume.parent.removeChild(_costume);
Vals.world.DestroyBody(_body);
}
protected function cleanUpBeforeRemoving():void
{
}
private function updateMyLook():void
{
_costume.x=_body.GetPosition().x * 30;
_costume.y=_body.GetPosition().y * 30;
_costume.rotation=_body.GetAngle() * 180/Math.PI;
}
}
And then on my main class the objects are spawned with the following code:
Code:
var metalActor=new MetalActor(this, new Point(300, 80), new Point(-30, -3));
which is then inserted into an array and the array is updated in the ENTER_FRAME function.
SO im not sure where i could get that mouse joint code so it would work.
Could you help me?