Box2d Via Libgdx: How To Create One Mousejoint Per Body For Separate Android Touches?
What's up everyone, Thanks for your time. I am making a Pong clone, and I want to restrict Box2D to two MouseJoints maximum; one MouseJoint maximum per paddle. The MouseJoints shou
Solution 1:
From what I can see the tempBody
is never reset to null. What that means is that the first time you touch the pad it sets the tempBody
to the touched paddle and then when you press outside the body the callback will not find a new body but not reset the testBody
to null, so when you assign testBody
to hitBody[pointer]
it is setting it to the first paddle.
The way your touch down function should look like is:
@OverridepublicbooleantouchDown(int screenX, int screenY, int pointer, int button) {
testPoint.set(screenX, screenY, 0);
camera.unproject(testPoint);
// ask the world which bodies are within the given// bounding box around the mouse pointer
hitBody[pointer] = null;
world.QueryAABB(callback, testPoint.x - 1.0f, testPoint.y - 1.0f, testPoint.x + 1.0f, testPoint.y + 1.0f);
hitBody[pointer] = tempBody;
// if we hit something we create a new mouse joint// and attach it to the hit body.if (hitBody[pointer] != null) {
MouseJointDefdef=newMouseJointDef();
def.bodyA = groundBody;
def.bodyB = hitBody[pointer];
def.collideConnected = true;
def.target.set(hitBody[pointer].getPosition().x, hitBody[pointer].getPosition().y);
def.maxForce = 3000.0f * hitBody[pointer].getMass();
mouseJoint[pointer] = (MouseJoint)world.createJoint(def);
hitBody[pointer].setAwake(true);
} else {
}
tempBody = null;
returnfalse;
}
This way the tempBody is always reset to null after use.
Post a Comment for "Box2d Via Libgdx: How To Create One Mousejoint Per Body For Separate Android Touches?"