Strange rope behaviour

Discuss issues specific to the Flash/AS3 port of Box2D
Posts: 5
Joined: Wed Feb 27, 2013 11:22 am

Strange rope behaviour

Postby BBman » Fri Mar 22, 2013 2:21 am

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;
            piece.body.SetPosition(new b2Vec2(anchorObject.body.GetPosition().x,tmpY));         
            //create the joint
            revolute_joint= new b2RevoluteJointDef();
            revolute_joint.enableLimit = true;
            revolute_joint.collideConnected = true;
            //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.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;
            //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));
         //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;;


btw, as the image shows, how can I make sure my graphics are perfectly matched to the box2d bodies?

1.jpg (20.85 KiB) Viewed 1773 times

Return to “Flash”

Who is online

Users browsing this forum: No registered users and 2 guests