# Help with tile collision

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

## Recommended Posts

Hi im trying to get tile collisions working actors can move freely on the map collisions are tiles which are flagged by the collision flag. What I think Im geting wrong is the calculation to determine in which tile the actor currently is usually i do actor.position / tile_size but Im starting to believe that the rounding is returning me unexpected data here is an example blue box is the actual collision box, colored boxes are the areas I get by doing the divisions. im not sure if I should apply speed before or after doing the collision any insights?

##### Share on other sites
If your collision box is always the same size or smaller on all sides as your tiles, then one collision check at each corner after applying movement should be good; abort the move if collision registers true.

If your collision box might be more than one tile across on any side, then find what tile each corner is in, and then iterate across the edges and do normal rectangle-rectangle collision between each tile and the hitbox. If moving, you can optimize this to only test on the leading edges; if moving down and left, test only the bottom and left edges for collision against tiles.

##### Share on other sites
yes I implemented this theory and while it works, it took away the possibility for me to use corridors that are 1-tile wide, since it will always register a collision with one of the sides.

Is there a way to resolve this?

##### Share on other sites
I think you may need to substract one from the character's width/height. Otherwise supposing tile and character width is 32 and the character's left x is 0, right x would be 0+32 = 32, and this divided by tile width would give that the right corner is on the neighbouring tile, whereas in fact it isn't.

##### Share on other sites
Also, implement a 'nudge'. If only one leading corner registers collision, and the rectangle is within a threshold of a tile boundary, 'nudge' it to align with the tile grid. That way, a character walking up to such a 1-tile corridor a little offset from aligned with it will automatically slide over into the corridor when they hit the wall.

Consider the following diagram.
XXXXXXXX........XXXXXXXXXXXXXXXX........XXXXXXXX..........CCCCCCCC................CCCCCCCC................CCCCCCCC................CCCCCCCC......................................................

If the character tries to move north, the collision engine will see only one corner (top-right) colliding, and that character's motion-parallel edge (character's right side) within threshold of a tile boundary, so it'll nudge him over to step into the corridor.

##### Share on other sites
yeah of course!

so thats what is going under in games like zelda (any 2D version).

that must be what gives the feeling that the corners are round shaped

• ### What is your GameDev Story?

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

• 17
• 14
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634094
• Total Posts
3015500
×