In my game I have a base class "Object", from which other objects are derived (Asteroid, Player, Bullet). All game objects are stored in a std::list<Object *>. Every frame, something like this happens:
Code:
// Not really how it's done, but just to give an impression
for(i = 0; i < end; i++) {
for(j = i + 1; j < end; j++) {
object[i]->collisionTest(object[j]);
}
}
My problem is that Asteroids shouldn't collide with eachother, neither should Bullet with Player or Bullet with Bullet. So a couple of useless CollisionTest()'s are run. But since the objects are stored as Object, it's impossible to know wether an Object is really an Asteroid, or Player etc (well actually it is possible to know, as shown in the code below, but I don't think that's a nice way of solving it). I was wondering how you would take care of this problem. I've thought of something like this:
Code:
// Inside CollisionTest(Object *obj) of a Bullet object
if(obj->getType() == ASTEROID) {
Asteroid *foo = reinterpret_cast<Asteroid *>(obj);
// Call Asteroid specific functions, like foo->explode()
}
else {
// Other object is a Player or Bullet, so don't do anything
}
Where the getType() returns a constant that every derived class would set for itself. But I think this is really ugly and I was hoping someone knows a better approach.