c++ - Collision Detection and Time Complexity: How do I make checking for collisions easier? -
i'm trying write program handles detection of various objects. objects have origin, width, height, , velocity. there way set data structure/algorithm every object isn't checking every other object? some sample code of problem i'm trying avoid: for (int = 0; < ballcount; i++) { (int j = + 1; j < ballcount; j++) { if (balls[i].colliding(balls[j])) { balls[i].resolvecollision(balls[j]); } } } as mentioned other answer(s), can use quadtree structure make collision detection faster. i recommend geos open-source c++ library, has quadtree implementation. here docs quadtree class . so pseudo code this: quadtree quadtree; // create , populate quadtree. // change whenever balls move. // here's intersection loop: (int i=0; i<ballcount; ++i) { envelope envelope = ...; // bounds (envelope) of ball std::vector<void*> possiblyintersectingballs; quadtree.query(envelope, possiblyintersectingballs); // loop on members of possiblyintersectingballs check // if int...