hello,
im trying to
create a system where an object is attached to a rope.
i managed to do that, but for some reason the rope acts strange, as it was some pieces attached to each other and not a one piece rope.
Ive been working on that for a long time, unsecessfuly. any suggestion would be great.
the code:
btw, as the image shows, how can I make sure my graphics are perfectly matched to the box2d bodies?
thanks
im trying to
create a system where an object is attached to a rope.
i managed to do that, but for some reason the rope acts strange, as it was some pieces attached to each other and not a one piece rope.
Ive been working on that for a long time, unsecessfuly. any suggestion would be great.
the code:
Code: Select all
private function createRope(attachedObject:GameObject,anchorObject:GameObject):void
{
var numPieces:Number;
//body definition of piece
var pieceBodyDef:b2BodyDef;
//piece body
var piece:RopePiece;
//definition of box fixture
var fixDef:b2FixtureDef;
//definition of box shape
var boxShape:b2PolygonShape;
//revolut joint between pieces
var revolute_joint:b2RevoluteJointDef;
//first, fix positions of bodies
var fixAnchorY:Number = fixBodyPosition("y",anchorObject,Globals.ROPE_PIECE_HEIGHT);
numPieces = calculateNumPiecesForRope(attachedObject,anchorObject);
var fixAttachedY:Number;
anchorObject.body.SetPosition(new b2Vec2(anchorObject.body.GetPosition().x,fixAnchorY));
//temporary move the attached object
//attachedObject.body.SetPosition(new b2Vec2(attachedObject.body.GetPosition().x,0));
//the current object to joint to
var link:b2Body = anchorObject.body;
//rope piece mass data
var ropeMass:b2MassData = new b2MassData();
ropeMass.mass = Globals.ROPE_PIECE_MASS;
ropeMass.I = Globals.ROPE_PIECE_MASS;
//var to know if link is a breaking point
var breakPoint:Number = 0;
//creater pieces according to rope length value
for(var i:Number = 1; i<=numPieces;i++)
{
//set body position
piece = new RopePiece(i%2,"piece");
//add piece to the world
var tmpY:Number = link.GetPosition().y + getShapeDim(link).y;
if(i==1)
tmpY+=0.2;
piece.body.SetPosition(new b2Vec2(anchorObject.body.GetPosition().x,tmpY));
add(piece);
//create the joint
revolute_joint= new b2RevoluteJointDef();
revolute_joint.enableLimit = true;
revolute_joint.collideConnected = true;
//trace(getShapeDim(link).y);
//revolute_joint.Initialize(link,piece.body,new b2Vec2(link.GetPosition().x,(link.GetWorldCenter().y+Globals.ROPE_PIECE_HEIGHT)));
revolute_joint.bodyA = link;
revolute_joint.bodyB = piece.body;
revolute_joint.localAnchorA.Set(0,Globals.ROPE_PIECE_HEIGHT);
revolute_joint.localAnchorB.Set(0,-Globals.ROPE_PIECE_HEIGHT);
//revolute_joint.maxMotorTorque = 0.1;
revolute_joint.upperAngle = 30;
//if is a middle link
if(i == Math.floor(numPieces / 2)){
breakPoint = 1;
}
revolute_joint.userData = attachedObject.jointId+" "+breakPoint;
physics.world.CreateJoint(revolute_joint);
//assign the current body to link, so it will be attached to the next piece
link = piece.body;
breakPoint = 0;
}
fixAttachedY = link.GetPosition().y + getShapeDim(link).y+Globals.ROPE_PIECE_HEIGHT/2;
attachedObject.body.SetPosition(new b2Vec2(anchorObject.body.GetPosition().x, fixAttachedY));
attachedObject.body.SetType(Globals.DYNAMIC_BODY);
//attach the last piece to the object
revolute_joint.enableLimit = false;
revolute_joint.Initialize(link,attachedObject.body,new b2Vec2(attachedObject.body.GetWorldCenter().x,attachedObject.body.GetWorldCenter().y - getShapeDim(attachedObject.body).y/2));
revolute_joint.userData = attachedObject.jointId;
physics.world.CreateJoint(revolute_joint);
}btw, as the image shows, how can I make sure my graphics are perfectly matched to the box2d bodies?
thanks