Space partitioning when everything is moving
Posted
by
Roy T.
on Game Development
See other posts from Game Development
or by Roy T.
Published on 2012-11-23T11:08:51Z
Indexed on
2012/11/23
11:16 UTC
Read the original article
Hit count: 431
java
|space-partitioning
Background
Together with a friend I'm working on a 2D game that is set in space. To make it as immersive and interactive as possible we want there to be thousands of objects freely floating around, some clustered together, others adrift in empty space.
Challenge
To unburden the rendering and physics engine we need to implement some sort of spatial partitioning. There are two challenges we have to overcome. The first challenge is that everything is moving so reconstructing/updating the data structure has to be extremely cheap since it will have to be done every frame. The second challenge is the distribution of objects, as said before there might be clusters of objects together and vast bits of empty space and to make it even worse there is no boundary to space.
Existing technologies
I've looked at existing techniques like BSP-Trees, QuadTrees, kd-Trees and even R-Trees but as far as I can tell these data structures aren't a perfect fit since updating a lot of objects that have moved to other cells is relatively expensive.
What I've tried
I made the decision that I need a data structure that is more geared toward rapid insertion/update than on giving back the least amount of possible hits given a query. For that purpose I made the cells implicit so each object, given it's position, can calculate in which cell(s) it should be. Then I use a HashMap
that maps cell-coordinates to an ArrayList
(the contents of the cell). This works fairly well since there is no memory lost on 'empty' cells and its easy to calculate which cells to inspect. However creating all those ArrayList
s (worst case N) is expensive and so is growing the HashMap
a lot of times (although that is slightly mitigated by giving it a large initial capacity).
Problem
OK so this works but still isn't very fast. Now I can try to micro-optimize the JAVA code. However I'm not expecting too much of that since the profiler tells me that most time is spent in creating all those objects that I use to store the cells. I'm hoping that there are some other tricks/algorithms out there that make this a lot faster so here is what my ideal data structure looks like:
- The number one priority is fast updating/reconstructing of the entire data structure
- Its less important to finely divide the objects into equally sized bins, we can draw a few extra objects and do a few extra collision checks if that means that updating is a little bit faster
- Memory is not really important (PC game)
© Game Development or respective owner