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
A brief query on collision detection
Moderator: Coders of Rage
- hurstshifter
- 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
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
http://www.thenerdnight.com
- Bakkon
- 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
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.hurstshifter wrote:cannot port the collision detection to other types of objects, etc...)
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]
}
- hurstshifter
- 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
Bakkon wrote: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.hurstshifter wrote:cannot port the collision detection to other types of objects, etc...)
Pretty brute force way of doing it, but it should get you started.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] }
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
http://www.thenerdnight.com