libGDX:悬挂绳打结的一些支点支点、libGDX

2023-09-08 00:03:55 作者:格式化ζ回憶

我试图让有一些对象挂绳在libGDX它的下端, 绳应该像在Box2D的挂绳

I am trying to make a hanging rope having some object at it lower end in libGDX, Rope should be like hanging rope in Box2D

我已经做了很多的研究,libGDX有它的 ropeJoint 的方法,但如何用它来作一根绳子?

I have done a lot of research, libGDX has its ropeJoint method but how to use it to make a rope?

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

Please help, It will be a great favor.

推荐答案

我已经开始与回答类似的问题 libGDX:挂绳

I've started with the answer to a similar question libGDX: Hanging Rope

和添加了一些code给它添加一些缺少code和解决一些问题,锚点,我认为以下code是一个合适的目标proble:

and added some code to it to add some missing code and fix some problem in anchor, I think the following code is a suitable to the target proble:

    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;
        }