简体   繁体   English

Box2D LibGDX绳索问题

[英]Box2D LibGDX Rope issue

I am creating a rope with a series of Box2D bodies with the following code: 我正在使用以下代码创建带有一系列Box2D实体的绳索:

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: 对于BodyFactory.createBox它需要满足以下条件:

world, xPos, yPos, width, height BodyType, density, friction, restitution, fixture user data.(length is same because it uses boxes) 世界,xPos,yPos,宽度,高度BodyType,密度,摩擦力,恢复,夹具用户数据。(长度相同,因为它使用方框)

Core.PPM is the pixels per meter. Core.PPM是每米的像素。 Also note that the position is being divided by PPM in the constructor. 另请注意,该位置在构造函数中被PPM划分。

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. 关节的localAnchor对于身体的中心,不是绝对值。 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 这意味着,如果要将关节设置为bodyA中心底部bodyA 中心顶部bodyB需要使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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