Jump to content

  • Log In with Google      Sign In   
  • Create Account


Many Collision checks in game loop


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 warnexus   Prime Members   -  Reputation: 1400

Like
0Likes
Like

Posted 30 March 2013 - 02:55 PM

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, 30 March 2013 - 02:57 PM.


Sponsor:

#2 moneal2001   Members   -  Reputation: 606

Like
0Likes
Like

Posted 30 March 2013 - 04:30 PM

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.



#3 Ludus   Members   -  Reputation: 970

Like
0Likes
Like

Posted 30 March 2013 - 05:51 PM

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.



#4 warnexus   Prime Members   -  Reputation: 1400

Like
0Likes
Like

Posted 30 March 2013 - 06:02 PM

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, 31 March 2013 - 01:25 PM.


#5 warnexus   Prime Members   -  Reputation: 1400

Like
0Likes
Like

Posted 30 March 2013 - 06:08 PM

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



#6 Paradigm Shifter   Crossbones+   -  Reputation: 5255

Like
0Likes
Like

Posted 30 March 2013 - 06:21 PM

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

#7 warnexus   Prime Members   -  Reputation: 1400

Like
0Likes
Like

Posted 30 March 2013 - 07:04 PM

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, 31 March 2013 - 01:30 PM.


#8 Paradigm Shifter   Crossbones+   -  Reputation: 5255

Like
0Likes
Like

Posted 30 March 2013 - 07:08 PM

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, 30 March 2013 - 07:09 PM.

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

#9 DT....   Members   -  Reputation: 487

Like
1Likes
Like

Posted 31 March 2013 - 01:02 AM

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.



#10 kalle_h   Members   -  Reputation: 1342

Like
0Likes
Like

Posted 31 March 2013 - 07:29 AM

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.

#11 warnexus   Prime Members   -  Reputation: 1400

Like
0Likes
Like

Posted 31 March 2013 - 01:37 PM

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.

Woah thanks for the examples! This definitely opens new opportunities for me to grow  smile.png



#12 warnexus   Prime Members   -  Reputation: 1400

Like
0Likes
Like

Posted 31 March 2013 - 01:47 PM

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.

True. I should put the lasers and ship separately. Definitely would clean up some unnecessary object searching in the list as well as eliminating the instance checks. Lasers won't be collide with other lasers. Thanks for the heads-up.


Edited by warnexus, 31 March 2013 - 01:47 PM.


#13 Ludus   Members   -  Reputation: 970

Like
-1Likes
Like

Posted 31 March 2013 - 11:08 PM

As others have mentioned, there are many ways to optimize collision detection. Which methods you can deploy depend on the mechanics of the game itself. Someone already brought up how lasers don't collide with other lasers. But what about enemies colliding with other enemies? From what I understand, you're creating a top-down space shoot em' up. In many games like this (and in many retro games in general) enemies don't collide with other enemies - they simply overlap and pass through each other. Because of this, enemies don't need to check for collision against other enemies. Also, will your game have power-ups you can collect? It should go without saying that these objects only need to check for collision against the player.

 

Someone mentioned spatial partitioning. For the type of game you're creating this is overkill. The most common method of keeping collision checks to a minimum in a top-down space shoot em' up is to create the enemies' objects during play as the player approaches where they should appear (as opposed to creating all the enemies at once when the level loads). You will also need to destroy the objects as they leave the screen. This is also a common method in retro 2D platformers and it's effects are easy to observe: have you ever wondered why enemies tend disappear when they move off screen, only to reappear at their starting point? This is why. Using this method of creating and destroying objects as needed also serves another purpose - enemies off screen will not be rendered or have their logic updated each frame, thus saving a lot of processing.


Edited by Ludus, 01 April 2013 - 12:38 AM.


#14 warnexus   Prime Members   -  Reputation: 1400

Like
0Likes
Like

Posted 01 April 2013 - 08:31 AM

As others have mentioned, there are many ways to optimize collision detection. Which methods you can deploy depend on the mechanics of the game itself. Someone already brought up how lasers don't collide with other lasers. But what about enemies colliding with other enemies? From what I understand, you're creating a top-down space shoot em' up. In many games like this (and in many retro games in general) enemies don't collide with other enemies - they simply overlap and pass through each other. Because of this, enemies don't need to check for collision against other enemies. Also, will your game have power-ups you can collect? It should go without saying that these objects only need to check for collision against the player.

 

Someone mentioned spatial partitioning. For the type of game you're creating this is overkill. The most common method of keeping collision checks to a minimum in a top-down space shoot em' up is to create the enemies' objects during play as the player approaches where they should appear (as opposed to creating all the enemies at once when the level loads). You will also need to destroy the objects as they leave the screen. This is also a common method in retro 2D platformers and it's effects are easy to observe: have you ever wondered why enemies tend disappear when they move off screen, only to reappear at their starting point? This is why. Using this method of creating and destroying objects as needed also serves another purpose - enemies off screen will not be rendered or have their logic updated each frame, thus saving a lot of processing.

Right. Enemies won't be colliding with other enemies. My game will have power-ups in the future. It has been on my mind. smile.png Thanks for another heads up right now the monsters will always be within the screen. But I can certainly experiment how the monsters should exit the screen like in common arcade shooters.Thanks for the explanation for creating and destroying objects






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS