Sign in to follow this  
Experiment-626

Collision detection

Recommended Posts

Hi all,

My question is should the engine be designed to detect collisions or should the entities have a collision detection built in? In other words, should the player class detect whether it hits something as well as the missile class or should the engine check for collision and loop through all of the current instances of the player and missile classes?

Share this post


Link to post
Share on other sites
It should be external to the entities. That way you can separate collision detection from your game objects, which will make life easier if you want to experiment with different implementations, etc. Also, parallelization will be much easier (you could have a collision detection thread, or n threads operating on n groups of objects from the list). By grouping the collision data and looping over it in a single bit of code you'll also get much better cache performance. Have a look into "data oriented design" for some ideas there.

Basically I'm of the opinion that it always pays in the long run to be as modular as possible, especially with potentially heavyweight subsystems.

Share this post


Link to post
Share on other sites
[quote name='johnchapman' timestamp='1313558981' post='4850153']
It should be external to the entities. That way you can separate collision detection from your game objects, which will make life easier if you want to experiment with different implementations, etc. Also, parallelization will be much easier (you could have a collision detection thread, or n threads operating on n groups of objects from the list). By grouping the collision data and looping over it in a single bit of code you'll also get much better cache performance. Have a look into "data oriented design" for some ideas there.

Basically I'm of the opinion that it always pays in the long run to be as modular as possible, especially with potentially heavyweight subsystems.
[/quote]

This is correct, the collision detection should be within the engine itself rather than with the object directly.

Share this post


Link to post
Share on other sites
They shouldn't. As said above, being as modular as possible pays a lot. Therefore, the entities shouldn't have a function which asks the engine if they have hit another entity. You should call a function from the engine itself which takes two game entities and determine if they collided. Like that, you separate work that the entity shouldn't be aware of.

An even better implementation would be to no pass directly entities to the engine's function, but objects representing collision shapes. You'll be able to reuse your engine's functionality on other projects like that, because it won't be dependent of your entities.

Share this post


Link to post
Share on other sites
This is game dependent implementation. What I mean is the function's job should be to only check if a collision occurred between two collision shapes. The game should then determine which types are the entities that are colliding. If your entities hold a handle of the collision shape, or you have whatever means to know which shapes is held by which entities, then you can determine of which type is the entities that are colliding together and react accordingly by, for example, sending a message to the entity representing the spaceship to tell it it was destroyed by a bullet.

Share this post


Link to post
Share on other sites
Something like this, where the collision is check by the engine, but the entities themselves handle the collision differently:
[code]
// Assume Hero is our player that collides with things, and Entities are objects the hero can collide with

// Loop through list of Entities that can be collided with
for(int i = 0; i < EntiityList.size(); i++)
{
Entity = EntityList.get(i);
if (Collides(Hero, Entity))
{
// notify hero he collided with Entity, and notify the entity it collided with hero
Hero->NotifyCollision(Entity);
Entity->NotifyCollision(Hero);
}
}
[/code]

Just a simple example of one way to do this.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this