Diagonal collisions with tiles

May 23, 2013 at 1:18 AM
Hi everyone,

I have a problem with diagonal collisions.
I know how to make a rectangle bounding collision and it works but my map has diagonal tiles.

Anybody have a code exemple ?
Thanks and sorry for my english, I speak french usually.
Coordinator
May 23, 2013 at 8:20 AM
Assuming tiles that are, say, 64x64 pixels wide, you could check if a player has landed on a 45degree incline (going up from left to right) by checking if ty > 64 - tx where tx and ty are the player's position modulated to the tile size (64 in this case). That is, if px, py are the player's absolute position (of his/her feet) on the map in pixels, then, tx = px mod 64, and ty = py mod 64 give you the player's position on the tile.

To check if the player landed on an 45 degree incline going down from left to right, you would check if ty > tx.

If you have diagonals that are less steep and span two tiles, then for an incline going up from left to right, the check for the first tile would be ty > 64 - tx / 2 and for the second, ty > 32 - tx / 2.

Similarly, for a downward incline spanning two tiles, the check for the first tile would be ty > tx / 2 and for the second, ty > 32 + tx / 2

This would probably be easier to visualise with some graphs. Basically, if you replace the inequality (>) with an equality in the equations above, you will get plots for the diagonal lines. By using the inequality, you are checking if the player is beneath the line.

For diagonal ceilings, the same equations would apply, by the inequality needs to be reversed (<) in order to check if the player is above the line, rather than below.