Optimization of Large-Scale, Real-Time Simulations by Spatial Hashing
Optimization of Large-Scale, Real-Time Simulations by Spatial Hashing
Bounding Volumes
All complex models we consider shall be surrounded
by a simple bounding volume to greatly reduce computation
required for both hashing and collision. Bounding volumes
may serve as either:
Collision Detection
Optimized collision proceeds by only colliding objects
that hash to the same bucket. In this manner, objects that can
Figure 5 – Finding the cells traversed by small, fast moving possibly collide (those in the same cell) are quickly found
objects is similar to rastering a line. with an O(1) cell query. There are 2 phases to collision:
The algorithm works as follows, where A and B are • Hash Phase: all mobile objects are hashed (static
Grid Cells (x,z), A is the lower valued cell (i.e A.x < B.x and objects are only hashed once at startup)
A.z < B.z), and A != B. • Collision Phase: for every hash bucket, collide the
objects in that bucket
for(i=A.x; i<(B.x–A.x); i++)
for(j=A.x; j<(B.z–A.z); j++) As noted above the hash function automatically
{ accounts for objects that cross cell boundaries. Such objects
Add object to the hash bucket… will be referenced in multiple hash buckets. It is possible
… associated with grid cell(i,j) that further optimization could be obtained by designating a
} third class of “seldom moving” objects. Such objects that
only rarely move might only be hashed after a movement.
This may seem a sub-optimal or “brute force” approach Note that static objects need only be hashed once at
but consider: application startup. A hashing based terrain traversal method
is described below in the simulations results section. Exact
• Cells are significantly larger than objects collision formulas are bounding volume dependent and will
• Time between frames is fairly small in real-time vary across applications. Note also that collision over time is
applications optional and many applications may not require it. Further
discussion of spatial hashing with regard to mobile object
From this we can conclude that an object will almost collision can be found in [3] and [7]. Discussion of the
always move no more than one cell from its current position mathematics of mobile object collision over time can be
during the frame. Clearly a complex algorithm for traversing found in [2] and [14].
cells is not needed, and may actually slow hash computation
somewhat. In almost all situations cell traversal over the
course of the frame will be similar to one of the three cases
Picking
Picking is a type of collision detection where user input
presented in figure 6. Thus our simple nested “for” loop is
(usually a mouse-controlled pointer) selects objects in the
adequate. The above discussion suggests at least two types
scene. Every object in the scene can possibly be picked but
of hashing based on object behavior.
by using the hash table we can quickly discount a majority of
objects. Picked objects can be found by:
• small, fast collision objects: bullets for example,
will be line traced through the grid
• projecting a ray through the screen where the user
• normal collision objects: simple nested for loop to clicked
“draw a box”
• in a 3D grid, tracing the ray through the grid
• in a 2D grid intersecting the ray with grid plane,
then hashing the intersection point
• objects in the hash buckets corresponding to
intersected grid cells are candidates for picking
Additionally, allowing the user to “click and drag” a
box on screen (similar to figure 7) is a common method of
allowing selection of objects and can efficiently implemented
by: 1) hashing the max point of the selection, 2) hashing the
min point of the selection, and 3) finding the cells between
them as described previously.
• Code: C++ The test machine for the simulation was a desktop PC:
• Graphics: OpenGL Intel Pentium4 3.2GHz, 1 Gig of RAM, and Radeon 9800
• Windows Framework: nehe.gamedev.net OpenGL Pro GPU. Figures 13 through 15 display the results of the
Application Framework simulation. The results are presented in frames-per-second
• Math Libraries: DirectX 9.0c (FPS) for each phase of the algorithm. “60+” denotes that
• Compiler: Microsoft Visual Studio.net 2003 phase can be computed over 60 times per second. “<1”
Version 7.1.3088 denotes the phase was computed less than once per second.
“Objects” denotes the number of mobile objects in the
The simulation consisted of mobile objects (multi- simulation. “Cell size” denotes the cell size in world units of
colored spheres) that randomly roam over a fixed, randomly each grid cell. The percentage value denotes the size of the
generated terrain (triangle mesh). A screenshot of the cell in relation to the entire domain space. Since the domain
simulation is shown in Figure 11. The hash table was space was 400 units across, a cell size of 100 is equivalent to
implemented using a two-dimensional vector (C++ vector 25% of the domain space. Objects had a radius of 1.0. “Un-
class). optimized” denotes measurement for the application with no
optimzation whatsoever. This is provided to illustrate the
magnitude of speedup from the hashing and to allow other
methods to be compared with these results. The highlighted
cells indicate a computation time too slow for real-time
applications (under 20 FPS). The slowest phase was AI
calculation and the fastest phase was visibility determination.
Un- Cell Size Cell Size Cell Size Cell Size Cell Size
Optimized 5 10 25 50 100
Objects 1.25% 2.50% 6.25% 12.50% 25%
500 60+ 60+ 60+ 60+ 60+ 60+
1000 60+ 60+ 60+ 60+ 60+ 60+
2500 25 60+ 60+ 60+ 60+ 60+
5000 <1 60+ 60+ 60+ 60+ 60+
10000 <1 60+ 60+ 60+ 60+ 2
20000 <1 60+ 60+ 50 5 <1
50000 <1 29 23 <1 <1 <1
Figure 13 – FPS for the collision phase where collision, response, and terrain traversal are computed. With a cell size of 5, collision
was computed for 20,000 objects over 60 times per second. Collision for 50,000 objects was computed around 30 times per second.
Without optimization less than 3000 objects could be supported in real-time.
Un- Cell Size Cell Size Cell Size Cell Size Cell Size
Optimized 5 10 25 50 100
Objects 1.25% 2.50% 6.25% 12.50% 25%
500 60+ 60+ 60+ 60+ 60+ 60+
1000 60+ 60+ 60+ 60+ 60+ 60+
2500 48 60+ 60+ 60+ 60+ 60+
5000 16 60+ 60+ 60+ 60+ 60+
10000 <1 60+ 60+ 60+ 60+ 60+
20000 <1 60+ 60+ 60+ 60 49
50000 <1 60+ 60+ 60+ 52 40
Figure 14 – FPS for the visibility determination phase where frustum culling and OpenGL draw calls for all visible objects are made.
Culling and draw calls could be performed for 50,000 objects over 60 times per second. Less than 5000 objects could be supported at
real-time frame rates without optimization.
Un- Cell Size Cell Size Cell Size Cell Size Cell Size
Optimized 5 10 25 50 100
Objects 1.25% 2.50% 6.25% 12.50% 25%
500 60+ 60+ 60+ 60+ 60+ 60+
1000 60+ 60+ 60+ 60+ 60+ 60+
2500 35 60+ 60+ 60+ 60+ 60+
5000 <1 60+ 60+ 60+ 60+ 60+
10000 <1 60+ 60+ 24 33 10
20000 <1 23 28 12 <1 <1
50000 <1 8 <1 <1 <1 <!
Figure 15 – FPS for the AI phase where a proximity based AI routine for all objects is computed. AI behaved much like collision but
was slightly slower. At a cell size of 5, computation for 20,000 objects could be performed approximately 50 times per second. Less
than 3000 objects could be supported in real-time without optimization.
10. References