Search code examples
javalibgdxbox2d

How to make one way platform on which i can come down if button pressed


So, what i want:

  • If my player's body linear velocity.y > 0 then move through platform.
  • If my player's body colliding with platform and button "DOWN" pressed, move down through it

What i've tried:

switch (fixA.getFilterData().categoryBits | fixB.getFilterData().categoryBits) {
        case Game.PLATFORM_BIT | Game.PLAYER_BIT:
            if (playerBody == contactEntityA.getBody()) {
                if (playerBody.getBody().getPosition().y <
                        contactEntityB.getBody().getPosition().y + .5 || playerController.isDownPressed()) { // .5 is half height of platform body
                    contact.isEnabled = false;
                }
            } else {
                if (playerBody.getBody().getPosition().y <
                        contactEntityA.getBody().getPosition().y + .5 || playerController.isDownPressed()) { // .5 is half height of platform body
                    contact.isEnabled = false;
                }
            }
        break;
}

My problem is:

Box2d doesn't make contacts if body doesn't move for a few seconds. So after some time, contact doesn't occur and i can't move down.


Solution

  • Simple hack helped me. I'm just applying little velocity to a body each frame.

    playerBody.applyLinearImpulse(0f, .000001f, .5f, .5f, true); 
    

    I'm not sure that is the best solution, write me please if you know how to do better.