Jump to content
  • Advertisement
Sign in to follow this  
Nicholas Kong

Many Collision checks in game loop

This topic is 2057 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);
 
} 
 
} 
}
}
Edited by warnexus

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Edited by warnexus

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Edited by warnexus

Share this post


Link to post
Share on other sites

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.

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!