A brief query on collision detection

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

A brief query on collision detection

Post 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
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: A brief query on collision detection

Post 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.
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: A brief query on collision detection

Post 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
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
Post Reply