# Detecting overlap with Phaser P2 physics

In a 2D platformer, I'd like to implement a ladder. I have collision working between the player and ground tiles. Now I would like to be able to detect when the player walks through a ladder tile (which can be one of three tile indices). When the player is overlapping a ladder and up is being pressed, the climbing state would be set.

Ideally, I'd like to get a callback when this happens. It seems that the overlap method from Arcade physics is not available in P2. What is there instead to implement overlap detection?

• May 20, 2018 at 22:35

If you know the top, right, left, and bottom positions of the hitbox you can check to see if the character box is within the ladder box by comparing sides of the boxes. This would let you know if the character is within the the ladder tile.

For example, left of ladder is x.position 18 and right is x.position 42. Left of player is x.position 19 and right is x.position 32.

if (player.left > ladder.left && player.right < ladder.right)
{
// Player is inside ladder hit box.
}


You can also check the top and bottom of rectangle too.

• Sure, but I'm using a physics engine packaged into a framework to avoid doing that. I can build a quad-tree and check every shape... this is hit detection and the very essence of the physics engine. The only thing is that it should allow penetrating the hit object in this case. I feel like there should be an easy way of doing this? It sounds like such a common need. Mar 5, 2018 at 14:03