How do I handle specific tile/object collisions?
Posted
by
Thomas William Cannady
on Game Development
See other posts from Game Development
or by Thomas William Cannady
Published on 2012-01-25T18:58:48Z
Indexed on
2012/03/30
5:43 UTC
Read the original article
Hit count: 364
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.
© Game Development or respective owner