2
\$\begingroup\$

i'm facing an issue at the moment in my javascript/html platformer game that despite searching for this issue i can't solve. The problem is this:

enter image description here

When i place two platforms next to each other and the player moves between them, the algorithm believes the player is colliding with the side (left or right) face of the next platform. This is because all my bounding boxes are placed in an array and iterated through, the algorithm doesn't care if the player has to pass through one collider to get to another, it just thinks it has collided with both.

One solution is of course to make it consider what it has collided with first, another is simply not to place two platforms together. My solution would be to implement a kind of occlusion culling with collides, which if the player collides with a top face it can't collide with a side face if that side face's top face is higher or equal to top first that was being collided in.

That was hard enough to explain let alone implement and i just feel like there is a better work around that wouldn't be too difficult to implement and would be efficient.

My solution:

enter image description here

Another solution is to disable gravity if the player is in contact with the ground, this must be done carefully not to trigger an oscillation effect and also means that two iterations need to be made, one for collision in the y (done first) and the other for collisions in the x (done second).

\$\endgroup\$
5

1 Answer 1

1
\$\begingroup\$

Try splitting your collision/response into two separate phases; first for the Y axis and then a second pass for the X axis. This will ensure that the character is already pushed "up" according to whatever is under the character's feet before wall collisions are applied and stop momentum.

You'll want to apply a threshold/limit to how much vertical separation is applied, otherwise the character will be able to "walk over" any wall. The threshold should be a fraction of the character's height, typically; usually between a quarter and an eighth, maybe even less.

This approach not only avoids ghost collisions between adjacent tiles, it also helps address minor discrepancies in position (e.g. if the new platform is 0.0073f units higher than the current platform, the character will still be able to walk onto it smoothly). As an added bonus, the character will now be able to "walk up" shallow steps! In pseudo code:

for each tile/platform:
  if character intersects:
    find Y overlap
    if overlap < threshold:
      character Y position -= Y overlap

for each tile/platform:
  if character intersects:
    find X overlap
    character X position -= X overlap

You might want to invert the order of axis checking when the character is falling or jumping so that the character will slide on the sides of any tiles/platforms.

This approach is not perfect; there are still "edge" cases (sorry) where you'll get ghost collisions, but it works for a good number of games.

I also highly recommend that you read and then re-read http://higherorderfun.com/blog/2012/05/20/the-guide-to-implementing-2d-platformers/. It doesn't answer your specific scenario, but it might answer some other questions you don't know you have yet. :)

\$\endgroup\$

You must log in to answer this question.

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