# Collision response in tiled game

This topic is 3681 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am making a tile based platformer and am having some problems with my collision detection/response. Currently I am checking which tiles the player is in and then checking each to see if that tile can be collided with. This part of my code works perfectly, I think the problem is in how I handle the collision. I look at the direction that the player is moving in and move them back outside of the tile. This works in most cases except where the player is moving in both x and y because when pushing against a wall to the left it will detect they are inside of a tile and move them up and right outside of the tile rather than just right. I can't find any method of working out way to move the player that works in all scenarios. Any help would be appreciated. Thanks.

##### Share on other sites
You might be dealing with a corner collision detection. When moving in two directions it is important to take into account the fact that you might hit a corner. There are cases, however, when you want to detect if you hit a wall, floor, etc. The best way that I have seen to do this is to look at the speeds of the moving object (the one that is about to collide with the wall, block, etc.). This would be both the x and y speeds. Now I'm thinking that you character does not move tile by tile, right? Then what you would need to do is to check if your character is within the collision limits. Here's some code to illustrate that.

a = object that is checking for collision (like character)
b = object that is stationary (like block)

int limit = b.left - a.x_speed;

if (a.right < limit)
{

// Hit Left Wall

}

You can also do this for the top bottom and right sides of the stationary object. This way if a side of the moving object is outside the limit then you do nothing. No need to move the object in that direction.

This technique can also be applied to tiles as well. I bet you are still stumped. Ok, here's some ascii art. The starred box will be object a and the + box will be object b. The vertical and horizontal lines are used to mark collision boundries. Object a and b can be thought of as tiles.

|
++++|+++++++
*******+** | +
* + * | +
* + * | +
*******+** | +
++++|+++++++
|

|
++++|+++++++
****+****** +
* + | * +
* + | * +
****+****** +
++++|+++++++
|

So I have two scenarios. One, object a's right face is within the collision boundary, so I would move object a outside of object b. In scenario two, object a's right face is outside the collision boundary so no movement is done. The thing that is accomplishes is that when all faces are tested in this way there will always be at least one that is within the collision boundary. I guarantee this! So, again, the collision boundary should be set according to the speed in in the collision direction. For corner collisions where two faces are within their boundries the whole tile gets pushed out.

I hope you understand this, if not I'll be glad to post more examples. This technique does work because I have tested it a number of times before. It is only a matter of understanding it in order to be able to use it.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634087
• Total Posts
3015445
×