如何在box2d中创建一个WeldJoint以将两个实体连接在一起?

时间:2014-01-16 11:57:18

标签: java libgdx

我目前正试图在一个矩形体的顶部附加一个圆形体,这样它就像一个火柴棍,我创建了定义和东西,但是身体单独移动,矩形体移动但是圆形体保持静止,它不会像一个人一样移动。

这是代码

/****************************************CREATING THE FIRST BODY (RECTANGLE BODY) ***********/
    rectangleBodyDef = new BodyDef();
    rectangleBodyDef.type = BodyType.DynamicBody;
    rectangleBodyDef.position.set(-10,2);
    rectangleBody = world.createBody(rectangleBodyDef);
    rectangleBodyShape = new PolygonShape();
    rectangleBodyShape.setAsBox(2f, 0.75f);
    rectangleBodyFixtureDef = new FixtureDef();
    rectangleBodyFixtureDef.shape = rectangleBodyShape;
    rectangleBodyFixtureDef.restitution = 0.8f;
    rectangleBody.createFixture(rectangleBodyFixtureDef);
    rectangleBody.setUserData(effect);

    /***************************************CREATING THE SECOND BODY (CIRCLE BODY) ************/
    circleBodyDef = new BodyDef();
    circleBodyDef.type = BodyType.DynamicBody;
    circleBodyDef.position.set(-7,2);
    circleBody = world.createBody(circleBodyDef);
    circleShape = new CircleShape();
    circleShape.setRadius(0.75f);
    circleFixtureDef = new FixtureDef();
    circleFixtureDef.shape = circleShape;
    circleFixtureDef.restitution = 0.8f;
    circleBody.createFixture(circleFixtureDef);

    /***********************************WELD JOINT*********************************************/
    weldJointDef = new WeldJointDef();
    weldJointDef.bodyA = rectangleBody;
    weldJointDef.bodyB = circleBody;
    weldJointDef.type =JointType.WeldJoint;
    weldJointDef.collideConnected = true;
    weldJointDef.localAnchorA.set(1, 0);
    weldJointDef.localAnchorB.set(-2,0);
    world.createJoint(weldJointDef);

0 个答案:

没有答案