libGDX: Hanging Rope [duplicate]

微笑、不失礼 提交于 2019-12-24 00:36:47

问题


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


来源:https://stackoverflow.com/questions/15433047/libgdx-hanging-rope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!