Tuesday, June 13, 2017

box2d - Libgdx body passing through block then fall on it


I'm using libgdx and its physic engine Box2d. My question is : how could I make my box2d body go through a block , ignore the first collison than make the block active so the ball can fall onto it. A good example is doodle jump.


enter image description here



Answer



I finally made it :)



Here's my code :


public void preSolve(Contact contact, Manifold oldManifold) {
fixtureA = contact.getFixtureA();
fixtureB = contact.getFixtureB();
if(fixtureA.getBody().getUserData() == "platform" && fixtureB.getBody().getUserData() == "ball"|| fixtureA.getBody().getUserData() == "ball" && fixtureB.getBody().getUserData() == "platform"){
if (fixtureA.getBody().getUserData() == "platform") {
platform_y = fixtureA.getBody().getPosition().y;
ball_y = fixtureB.getBody().getPosition().y;
} else if(fixtureA.getBody().getUserData() == "ball") {
ball_y = fixtureA.getBody().getPosition().y;

platform_y = fixtureB.getBody().getPosition().y;
}
if(ball_y < platform_y + 1.5F) { //the ball is below
contact.setEnabled(false);
}
}

}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...