Collision detection between all objects

Started by
5 comments, last by mjfara 13 years, 9 months ago
Hello everyone,

I'm coding in C++, using the Airplay SDK for iPhone deployment.

I've managed to get simple 2-d collision detection to work.
I have implemented two methods, one is terrain collision, which I am using tiles for. And the other is object collision, which I use bounding boxes.

Here is my scenario at the moment.

I have 4 classes:
Player
Enemy
Bullet
Map

Terrain collision is simple, when I fire a bullet I use the map class to check surrounding tiles for bounds. This is no problem, as the bullet moves, so does the tile it's on, and it checks again.

Now, in what class should I put my object collision detection?
In the bullet class? Or in the enemy class?

If I put it in the bullet class, I will have to run a loop for every enemy object currently loaded, and see if they collide with that bullet, correct?

If i put it in the enemy class, I will run a loop for every bullet object currently loaded.

I plan to have many enemies on screen at once, upwards of 30, will this be a performance hit? Is there a better way to go about it rather than looping through every object on the screen?

As always your help is appreciated, I couldn't be making such progress without you guys.

=)
Advertisement
You could have a "middle-man" as it were that will check for collisions between objects and then inform said objects (Through a method call or some sort of callback) so that they can individually do something based on this event happening.

So for example when a bullet collides with an enemy both the bullet and enemy will be told that they have collided with the enemy or bullet respectively. The bullet would probably just remove itself or perhaps create a special explosion effect or something and the enemy can take a chunk off it's health or begin it's death sequence.

Performance wise I wouldn't have thought 30 enemys on screen would really slow the collision system down though I'm not very familiar with the iPhone's hardware. If you want to think about optimise it maybe think of some sort of partitioning system to reduce the amount of comparisons to be made, though I think this would probably be overkill and maybe even hurt performance even more.
It's not a bug... it's a feature!
I'd recommend putting the collision detection code not in the 'bullet' or 'enemy' class, but rather in a class or module that has access to all of the bullets and enemies in the simulation.

I would start by using a simple loop to check for collisions. If there's a performance hit (and there could be, I imagine), you may need to look for ways to optimize the process, for example:

1. An 'early out' such as a bounding circle or AABB test. (Of course if you're already using simple shapes such as circles or axis-aligned boxes for your entities, that won't help.)

2. A broad-phase culling step of some sort.
So in my main loop I would have a separate function that will check for a collision with every object?
So I would have to have every object stored in an array or some other container, correct?
Something like:

obj *array;

void checkcollision()
{
for(int i = 0; i < arraysize; i++)
{
for(int j = 0; j < arraysize; j++)
{
if(j != i)
{
//Check for collision between array and array[j]
}
}
}
}

This seems like it will work, hopefully it's not a performance hit.
Is there a different type of container that is better for this situation than an array?
I remember making linked lists back in college, but forgot the concept completely.

My language of choice is C#, which has an ArrayList, is there anything similar in C++?
Quote:obj *array;

void checkcollision()
{
for(int i = 0; i < arraysize; i++)
{
for(int j = 0; j < arraysize; j++)
{
if(j != i)
{
//Check for collision between array and array[j]
}
}
}
}
If all your objects are stored in the same array, you should arrange your nested loop so that the same pair isn't tested multiple times. (For example, in the above loop, object '0' will be tested against object '1', and then later, object '1' will be tested against object '0'.)
Quote:Is there a different type of container that is better for this situation than an array?
I remember making linked lists back in college, but forgot the concept completely.
The most straightforward choice in C++ would probably be std::vector.
You can use the STL container std::vector for that:

#include <vector>std::vector<Obj> objectArray;


Then you can add and remove objects at runtime.
It's not a bug... it's a feature!
Thanks again you guys are awesome ^_^

This topic is closed to new replies.

Advertisement