简体   繁体   中英

Box2D LibGDX Rope issue

I am creating a rope with a series of Box2D bodies with the following code:

public void create(float length, float ropeLength){
    Array<Body> bodies = new Array<Body>();
    bodies.add(BodyFactory.createBox(world, position.x, position.y, length, length, BodyType.StaticBody, 0, 0, 0, "RopeMain"));

    for(int i = 1; i < ropeLength; i++){
        bodies.add(BodyFactory.createBox(world, position.x, position.y - (((length/2) / Core.PPM) * i),
                length, length, BodyType.DynamicBody, 0, 0, 0, "RopeBody" + i));

        RopeJointDef rDef = new RopeJointDef();
        rDef.bodyA = bodies.get(i - 1);
        rDef.bodyB = bodies.get(i);
        rDef.collideConnected = true;
        rDef.maxLength = (length/2)/Core.PPM;
        rDef.localAnchorA.set(position.x, -((length / 2) / Core.PPM));
        rDef.localAnchorB.set(position.x, ((length / 2) / Core.PPM));
        world.createJoint(rDef);
    }
}

Allow me to share some parameters...

For BodyFactory.createBox it requires the following:

world, xPos, yPos, width, height BodyType, density, friction, restitution, fixture user data.(length is same because it uses boxes)

Core.PPM is the pixels per meter. Also note that the position is being divided by PPM in the constructor.

Question: why do the following lines shoot to the right?

在此处输入图片说明

Any info is very helpful, also how will density, friction, and restitution affect the rope? Thanks!

The joint's localAnchor is relative to the center of the body and isn't an absolute value. That means that if you want to set the joint to the center-bottom of bodyA and center-top of bodyB you need to use

    rDef.localAnchorA.set(0, -((length / 2) / Core.PPM));
    rDef.localAnchorB.set(0, ((length / 2) / Core.PPM));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM