Search code examples
cocos2d-iphonechipmunk

Creating CCSprites and using them as a rope


I have a tire in my screen and I would like to have a rope connected to the top of the tire and to the top of the screen. I have incorporated physics into my game with chipmunk+spaceManager so I need to some how have this rope react to the physics also. I just need it to move back and forth with the tire when it gets hit. I have used a cpConstraintNode to get a line drawn to use it as a rope up to this point but everything I have seen and looked into, there is no way to attach a CCSprite to a constraint. So my question is How would I go about creating this rope and have it react the same as the tire when it is moving? Here is my code that I have done with the constraint: I am using cocos2d and chipmunk + spaceManger

//The "rope"
        cpVect a1 = cpv(0,30);      //Local coordinates of tire
        cpVect a2 = cpv(70,320);    //World coordinates (staticBody is at (0,0))

        //calculate the length of the rope
        float max = cpvdist(cpBodyLocal2World(upper->body, a1), a2);
        cpConstraint *rope = [game.spaceManager addSlideToBody:upper->body fromBody:game.spaceManager.staticBody toBodyAnchor:a1 fromBodyAnchor:a2 minLength:1 maxLength:max];

        cpConstraintNode *ropeNode = [cpConstraintNode nodeWithConstraint:rope];
        ropeNode.color = ccBLUE;

Solution

  • This post presents a great way to draw ropes in Cocos2D using Verlet Integration.

    The only drawback is that the example uses Box2D. But the code can be ported to chipmunk.