1

我正在尝试在 libGDX 中制作一条在其下端有一些物体的吊绳,绳索应该像Box2D 中的吊绳

我做了很多研究,libGDX 有它的ropeJoint方法但是如何使用它来制作绳子?

请帮助,这将是一个很大的帮助。

4

1 回答 1

0

我从回答类似问题 libGDX: Hanging Rope开始

并添加了一些代码来添加一些缺失的代码并修复锚点中的一些问题,我认为以下代码适合目标问题:

    BodyDef ropeStartDef = new BodyDef();
    int EACH_RING_DISTANCE = 10
    ropeStartDef.type = BodyType.StaticBody;
    ropeStartDef.position.set(m_camera.viewportWidth/2,m_camera.viewportHeight );
    Body ropStartBody = m_world.createBody(ropeStartDef);
    PolygonShape ropeStartShape = new PolygonShape();
    ropeStartShape.setAsBox(5, 5);
    ropStartBody.createFixture(ropeStartShape, 0);
    ropeStartShape.dispose();
    //----------------------------
    RevoluteJointDef jd = new RevoluteJointDef();
    Body prevBody = ropStartBody;
    int angle = -90;
    Vector2 position = ropeStartDef.position;
    BodyDef previousbodyDefinition= ropeStartDef;
    for(int i=0; i<5; 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 = m_world.createBody(bd);
            FixtureDef eachRingFD;
            eachRingFD = new FixtureDef();
            CircleShape chainCircleshape =new CircleShape();
            chainCircleshape.setRadius(4);
            eachRingFD.density = 2;
            eachRingFD.shape =chainCircleshape;
            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);
            //-----------------------added based on http://www.emanueleferonato.com/2009/10/05/basic-box2d-rope/
                RevoluteJointDef joint = new RevoluteJointDef();
                joint.initialize(prevBody, body, anchor);
                m_world.createJoint(joint);

            //------------------------end of added
            prevBody = body;
            previousbodyDefinition = bd;
        }
于 2014-07-01T15:39:09.027 回答