Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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
1  
add comment

2 Answers

up vote 6 down vote accepted

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

share|improve this answer
1  
@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
add comment

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
add comment

Your Answer

 
discard

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.