Page 1 of 1

A brief query on collision detection

Posted: Wed Sep 09, 2009 10:37 am
by hurstshifter
Hey Everyone,

I've recently began implementing some collision detection in a small game I'm working on in SDL/C++. I've got it working for basic projectiles that travel in a straight line along the Y axis and collide with enemies that travel in the opposite direction. My main question is on how the collision function accepts its arguments. The way I had initially performed this was by having the function accept a projectile and enemy object pointer and make its calculations based on that, but I realized pretty quickly that this is sloppy for several reasons (passing a lot of unnecessary data to the function, cannot port the collision detection to other types of objects, etc...). Would it be a better idea to have an accessor function in each object that returns its SDL_Rect(x,y,w,h) and have the collision detection function accept 2 of these as its formal parameters? I assume this will give me the freedom of using this function for essentially any object. Trying to keep it simple here for now.

-Hurst

Re: A brief query on collision detection

Posted: Wed Sep 09, 2009 11:14 am
by Bakkon
hurstshifter wrote:cannot port the collision detection to other types of objects, etc...)
An "easy" (your mileage may vary) way to cover this is to rework your object hierarchy. Have all objects be a decedent of a main root object. Then your collision method can take in two pointers to this main root.

Code: Select all

void Object::Update()
{
     for( all objects in list/vector/array )         // loop through all objects in the game
          if(this != array[n])                       // don't collide with yourself, dummy
               if(CheckCollision(this, array[n])     // check if the objects are colliding, method returns true/false
                    Collide(array[n])                // perform reactions this objects has when colliding with array[n]
}
Pretty brute force way of doing it, but it should get you started.

Re: A brief query on collision detection

Posted: Wed Sep 09, 2009 12:55 pm
by hurstshifter
Bakkon wrote:
hurstshifter wrote:cannot port the collision detection to other types of objects, etc...)
An "easy" (your mileage may vary) way to cover this is to rework your object hierarchy. Have all objects be a decedent of a main root object. Then your collision method can take in two pointers to this main root.

Code: Select all

void Object::Update()
{
     for( all objects in list/vector/array )         // loop through all objects in the game
          if(this != array[n])                       // don't collide with yourself, dummy
               if(CheckCollision(this, array[n])     // check if the objects are colliding, method returns true/false
                    Collide(array[n])                // perform reactions this objects has when colliding with array[n]
}
Pretty brute force way of doing it, but it should get you started.

I actually already have all of my objects inheriting from a partent "Object" class, so this should be rather easy to implement. Thx