在libgdx中使用Prismatic Joint

时间:2016-08-21 21:13:28

标签: libgdx box2d

我试图创建一个电梯。我使用PrismaticJointDef,但是身体A具有相同的身体位置B.如何定义两个身体之间的距离?我也使用了DistanceJointDef,但得到了相同的结果。 我希望像这个视频得到这样的结果。 https://www.youtube.com/watch?v=rzNjNBKYuGI

private void defineBodyA(){
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(getX() + getWidth() / 2, getY() + getHeight() / 2);
    bodyA = world.createBody(bodyDef);
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(getWidth() / 2, getHeight() / 2);
    fixtureDef.shape = shape;
    bodyA.createFixture(fixtureDef);
    bodyA.setLinearDamping(30f);
    shape.dispose();
}


private void defineBodyB{
    bodyDef.position.set(getX() + getWidth() / 2, (getY() + 80 / PPM);
    bodyDef.type = BodyDef.BodyType.StaticBody;
    bodyB = world.createBody(bodyDef);
    PolygonShape upOfElevator = new PolygonShape();
    upOfElevator.setAsBox(16f / PPM, 0.5f / PPM);
    fixtureDef.shape = upOfElevator;
    bodyB.createFixture(fixtureDef);
    upOfElevator.dispose();
}

PrismaticJointDef prismaticJointDef = new PrismaticJointDef();
    prismaticJointDef.bodyA = bodyA;
    prismaticJointDef.bodyB = bodyB;
    world.createJoint(prismaticJointDef);

DistanceJointDef distanceJointDef = new DistanceJointDef();
    distanceJointDef.bodyA = bodyA;
    distanceJointDef.bodyB = bodyB;
    distanceJointDef.length = 100 / PPM;
    world.createJoint(distanceJointDef);

1 个答案:

答案 0 :(得分:0)

此视频可能会有所帮助https://www.youtube.com/watch?v=l7w5vGTESHI