How do I handle specific tile/object collisions?
- by Thomas William Cannady
What do I do after the bounding box test against a tile to determine whether there is a real collision against the contents of that tile? And if there is, how should I move the object in response to that collision?
I have a small object, and test for collisions against the tiles that each corner of it is on.
Here's my current code, which I run for each of those (up to) four tiles:
// get the bounding box of the object, in world space
objectBounds = object->bounds + object->position;
if ( (objectBounds.right >= tileBounds.left) &&
(objectBounds.left <= tileBounds.right) &&
(objectBounds.top >= tileBounds.bottom) &&
(objectBounds.bottom <= tileBounds.top))
{
// perform specific test to see if it's a left, top , bottom
// or right collision. If so, I check to see the nature of it
// and where I need to place the object to respond to that collision...
// [THIS IS THE PART THAT NEEDS WORK]
//
if( lastkey==keydown[right] && ((objectBounds.right >= tileBounds.left) &&
(objectBounds.right <= tileBounds.right) &&
(objectBounds.bottom >= tileBounds.bottom) &&
(objectBounds.bottom <= tileBounds.top)) )
{
object->position.x = tileBounds.left - objectBounds.width;
}
// etc.