libGDX: Hanging Rope [duplicate]

2019-07-15 04:52发布

问题:

This question is an exact duplicate of:

  • libGDX: Hanging rope knotted with some pivot 1 answer

I am new to android libGDX game engine and searching form many days to make a hanging rope,

I also read out all the Box2D documentation, There is a method ropJoint but no enough explanation given to use it.

Can any body help me to make a rope like this one. Hanging Rope in Box2d

I also tried to search for some good libGDX book for android but couldn't find.

your help is required, please do the favor.

回答1:

Just create some rectangle bodies (setAsBox in box2d) in a loop and connect each other with revolute joints (Make a RevoluteJointDef before loop and inside loop call initialize(..)).

This is my code:

RevoluteJointDef jd = new RevoluteJointDef();
Body prevBody = startBody;
for(int i=0; i<ringCount; i++)
    {
        BodyDef bd = new BodyDef();
        bd.type = BodyType.DynamicBody;
        bd.angle = angle-MathUtils.PI/2;
        bd.position.set(position.x + i*MathUtils.cos(angle)*EACH_RING_DISTANCE, 
                        position.y + i*MathUtils.sin(angle)*EACH_RING_DISTANCE);
        Body body = world.createBody(bd);
        body.createFixture(eachRingFD);

        Vector2 anchor = new Vector2(bd.position.x - MathUtils.cos(angle)*EACH_RING_DISTANCE/2f, 
                                     bd.position.y - MathUtils.sin(angle)*EACH_RING_DISTANCE/2f);
        jd.initialize(prevBody, body, anchor);
        prevBody = body;
    }
//connect a hanging shape to rope here if exists