• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Nicholas Kong

Many Collision checks in game loop

13 posts in this topic

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
0

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.

0

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.

0

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
0

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

0

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.

0

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
0

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
0

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.

1

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.
0

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.

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

0

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.

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
0

Share this post


Link to post
Share on other sites

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
-1

Share this post


Link to post
Share on other sites

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

0

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  
Followers 0