Collision

Hey Community,

i have a question about Collision.
How can you determine what sides of a Rectangle intersect with the other Rectangle.
Or is there another way to determine if you get killed or kill the enemy (e.g. Mario).

Cant find it anywhere.

Thx for helping.
Moep

I guess you want something like the following:

  • if player hits top of enemy, then kill enemy
  • if player hits side or bottom of enemy, then kill player

I assume you are already using a standard rectangle intersection check to determine if player + enemy collide.
If they do collide, then you can compare previous and current positions of the player’s bottom edge, with the previous and current positions of the enemy’s top edge. A bit like this in pseudo code:


if (player collides with enemy) and (player.previous.bottom.y was above enemy.previous.top.y) and (player.bottom.y is below enemy.top.y)
    player has hit top of enemy, so kill enemy
else
    player has hit left, right or bottom of enemy, so kill player

Note that this isn’t completely accurate, but it’s quite simple to code and has always worked well enough for me.

Ok thank you i will try that tomorrow.

Just check the distance along the Y from the player’s bottom to the enemy’s top. If it’s within a small enough margin then he is considered to have bounced on top. You may also want to check X distance along each of the player’s and enemy’s sides, but I didn’t do this as I like to make things more in the player’s favor.