2

I'm working on Python, but I suppose that doesn't affect the question itself.

I'm working on a game, and I need to store entities, each of which has an [x,y,z] in the world. I need to be able to run a "All entities within X euclidean distance of point Y".

These entities will be moving fairly often.

What would be the most efficient way to store the entities to make this as fast as possible?

Salis
  • 433
  • 1
  • 8
  • 15

2 Answers2

2

As an alternative to what has been suggested already, if you don't need an exact distance, you could also use spatial hashing, which is quite easy to implement.

In summary, you have to think of your world as a grid where each cell in the grid would correspond to one bucket in the hash table. Since your entities are moving often, on each new frame you could clear and reconstruct the whole table and put the entities to their corresponding bucket depending on their position. Then, for any given entity you could just check the cells nearby and get the entities lists.

Pin
  • 3,746
  • 4
  • 26
  • 33
1

You can use a kd-tree (link has photo and code and examples) or an octree (this link is a C++ class template which you can use). Real-usage can be seen in this open-source game engine

Mihai Maruseac
  • 20,967
  • 7
  • 57
  • 109
  • Can you elaborate at all or link me to something that explains how to apply it here? I think I understand the structure (In principle you divide the entities positions into grids and then eliminate all but nearby grids? I'm not sure exactly), but how would I actually apply the data to the structure? – Salis Jun 21 '13 at 08:10
  • There's a link to the wiki page for the octree structure and there are links for implementation there (now the link is good, previously I pasted a wrong one). – Mihai Maruseac Jun 21 '13 at 08:12