Collision Detection Suggestions

Started by
2 comments, last by xIshtarx 19 years, 6 months ago
Just wondering how each of you have implemented collision detection of two sprites in your games. I started out using the one in "Game Programming All In One 2nd Ed." where you compare the central point of the source sprite with the entire bitmap area of the colliding sprite. Although it wasn't precise, it was sufficient for being simple and easy to implement. Now I've developed my own sprite collision detection function: for each sprite, you may define x amount of rectangles on the fly via a createCollisionRectangle() function such that you create enough rectangles to encompass the entire sprite without including a lot of "empty space" or leaving out odd-shaped parts of the sprite. Then the collide() function compares all the rectangles of the source sprite with all the rectangles of the colliding sprite. It's seems very accurate. I was just curious on how each of you use collision detection.
Advertisement
I am trying to use bounding-volumes in my first attempt at game-making, crude but effective.
I set up a structure for my different items such as spaceship, bullets,aliens and have a collision radius for each one when they confict i carry out the actions, such as kill alien and remove bullet.............i got this from another guy who was a tad smart!

void Collision( Actor *a, Actor *b )
{
if( a->active && b->active )
{
float r=(float)sqrt((a->x-b->x)*(a->x-b->x)+(a->y-b->y)*(a->y-b->y));
if( r<(a->colr+b->colr) )
{
if( b->type==ALIEN )
{
if( a->type==BULLET )
I got the book Game Programming All In One. And the collision detection for bounding rectangles is bad, because its slow and it will sometimes say theres no collision when there is. I dunno if its the same in 2nd ed. What u say, is indeed very accurate but may become CPU expensive if u have alot of rectangles. Id just use this:
if ((rect1.x < rect2.x + rect1.width/2 + rect2.width/2) &&
(rect1.x > rect2.x - rect1.width/2 - rect2.width/2) &&
(rect1.y < rect2.y + rect1.height/2 + rect2.height/2) &&
(rect1.y > rect2.y - rect1.height/2 - rect2.height/2))
{return true;}
Not very precise, but fast

This topic is closed to new replies.

Advertisement