Question

I am getting some weird some weird behaviour with my code. My bodies with the same MASK and CATEGORY when overlapping and touchDragged, recreate the previous 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;

Here is my MouseJoing implementation:

/**
 * 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;
}

Thanks for your time....

Was it helpful?

Solution

Fixed by iterating all created jointsworld.getJoints(worldJoints);and destroying the old ones. .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top