LIBGDX collision with bottom part of box only

Viewed 740

Im trying to get my collision using box2D in jumping game work correct but i have some problems. The player have Box Shape and Platform is just a line made with chainShape.

First problem is that i would like to enable collision only when Player touch Platform with bottom line of it's box. I tried to do that by checking the y coordinates of body but it doesnt worked becouse the diffrence beetween platform and bottom part of box while collision was about 0.5 units.

Second problem is that at the moment the collision is turning on two or three times for same platform in single touch. Here i don't have idea how i can fix it.

Actual code for collision in my code is:

@Override
public boolean shouldCollide(Fixture fixtureA, Fixture fixtureB) {
    if(fixtureA == fixture || fixtureB == fixture){
        return body.getLinearVelocity().y< 0; //&& (fixtureA.getBody().getPosition().y <= fixtureB.getBody().getPosition().y);
    }
    return false;
}

@Override
public void preSolve(Contact contact, Manifold oldManifold) {
    if(contact.getFixtureA() == fixture || contact.getFixtureB() == fixture){
        //System.out.println("Contact A: "+ contact.getFixtureA().getBody().getPosition().y);
        //System.out.println("Contact B: "+ contact.getFixtureB().getBody().getPosition().y);
        if(contact.getFixtureA().getBody().getPosition().y == contact.getFixtureB().getBody().getPosition().y)
            contact.setEnabled(true);
        else
            contact.setEnabled(false);
    }
}
//COLLISION CALCULATION
@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
                body.applyLinearImpulse(0, jumpPower, body.getWorldCenter().x, body.getWorldCenter().y, true);
}

Thanks in advance.

0 Answers
Related