Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: The right way to detect collisions of many objects  (Read 1394 times)

0 Members and 2 Guests are viewing this topic.

bebewr

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
The right way to detect collisions of many objects
« on: June 08, 2016, 02:11:26 pm »
Hello guys, the answer to my question might be obvious to you, but i have some doubts about the right way I should detect collisions in my program.
So far, I've created three classes: Player, Projectile and Mob.
I move Player object and shoot with Projectile objects,putting them into std::vector projectilesvector. Mobs spawn randomly and they are saved in std::vector mobsvector. In every game loop I check all Projectiles object's and Mob's objects, if they intersect:

for( int i = 0; i < projectilesvector.size(); i++ )
{
        for( int j = 0; j < mobsvector.size(); j++ )
       {
        //   if ( projectilesvector[i] intersects mobsvector[j] ) collision detected
       }
}
 

For example, with 500 projectiles and 100 mobs I have to make it through 50000 loops, which takes substantial amount of time.
I wonder if there is a faster way to check collisions, without nested loops.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: The right way to detect collisions of many objects
« Reply #1 on: June 08, 2016, 02:23:06 pm »
What you need is space partitioning, for example a quad-tree.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: The right way to detect collisions of many objects
« Reply #2 on: June 08, 2016, 02:24:29 pm »
One common solution is to use a quadtree. There are many resources out there about this.

Does your game have 500 bullets at once? And did you actually notice performance issues?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

bebewr

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: The right way to detect collisions of many objects
« Reply #3 on: June 08, 2016, 04:45:58 pm »
What you need is space partitioning, for example a quad-tree.

Thanks for feedback, this is what I needed

Does your game have 500 bullets at once? And did you actually notice performance issues?

I'm trying to write a primitive engine for future projects, so I've been testing different amounts of objects. I couldn't see the perfomance issues by my eyes, but keeping in mind, that the final program would be much bigger,  the time needed to process one frame was just too big.