Many Collision checks in game loop

Started by
12 comments, last by Nicholas Kong 11 years ago
If I add more projectiles for my character or for present/future monsters in my game, eventually the collision checks will soon grown in my game loop. There will definitely be far too many if statements checking instances of projectile and skill which I know will be difficult to manage or inefficient code. Right now it is okay since I only added a laser for my ship character and a skill for a ghost. But I thought I think long term when it comes to a large codebase for a game. smile.png
Here's what I mean:

while(!isLooping)
{
 
 
if(isRunning){
// collision detection checks of the game
for(GameComponent component: gameObjects){
if(component instanceof Laser)
{
((Laser) component).checkMonsterCollision(gameObjects);
 
}
if(component instanceof GhostSkill)
{
((GhostSkill) component).checkShipCollision(gameObjects,shipLifeTitle,shipHealthSystem);
 
} 
 
} 
}
}
Advertisement

does you collision check also perform you damage. it looks like it from what i am seeing. if it does you could possibly separate the collision from the damage. you could have a basic collision call for projectile game objects and if it came back true call the proper damage for the projectile type.


for(GameComponent component: gameObjects)
{
    if(component instanceof Projectile)
    {
        //return object the projectile collided with
        GameComponent object = ((Projectile) component).checkCollision(gameObjects)
         
        if( object != null)
        {
            // each player projectile and enemy skill would hold their own damage data or have a DoDamage() override
            switch(component.Type)
               case "Player":
                  component.DoDamage(object);
                  break;
               case"Enemy":
                   component.DoDamage(object, shipLifeTitle, shipHealthSystem);
                   break;     
        }
    }
}


this would be a simplified system where there would only be one object that a projectile could hit and do damage to at one time. but could be modified to allow a list or array of objects that could be damaged at once.

i am somewhat of a beginner so there may be better ways of handling it, but that's how i would do it.

All of your game objects should be stored in a dynamic array. Furthermore, each entity should have a logic function that is called each frame. Using the dynamic array, you will call the logic function of each object during the loop function. The logic function of an object will check for collision against all other objects and update movement.

does you collision check also perform you damage. it looks like it from what i am seeing. if it does you could possibly separate the collision from the damage. you could have a basic collision call for projectile game objects and if it came back true call the proper damage for the projectile type.


for(GameComponent component: gameObjects)
{
    if(component instanceof Projectile)
    {
        //return object the projectile collided with
        GameComponent object = ((Projectile) component).checkCollision(gameObjects)
         
        if( object != null)
        {
            // each player projectile and enemy skill would hold their own damage data or have a DoDamage() override
            switch(component.Type)
               case "Player":
                  component.DoDamage(object);
                  break;
               case"Enemy":
                   component.DoDamage(object, shipLifeTitle, shipHealthSystem);
                   break;     
        }
    }
}


this would be a simplified system where there would only be one object that a projectile could hit and do damage to at one time. but could be modified to allow a list or array of objects that could be damaged at once.

i am somewhat of a beginner so there may be better ways of handling it, but that's how i would do it.

Oh I see where you are getting! Yes, the ship damage calculation is within the collision detection code. You got a point there. I should separate damage calculation and collision separately.

All of your game objects should be stored in a dynamic array. Furthermore, each entity should have a logic function that is called each frame. Using the dynamic array, you will call the logic function of each object during the loop function. The logic function of an object will check for collision against all other objects and update movement.

It is implemented like that smile.png

Hmm, I'm not a fan of that method. You'll probably check for collisions twice for each object pair then when you don't need to.

Typically you place objects which can collide into a list and only check collisions against objects which are further along in the list, then call a callback method for both objects to resolve the collision or do damage etc.

You may also want to place objects in collision groups (e.g. bullets can't collide with each other, etc.) which can be implemented as separate lists or by having a bitfield which stores which objects are able to collide with each other.

Remember to only do expensive collision with objects where one or both are actually moving as well.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Hmm, I'm not a fan of that method. You'll probably check for collisions twice for each object pair then when you don't need to.

Typically you place objects which can collide into a list and only check collisions against objects which are further along in the list, then call a callback method for both objects to resolve the collision or do damage etc.

You may also want to place objects in collision groups (e.g. bullets can't collide with each other, etc.) which can be implemented as separate lists or by having a bitfield which stores which objects are able to collide with each other.

Remember to only do expensive collision with objects where one or both are actually moving as well.

the collision detection methods I posted up only happens when the projectiles are in the ArrayList named gameObjects which is when the projectiles are drawn on screen.

Something I forgot to mention the laser is coming from the ship towards the monsters and the ghost skill is coming from the enemy towards the ship. But I see where you are coming from. I going to try to restructure the code better. Thanks for the heads-up.

Righto, I was commenting on Ludus' post which suggested each object check collision against all others, which is unnecessary since if A collides with B then B collides with A as well and doesn't need to be checked.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Trying researching spatial partitioning and bounding volumes.

Examples would be:

Spatial partiions: Sort-and-Sweep, 2D grid, quad-trees, binary space partitioning.

Bounding volumes: Axis-Aligned Bounding Box (AABB), spheres.

Also, keep your brute-force methods around. You'll need 'em later on to verify correctness of "all-of-the-above."

HTH.

Put lasers and ships to separate list. No need for checking type of object at inner loop. Can lasers collide with other lasers? If not then you are checking way too many units against each other.

This topic is closed to new replies.

Advertisement