当身体重叠时,之前的MouseJoints不会被破坏 - 截图

时间:2014-05-14 19:26:48

标签: libgdx box2d

我的代码变得奇怪了一些奇怪的行为。 当重叠和touchDragged时,我的身体具有相同的MASKCATEGORY,重新创建以前的mouseJoints。

enter image description here

    //collision
final short CATEGORY_PLAYER = 0x0001;  // 0000000000000001 in binary
final short CATEGORY_SCENERY = 0x0004; // 0000000000000100 in binary

final short MASK_PLAYER = CATEGORY_SCENERY; // or ~CATEGORY_PLAYER
short MASK_SCENERY = -1;

以下是我的MouseJoing实施:

/**
 * Creates the MouseJoint definition.
 * 
 * @param body
 *            First body of the joint (i.e. ground, walls, etc.)
 */
private void createMouseJointDefinition(Body body) {
    mouseJointDef = new MouseJointDef();
    mouseJointDef.bodyA = body;
    mouseJointDef.collideConnected = false;
    mouseJointDef.maxForce = 500;
}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    /*
     * Define a new QueryCallback. This callback will be used in
     * world.QueryAABB method.
     */
    QueryCallback queryCallback = new QueryCallback() {

        @Override
        public boolean reportFixture(Fixture fixture) {
            boolean testResult;

            /*
             * If the hit point is inside the fixture of the body, create a
             * new MouseJoint.
             */
            if (testResult = fixture.testPoint(touchPosition.x,
                    touchPosition.y)) {
                mouseJointDef.bodyB = fixture.getBody();
                mouseJointDef.target.set(touchPosition.x, touchPosition.y);
                mouseJoint = (MouseJoint) world.createJoint(mouseJointDef);
            }

            return testResult;
        }
    };

    /* Translate camera point to world point */
    camera.unproject(touchPosition.set(screenX, screenY, 0));

    /*
     * Query the world for all fixtures that potentially overlap the touched
     * point.
     */
    world.QueryAABB(queryCallback, touchPosition.x, touchPosition.y,
            touchPosition.x, touchPosition.y);

    return true;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {

    /* Whether the input was processed */
    boolean processed = false;

    /* If a MouseJoint is defined, destroy it */
    if (mouseJoint != null) {
        world.destroyJoint(mouseJoint);
        mouseJoint = null;
        processed = true;
    }

    return processed;
}

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {

    /* Whether the input was processed */
    boolean processed = false;

    /*
     * If a MouseJoint is defined, update its target with current position.
     */
    if (mouseJoint != null) {

        /* Translate camera point to world point */
        camera.unproject(touchPosition.set(screenX, screenY, 0));
        mouseJoint.setTarget(new Vector2(touchPosition.x, touchPosition.y));
    }

    return processed;
}

谢谢你的时间......

1 个答案:

答案 0 :(得分:0)

通过迭代所有创建的关节world.getJoints(worldJoints);并销毁旧关节来修复。