1
\$\begingroup\$

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?

\$\endgroup\$
1

2 Answers 2

0
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ Mar 5, 2018 at 14:03
0
\$\begingroup\$

From a related answer: https://gamedev.stackexchange.com/a/109446/43472

As for overlap, P2 doesn't support it directly, but there is an equivalent solution known as the "Postbroadphase Callback". Basically this is a callback where P2 collects all potential collisions and then asks you - via the callback - whether to allow those collisions to resolve or be ignored. You return false in the callback to tell P2 not to perform collision resolution, but the fact that the callback was called in the first place means that those two bodies have overlapped, so this is where you put your overlap handling code.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .