Dmitry K.
Dmitry K.

Reputation: 3198

Better way to check game objects collisions

guys! I have a problems with processing collision of game objects (with other sizes). My algorithm is:

for (Array<GameObject*>::iterator it = objects.begin(); it != objects.end(); it++)
{
   RECT objRect = (*it)->GetBoundingBox();
   for (Array<GameObject*>::iterator it2 = object.begin(); it2 != object.end(); it2++)
   {
      RECT objRect2 = (*it2)->GetBoundingBox();
      IntersectRectangles(objRect, objRect2);
   }
}

Yes, that is works fine. But it works very slow. I have a idea (check only nearby objects to object), but that means more and more iterations. Maybe the better way is exist?

Upvotes: 2

Views: 856

Answers (2)

Keen
Keen

Reputation: 26

You can divide game world into some big zone, and check collssion of game ojcects in one zone just. This is a basic method.

Upvotes: 1

Neil Kirk
Neil Kirk

Reputation: 21773

Divide your game world into cells, which contain a list of pointers to objects that reside within them. Make sure to update your cells when objects move between them and the size of the cell is larger than your biggest object. Only check collisions with objects in the same cell and adjacent cells. You can experiment with cell sizes and refine with more optimizations later on.

Upvotes: 3

Related Questions