My world is made up of 'regions' laid out in a grid and I have some entities that could be overlapping several regions at once. I need some way of querying what entity (if any) is at a given world position.
I had thought I could do it with octrees but I can't really see them working. The first issue is that I can't just have 1 octree for the whole world, because I have to deal with multiplayer and having multiple players that could potentially be very very far away from each other. If I instead do 1 octree per region, that leads to more problems: if an entity is in region 1 and 2, and I query a position inside region 2 which the entity overlaps, region 2 won't have an entry for it.
I could possibly duplicate the entries so that both octrees have the same entry and it wouldn't matter which you queried, but then I run into another problem: I can't add entries to a region which isn't loaded yet.
Another solution might be to query all the neighbor regions' octrees if nothing is found within the octree of the original region. That means even more time taken to find an entity and either require that entities be no larger than a single region or I'd have to keep expanding the search outwards. Overall not a terrible solution, but not one I'm particularly happy with.
Anyone else have some better solutions? Whatever structure is used needs to be buildable in realtime, these entities are dynamically placed.
Handling large entities on a grid
You need to provide more context.
How many grid cells are there? How many of these entities? How many grid cells can a single entity possibly overlap?
How many grid cells are there? How many of these entities? How many grid cells can a single entity possibly overlap?
Sure. By grid cells do you mean within a region? or grid cells of regions? In the first case, its 32x32x256, in the second case, unlimited (its an infinite world). Entity numbers are variable. Like I said, the user is placing them down. I'd probably guess there'd typically be less than 100 in a single region, though if they build a massive vertical structure, there could be a lot more. As for how many cells an entity can overlap, I'm not sure, we haven't made all of them yet. It wouldn't likely much of an issue to limit the max size to that of a region (32x32x256), as that's quite large. I'd say on average, probably won't be more than a few dozen for most entities.
I don't think you want to query the octrees of regions that you haven't loaded.
I could possibly duplicate the entries so that both octrees have the same entry and it wouldn't matter which you queried, but then I run into another problem: I can't add entries to a region which isn't loaded yet.
When you load a region, not earlier, you can collect overlapping entities in nearby regions.and add references in the new region.
You seem to be thinking of the problem inside-out.
Don't build a spatial partitioning structure and then assume that every object will only belong neatly to one subspace. Instead, build a spatial partitioning structure and give each subspace an identifier (or just hold a pointer to it, more efficient). Then, each entity you have maintains a list of which partition subspaces it overlaps. If the entity moves, rebuild the membership list. If the world is mutated sufficiently, rebuild the spatial partitioning structure.
Octrees should be sufficient for this although you might have better results in non-uniform worlds with a kd-tree instead.
Don't build a spatial partitioning structure and then assume that every object will only belong neatly to one subspace. Instead, build a spatial partitioning structure and give each subspace an identifier (or just hold a pointer to it, more efficient). Then, each entity you have maintains a list of which partition subspaces it overlaps. If the entity moves, rebuild the membership list. If the world is mutated sufficiently, rebuild the spatial partitioning structure.
Octrees should be sufficient for this although you might have better results in non-uniform worlds with a kd-tree instead.
So it's looking like I need to set it up like this:
* When a region loads, load in any saved entities and add them to the octree.
* Query the 8 neighbor regions. For any that are loaded:
- Find any entities in the current region that overlap the neighbor region (loop all entities and do a check?) and add them to the neighbor's octree
- Find any entities in the neighbor region that overlap the current region and add them to current region's octree
That's going to be a lot of looping over all entities. Should I be concerned about this? Is there a better way? I know the answer is probably going to be "test and find out" but I figured I'd ask before I waste time implementing it.
* When a region loads, load in any saved entities and add them to the octree.
* Query the 8 neighbor regions. For any that are loaded:
- Find any entities in the current region that overlap the neighbor region (loop all entities and do a check?) and add them to the neighbor's octree
- Find any entities in the neighbor region that overlap the current region and add them to current region's octree
That's going to be a lot of looping over all entities. Should I be concerned about this? Is there a better way? I know the answer is probably going to be "test and find out" but I figured I'd ask before I waste time implementing it.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement