Collision Design

This is a discussion on Collision Design within the Game Programming forums, part of the General Programming Boards category; In my game I have a base class "Object", from which other objects are derived (Asteroid, Player, Bullet). All game ...

  1. #1
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89

    Collision Design

    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.

  2. #2
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070
    I do almost the exact same thing in my engine. I can't think of a better way to do it either.

    My thinking is that although it's ugly, it still works.
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

  3. #3
    glo
    glo is offline
    Registered User
    Join Date
    May 2006
    Posts
    169
    Why is this approach ugly? is it the fact it adds a new member to struct?
    Thanks.

  4. #4
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070
    No, it's just the excessive use of if/else statements that have to be used to determine what to do with each type of object.
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    You can use separate lists of objects and group them by type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some collision handling fun
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 04-13-2008, 08:45 AM
  2. Collision Detection Problems
    By Dark_Phoenix in forum Game Programming
    Replies: 1
    Last Post: 12-17-2006, 02:25 PM
  3. Collision Detection
    By Grantyt3 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2005, 03:21 PM
  4. collision detection
    By DavidP in forum Game Programming
    Replies: 2
    Last Post: 05-11-2002, 01:31 PM
  5. Replies: 4
    Last Post: 05-03-2002, 09:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21