Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Advice on more accurate tile collision detection with Mappy

This thread is locked; no one can reply to it. rss feed Print
Advice on more accurate tile collision detection with Mappy
fny121
Member #15,915
March 2015

My game is a fairly simple orthogonal RPG. Environment tiles and sprites are all 48x48 pixels. I'm using bounding boxes for collision detection, and Mappy is my tile/map system.

I currently am able to do collision detection by marking the "tl" flag in Mappy for tiles which are obstacles (walls, fences, boulders, etc.) using the following basic code:

BLKSTR *currentBlock;
currentBlock = MapGetBlock((PLAYER::x/mapblockwidth), (PLAYER::y/mapblockheight));
if (currentBlock->tl)
blockIsObstacle = true;
else
blockIsObstacle = false;

The idea here being, obviously, that if a particular tile's blockIsObstacle result is true, the player cannot pass through it. While this works, it is not accurate: it's based strictly on the center point of the player. This method would allow the player to pass half-way through an obstacle before stopping.

What's the preferred/efficient and effective method of handling this? It's not that the game is incredibly intensive and requires the absolute most efficient solution to everything, but I was a little confused about what the "best" approach to this is, so I was curious what others have done (or think) about handling this. I may be making it out to be more complicated than it actually is, so I apologize if that's the case. :)

l j
Member #10,584
January 2009
avatar

Instead of testing a single point, test the corners of the player. The player will be able to touch 4 tiles at once.

This can break if you have sprites larger than the tiles in which case you can define 'hotspots', arbitrary points you defined yourself that are used to check if a collision takes place.

duncan perham
Member #15,403
November 2013

Another method would be to project points from the center of the character. For example, if the character takes up 50 pixels, from his center point, add/subtract 25, to create 4 test points(N,E,S,W). Then test each point to see if it has or will enter into a no go zone.

If required you could enhance this by testing corners too. I use a simple
bool IsPointInRect(Point *ThePoint, RECT *TheRect) function. Simply to test if the given point is within the rect, to test if a rect is in rect, test all 4 points.

None of these methods are super perfect for all jobs, but they do for simple stuff.

Go to: