更改libgx中关节的坐标

时间:2015-09-11 01:02:10

标签: java libgdx box2d

我在libGDX中遇到的问题是:当我在两个方框(BodyA和BodyB)之间创建一个关节时,BodyA的关节是从屏幕坐标(0,0)创建的。我的BodyA不在坐标(0,0)。 我想从任何我想要的地方创建一个关节。我怎么能这样做?

private Body[] segments;
private RevoluteJoint[] joints;

@Override
public void create() {
    gravity = new Vector2(0, -9.81f);
    world = new World(gravity, true);

    camera = new OrthographicCamera();
    camera.setToOrtho(false, Gdx.graphics.getWidth() /*480*/, Gdx.graphics.getHeight() /*800*/);
    renderer = new Box2DDebugRenderer();

    createBody(400, 20, 240, 200, 0, true);

    createRope(20);

}

private void createRope(int length) {
    segments = new Body[length];
    joints = new RevoluteJoint[length - 1];
    RopeJoint[] ropeJoints = new RopeJoint[length - 1];

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DynamicBody;

    float width = 18f, height = 32f;

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(width / 2, height / 2);

    for (int i = 0; i < segments.length; i++) {
        segments[i] = world.createBody(bodyDef);
        segments[i].createFixture(shape, 2);
    }

    shape.dispose();

    RevoluteJointDef jointDef = new RevoluteJointDef();
    jointDef.localAnchorA.y = -height / 2;
    jointDef.localAnchorB.y = height / 2;

    for (int i = 0; i < joints.length; i++) {
        jointDef.bodyA = segments[i];
        jointDef.bodyB = segments[i + 1];
        jointDef.collideConnected = true;
        joints[i] = (RevoluteJoint) world.createJoint(jointDef);
    }

    RopeJointDef ropeJointDef = new RopeJointDef();
    ropeJointDef.localAnchorA.set(0, -height / 2);
    ropeJointDef.localAnchorB.set(0, height / 2);
    ropeJointDef.collideConnected = true;
    ropeJointDef.maxLength = height;

    for (int i = 0; i < ropeJoints.length; i++) {
        ropeJointDef.bodyA = segments[i];
        ropeJointDef.bodyB = segments[i + 1];
        ropeJointDef.collideConnected = true;
        ropeJoints[i] = (RopeJoint) world.createJoint(ropeJointDef);
    }
}

private Body createBody(float hx, float hy, float xVec, float yVec, float angle, boolean isStatic) {
    BodyDef bodyDef = new BodyDef();

    if(isStatic) {
        bodyDef.type = BodyType.StaticBody;
    }else {
        bodyDef.type = BodyType.DynamicBody;
    }

    PolygonShape shape = new PolygonShape();

    shape.setAsBox(hx, hy, new Vector2(xVec, yVec), angle);

    Body body = world.createBody(bodyDef);

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;

    body.createFixture(fixtureDef);

    return body;
}

1 个答案:

答案 0 :(得分:0)

您可以通过定义 localAnchorA localAnchorB 来设置“关节偏移量”。简而言之,联合创作模式看起来像:

    Body bodyA, bodyB;

    ...

    //I'm creating joint Revolute type
    RevoluteJointDef jointDef = new RevoluteJointDef();

    //I'm setting two bodies between which joint will be created
    jointDef.bodyA = bodyA;
    jointDef.bodyB = bodyB;

    //I'm setting local offset for bodyA and bodyB
    jointDef.localAnchorA.set( new Vector2(0, 10));  //the point over center of bodyA
    jointDef.localAnchorB.set( new Vector2(0, -10 );  //the point under center of bodyB

    ... //I can also define some joint characteristics like .enableLimit, .lowerAngle etc depending on joint type

    //finally I'm creating the joint
    world.createJoint(jointDef);

它应该创建类似的东西:

joint example