0

I'm reading some about QuadTrees and how I can use them for a better collision detection. But I doesn't understand why QuadTrees brings me more performance. I'm trying this on a 2D Game.

If I'm using a QuadTree I have to clear on every frame the QuadTree and insert all my Objects and then I loop through this to get all possible collision objects. And then loop through this to get my collision object i need. Why this is better then loop through all my Objects instead? For the QuadTree I need O(n logn) if im right. But the loop through all my objects is O(n)

Greetz

R3Tech
  • 743
  • 2
  • 9
  • 22
  • While this is a very cool, and fascinating question, I don't think it's quite on topic for StackOverflow. You may have more luck on http://gamedev.stackexchange.com/ And for a short answer, QuadTrees can help limit the number of objects you need to check for collision detection, thus improving performance. Collision detection is expensive if you have to check it against every single object in the scene. http://www.mikechambers.com/blog/2011/03/21/javascript-quadtree-implementation/ – Brandon Anzaldi Apr 25 '16 at 19:59

1 Answers1

1

Comparing each Object with all others is O(n^2), because you need to go through all object (O(n)), and for each object you need to check (almost) all others, so that's another O(n), resulting in O(n^2).

This is worse than O(n log n).

Comparing every object to every other may still be cheaper if you have only 5 objects or so, because you avoid the overhead of creating a quadtree.

Also, you may be able to reuse the quadtree if not all of the objects change their positions.

TilmannZ
  • 1,784
  • 11
  • 18
  • So if I only need to check my player to all objects, working without a quadtree should be better? Thanks you – R3Tech Apr 26 '16 at 09:05
  • If only one object move, it doesn't even need to be in the quadtree. Whether a quadtree is better depends on the number of objects. Best try it out. – TilmannZ Apr 26 '16 at 10:38