Help with tile collision
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?
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.
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.
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?
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement