# Collision detection for sloping tiles

I've been looking into adding 'sloping' tiles into my platform game and my first attempt at collision detection with these tiles has produced some promising results (considering it's a first attempt), however, there is something that I just can't work out.

First, some background.

My tiles (on my dev device which is a Google Nexus 10 tablet) are 102 pixels wide by 100 pixels tall (this was done on purpose so they fit better to the screen).

So, my 'slope tile' looks something like this:

Not being a perfect square, the slope isn't quite 45 degrees but that shouldn't really matter I don't think.

Now, in order to find out the vertical 'centre' for collision purposes, I'm doing the following:

• Find out where the sprite is in relation to the X Coordinate of the tile. (So, if the tile is at 100 and the sprite is at 110, then I am 10 in from the X of the tile.

• I then multiply that by my slope (slope being height/width or 100/102 in my case)

• I then divide by 2 to get the centre

• Finally, I subtract that result from the height of the tile (to invert it)

This gives me the vertical center which I can compare with the centre of my sprite to check for collision.

So, my code looks like this:

Results

When I run this, although my sprite can slide up and down the slope it doesn't follow the (visible) path of my tile so I get something like this:

So when moving down the slope it moves in a jagged line and when trying to move up, it moves up the slope until it hits the next tile along, then gets stuck

Would appreciate if someone could point out why my sprite path is not following the slope correctly or anything else I may be doing incorrectly.

Edit 1 - results from further experimenting

I've noticed that the sprite when at the top-left of a slope tile sinks into the tile slightly, this is why the sprite was 'getting stuck' when moving up - because it was hitting solid blocks behind (which have their own collision detection)

Strangely, I've also noticed that if I remove the 'divide by 2' from my above calculations the path of the sprite is pretty much OK although the sprite itself still sinks into the tile slightly (But now throughout the whole slope) - therefore if I remove the solid blocks, it works OK.

So I have no idea why not dividing by 2 makes sense and also why my sprite is not correctly sitting on the slope!!

• Very well written question, of the few actually worthy for this site. Dec 31, 2013 at 15:40
• Your equation appears to be correct, it's more likely the sprite is falling in between the tiles or something similar. How are you detecting the tile that's under your sprite? Maybe some visual debugging of highlighting the tile you're colliding with (and even keeping data points to you can actually draw the line of the sprite) would be helpful.
– House
Dec 31, 2013 at 15:57
• – House
Dec 31, 2013 at 16:03
• Thanks @Byte56 I did have a read of those questions (plus other on other forums) I've updated my question. I was colliding with solid tiles under my sloped ones. I've removed them but oddly, it still follows this strange path. Even more odd (as in my edit above) if I don't divide by 2, then the path looks consistent (even though my sprite falls slightly into the tile) Dec 31, 2013 at 16:11
• I've had a similar issue in my platformer, ended up being a "one off" error. Actually it was an unnecessary one off somewhere else that I accounted for everywhere else but reared its ugly head when it came to slope collision resolve. Dec 31, 2013 at 18:05