Help with tile collision

Started by
4 comments, last by EvilNando 15 years, 8 months ago
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 Photobucket 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?
Advertisement
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.
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
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?
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.
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.
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
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

This topic is closed to new replies.

Advertisement