20

I want to learn how to create good object-oriented (OO) design practice for collision between two objects situation in game development.

Let's say I have a SpaceShip class and a Meteor class. When Meteor collide with the SpaceShip, the SpaceShip will be destroyed.

The question: What class should I put the method to check if there is collision between meteor and spaceship as well as the collision resolution method (destroy the spaceship)? Is it at SpaceShip class or Meteor class? Or maybe I should put at another class, ie. GameArea or GameController class?

Note: for the sake of simplicity, assume the Meteor and SpaceShip is in form of image resource. I'm used to use Java language, but other language is okay too.

null
  • 8,669
  • 16
  • 68
  • 98
  • I'd vote for implementing collision detection in `GameController` (or whatever class is managing your gameplay area), with a `collidedWithObject(GameObject theObject)` method on `Meteor` and `SpaceShip` (and any other object that may collide with things) that gets called to notify the objects of a collision. – aroth Apr 11 '12 at 04:46
  • Thx for your vote. Do you know by any chance a good tutorial or book about object-oriented game design? – null Apr 11 '12 at 07:12

4 Answers4

15

It's more natural to think that collision detection is a responsibility which does not belong to Spaceship or Meteor classes. Especially when this gets complicated with multiple collision possibilities in different directions. If you put this logic in both these classes they will need to have references to lots of other objects that are around them which wouldn't be appropriate.

You could have this on a separate class such as CollisionDetector which keeps track of coordinates of all objects in game space and detect collisions. Collision prevention also appears to be a separate responsibility that should be in a different class in my opinion. You could have a separate class CollisionResolver for this. Depending on the requirements, CollisionDetector could talk to CollisionResolver.

CollisionResolver may need to be able to talk to Spaceships, for purposes such as advising them to change direction, or to command firing missiles towards the Meteor.

CollisionDetector and CollisionResolver could sit within the GameSpace/*GameController*. etc..

This would promote Single Responsibility Principle so each component would be doing only one focussed task.

Rob Kielty
  • 7,958
  • 8
  • 39
  • 51
user1502505
  • 724
  • 1
  • 8
  • 11
3

Collision detection, in my opinion, is not part of an object... it should be defined as something else - some physics manager, etc. That way your objects will be independent on the collision algorithms.

Another thing is that in games usually object consists of several layers (components): Graphics Layer, Physics Layer, Logic Layer. That way physics manager manages only physics component of given objects.

class GameObject 
{
    RenderComponent  m_renderComponent;
    LogicComponent   m_aiComponent;
    PhysicsComponent m_physicsComponent;
};
Mysticial
  • 464,885
  • 45
  • 335
  • 332
fen
  • 9,835
  • 5
  • 34
  • 57
  • So do you mean the collision logic should resides in PhysicsComponent class? – null Apr 11 '12 at 08:27
  • 1
    yes... collision component (for instance some bounding box) can be inside physics component. – fen Apr 11 '12 at 08:56
1

Well, I usually create a (sometimes generic) GameObject class or interface that has a collides method. For example:

template< typename T = int > class GameObject
{
public:
    bool collides(const GameObject& obj);
};

// usage
GameObject<int> my_obj, your_obj;
if(my_obj.collides(your_obj)) { ... };

Another thing I sometimes (but rarely) do is to create a separate GamePhysics class:

template< typename T > class GamePhysics
{
public:
    /* you may make this static or the class a singleton */
    void detect_collision(const T& obj, const T& obj2);
};
Mysticial
  • 464,885
  • 45
  • 335
  • 332
ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153
1

In Java (or any other OO language) I would place a CollisionDetected callback/event in the common ancestor class of all moving objects in your game.

This is a simplified description of a game:

  • In a game, there's usually a game-loop. A game-loop is like a while(true) loop that runs continuously (kind of like the main UI thread of an application) and at every step checks what has changed with the objects, what should be updated and what events should be called (and more...).

  • For responsiveness, this loop should cycle many times a second.

  • Inside this loop, a Physics Engine object should continuously update it's status. This would be an instance object of an independent class. It's this engine that should detect collisions between objects and call the CollisionDetected event on all objects that have collided.

It's an idea, not the definitive solution...

Fengari
  • 453
  • 3
  • 6