Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For example, your game has 100 enemies (on different teams) running around and their AI wants to inspect the nearby entities to see which it should attack. What is a fast way to organize those entities so that each enemy does not have to calculate the distance between itself and all other entities?

In short, what is a fast way for an AI entity to answer the question "Who is near me?"

share|improve this question
try using quad trees – Ali.S Aug 17 '11 at 21:14
up vote 7 down vote accepted

You want a spatial index such as quadtree (2D) or octree (3D).

share|improve this answer
@FxIII I'm not sure I follow at all what you are on about. These spatial tree datastructures are specifically designed to answer the poster's question, and are well understood and used. – Will Aug 19 '11 at 6:27

The simplest solution is a grid. Lay a 2D grid over your level. Each cell in the grid maintains a collection of which entities are currently occupying it. As the entities move, take them out of the cell they're leaving and add them to the one they enter.

You can then find nearby entities just by examining the nearby cells. A quadtree refines this by recursively subdividing the grid, but sometimes a flat one is adequate, especially if your level size is fixed and relatively small.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.