Sign in to follow this  
EvilNando

Help with tile collision

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 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?

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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........XXXXXXXX
XXXXXXXX........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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this