SlideShare a Scribd company logo
1 of 57
Download to read offline
Physics for Game Programmers:
Spatial Data Structures
Gino van den Bergen
gino@dtecta.com
“I feel like a million tonight …
…But one at a time.”
Mae West
Collision Queries
● Find pairs of objects that are colliding
now, …
● … or will collide over the next frame if no
counter action is taken.
● Compute data for response.
Continuous Collision Queries
● Fast moving objects, such as bullets and
photons (for visibility queries), need to be
swept in order to catch all potential hits.
● Querying a scene by casting rays (or
shapes) is an important ability.
Collision Objects
● Static environment (buildings, terrain) is
typically modeled using polygon meshes.
● Moving objects (player, NPCs, vehicles,
projectiles) are typically convex shapes.
● We focus on convex-mesh collisions.
Mesh-mesh is hardly ever necessary.
Convex Shapes
Three Phases
● Broad phase: Determine all pairs of
independently moving objects that
potentially collide.
● Mid phase: Determine potentially
colliding primitives in complex objects.
● Narrow phase: Determine contact
between convex primitives.
Mid Phase
● Complex objects such as triangle meshes
may be composed of lots of primitives.
● Testing all primitives will take too long,
especially if only a few may be colliding.
● How to speed things up? Or rather,
achieve a more output-sensitive solution?
Spatial Coherence
● Expresses the degree in which a set of
primitives can be ordered based on spatial
location.
● Primitives occupy only a small portion of
the total space.
● A location in space is associated with a
limited number of primitives.
Which Is More Coherent?
Divide & Conquer
Capture spatial coherence by using some
divide & conquer scheme:
● Space Partitioning: subdivide space into
convex cells.
● Model Partitioning: subdivide a set of
primitives into subsets and maintain a
bounding volume per subset.
Uniform Grid
● Subdivide a volume into uniform rectangular
cells (voxels).
● Cells need not keep coordinates of their position.
● Position (x, y, z) goes into cell
where ex, ey, ez are the cell dimensions.
      zyx ezeyexkji /,/,/),,( 
Uniform Grid (cont’d)
Two alternative strategies:
● Add a primitive to all cells that overlap the prim’s
bounding box. Overlapping boxes must occupy the
same cell.
● Add a primitive to the cell that contains the
center of its box. Neighboring cells need to be
visited for overlapping boxes, but cells contain
fewer objects.
Uniform Grid (cont’d)
● Grids work well for large number of
primitives of roughly equal size and density
(e.g. cloth, fluids).
● For these cases, grids have O(1) memory
and query overhead.
Spatial Hashing
● Same as uniform grid except that the
space is unbounded.
● Cell ID (i, j, k) is hashed to a bucket in a
hash table.
● Neighboring cells can still be visited.
Simply compute hashes for (i±1, j±1,
k±1).
Spatial Hashing (cont’d)
● As for grids, spatial hashing only works
well for primitives of roughly equal size and
density.
● Remote cells are hashed to the same
bucket, so exploitation of spatial coherence
may not be that great.
Binary Space Partitioning
● A BSP is a hierarchical subdivision of
space into convex cells by (hyper)planes.
k-D Trees
● A k-D trees is a BSP with axis-aligned
partitioning planes.
BSP Trees versus k-D Trees
● k-D trees have smaller memory footprints
and faster traversal times per node.
● BSP Trees need fewer nodes to “carve
out” a proper volume representation.
● The volume enclosed by a polygon mesh
can be accurately represented by a BSP.
Point Queries on a BSP
while (!node->isLeaf) {
node = node->plane.above(p) ?
node->posChild :
node->negChild;
}
return node->inside;
Volume Queries on a BSP
● Fat objects may overlap multiple cells, so
multiple root paths need to be traversed.
● Shrink the query object to a point and
dilate the environment by adding the
negate of the query object [Quake2].
● The dilated environment is better known
as Configuration Space Obstacle (CSO).
Exact CSO for a Disk
Approximate CSO for a Disk
Add Auxiliary Bevel Planes
Dynamic Plane-shift BSP Trees
● What if we have query objects with
different sizes?
● Keep the original BSP and offset the
planes while traversing [Melax, MDK2].
● Since obstacles occur both in the + and -
half-space, we offset in both directions.
Bounding Volumes
● Should fit the model as tightly as
possible.
● Overlap tests between volumes should be
cheap.
● Should use a small amount of memory.
● Cost of computing the best-fit bounding
volume should be low.
Bounding Volumes (cont’d)
Good bounding volume types are:
● Spheres
● Axis-aligned bounding boxes (AABBs)
● Discrete-orientation polytopes (k-DOPs)
● Oriented bounding boxes (OBBs)
Bounding Volume Types
Why AABBs?
● Offer a fair trade-off between storage
space and fit.
● Overlap tests are fast.
● Allow for fast construction and update of
bounding volumes.
● Storage requirements can be further
reduced through compression.
Binary AABB Tree
● Each internal node has two children.
● Each leaf node contains one primitive.
● For N primitives we have N leaf nodes and
N – 1 internal nodes (2N – 1 AABBs in
total).
● Best trees need not be, and usually are
not, height-balanced.
Volume Queries on an AABB Tree
● Compute the volume’s AABB in the AABB
tree’s local coordinate system.
● Recursively visit all nodes whose AABBs
overlap the volume’s AABB.
● Test each visited leaf’s primitive against
the query volume.
Sequential Tree Traversal
int i = 0;
while (i != nodes.size()) {
if (overlap(queryBox, nodes[i].aabb)) {
if (nodes[i].isLeaf)
primTest(nodes[i]);
++i;
} else i += nodes[i].skip;
}
Memory Considerations
● Nodes are stored in a single array.
● Each left child is stored immediately to
the right of its parent.
● The skip field stores the number nodes in
the subtree (the number of nodes to skip if
the overlap test is negative).
Ray Cast on an AABB Tree
● Returns the primitive that is stabbed by
the ray earliest.
● Requires a line-segment-versus-box test.
● Each stabbed primitive shortens the ray.
● Traverse the AABB tree testing the AABB
closest to the ray’s source first.
Line-segment vs. Box Test
● Use a SAT testing the box’s three
principal axes and the three cross products
of principal axes and the line segment.
● If the line segment is almost parallel to an
axis then the cross product is close to zero
and the SAT may return false negatives!!!
Line-segment Box Test (cont’d)
● The cross product rejection test has the form
| rz * cy – ry * cz | > |rz| * ey+ |ry| * ez,
where r is the ray direction, and c and e are resp.
the center and extent of the AABB.
● The rounding noise in the lhs can get greater
than the rhs, resulting in a false negative!!
Line-segment Box Test (cont’d)
● The lhs suffers from cancellation, if c is at some
distance from the origin.
● The solution is to add an upper bound for the
rounding noise to the rhs, which is
max(|rz * cy|, |ry * cz|)ε,
Here, ε is the machine epsilon of the number type.
Shape Casting on an AABB Tree
● Similar to ray casting but now we need to
find the primitive that is hit by a convex
shape.
● Perform a ray cast on the Minkowski sum
of the node’s AABB and the shape’s box:
[a, b] – [c, d] = [a - d, b - c]
AABB Tree Construction
● AABB Trees are typically constructed top
down.
● Bottom-up construction may yield good
trees, but takes a lot of processing.
● Start with a set of AABBs of the
primitives.
Top-down Construction
● Compute the AABB of the set of AABBs. This is
the root node’s volume.
● Split the set using a plane. The plane is chosen
according to some heuristic.
● AABBs that straddle the plane are added to the
dominant side. (AABBs of the two sets may
overlap.)
● Repeat until all sets contain one AABB.
Median Split Heuristic
● Compute the bounding
box of the set of AABB
center points.
● Choose the plane that
splits the box in half
along the longest axis.
Median Split Heuristic No Good
● Median splits may not carve out empty
space really well.
Better Splitting Heuristic
● Off-center splits may do better.
Surface Area Heuristic
● Find the splitting plane that minimizes
SA(AABBleft) * Nleft + SA(AABBright) * Nright
● Here, SA(AABB) is the surface area of the
box, and N is the number of primitives.
Surface Area Heuristic (cont’d)
● Determining the best SAH splitting plane
can be computationally expensive.
● Sufficiently-good splitting planes can be
found quickly by using a binning technique:
● Evaluate a pre-defined set of splitting
planes, and pick the best one.
Surface Area Heuristic (cont’d)
● Group primitives per
cell and compute the
AABB of the group.
● Compute AABBleft and
AABBright for each
splitting plane from
these cell AABBs, and
compute their SA.
Top-down Construction Demo
Updating AABB Trees
● AABB trees can be updated rather than
reconstructed for deformable meshes.
● First recompute the AABBs of the leaves.
● Work your way back to the root:
A parent’s box is the AABB of the children’s boxes.
● For extreme deformations reconstruction is
better and may not be that slow [Wald].
Compressed AABB Trees [Gomez]
● Only 6 of the 12 faces of the child AABBs
differ from the parent’s faces.
● Pack the children paired, and only store
the coordinates of these new inner faces.
● For each of the 6 inner faces store a bit to
denote which child it belongs to.
Compressed AABB Trees [Gomez]
● Inner faces are
shared among
children.
● Sharing need not
be even.
Compressed AABB Trees [Gomez]
● Encode each of the 6 inner face coordinates by a
single byte.
● The byte represents the coordinate as a fraction
of the parent’s AABB.
● Lower bounds are rounded down. Upper bounds
are rounded up.
● This reduces the memory footprint from 56 to 16
bytes per primitive.
Boxtree [Zachmann]
● Since the set of primitives is split along
the longest axis, only one of each child’s
faces will differ significantly from its
parent’s.
This face
differs
significantly
from the box’s
parent.
This face is close
to the box’s parent
Boxtree [Zachmann]
● Store only the coordinate for the inner
faces (similar to k-d tree.)
● The other coordinates are inherited from
the parent box.
Only these
coordinates
are stored.
Boxtree [Zachmann]
The Boxtree (aka Bounding-Interval Hierarchy)
has a few benefits over traditional AABB trees:
● Smaller memory footprint.
● Slightly faster build times.
● Faster traversal times due to the fact that the
number of axes in the SAT can be further reduced.
● Empty space is captured less greedily, so YMMV!
References
● Stefan Gottschalk e.a. OBBTree: A Hierarchical
Structure for Rapid Interference Detection. Proc
SIGGRAPH, 1996
● Gino van den Bergen. Efficient Collision
Detection of Complex Deformable Models using
AABB Trees. JGT, Vol. 2, No. 4, 1997
● Stan Melax. Dynamic Plane Shifting BSP
Traversal. Proc. Graphics Interface, 2000
References
● Ingo Wald. On Fast Construction of SAH Based
Bounding Volume Hierarchies. Proc. Eurographics,
2007
● Miguel Gomez. Compressed Axis-Aligned
Bounding Box Trees. Game Programming Gems 2,
2001
● Gabriel Zachmann. Minimal Hierarchical Collision
Detection. Proc. VRST, 2002.
Thank You!
My pursuits can be traced on:
● Web: http://www.dtecta.com
● Twitter: @dtecta
● Or just mail me…

More Related Content

Similar to Physics for Game Programmers: Spatial Data Structures

Caustic Object Construction Based on Multiple Caustic Patterns
Caustic Object Construction Based on Multiple Caustic PatternsCaustic Object Construction Based on Multiple Caustic Patterns
Caustic Object Construction Based on Multiple Caustic PatternsBudianto Tandianus
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11chidabdu
 
Data Mining: Concepts and Techniques (3rd ed.) — Chapter 5
Data Mining:  Concepts and Techniques (3rd ed.)— Chapter 5 Data Mining:  Concepts and Techniques (3rd ed.)— Chapter 5
Data Mining: Concepts and Techniques (3rd ed.) — Chapter 5 Salah Amean
 
Clustering of graphs and search of assemblages
Clustering of graphs and search of assemblagesClustering of graphs and search of assemblages
Clustering of graphs and search of assemblagesData-Centric_Alliance
 
So sánh cấu trúc protein_Protein structure comparison
So sánh cấu trúc protein_Protein structure comparisonSo sánh cấu trúc protein_Protein structure comparison
So sánh cấu trúc protein_Protein structure comparisonbomxuan868
 
PR-132: SSD: Single Shot MultiBox Detector
PR-132: SSD: Single Shot MultiBox DetectorPR-132: SSD: Single Shot MultiBox Detector
PR-132: SSD: Single Shot MultiBox DetectorJinwon Lee
 
Paper study: Learning to solve circuit sat
Paper study: Learning to solve circuit satPaper study: Learning to solve circuit sat
Paper study: Learning to solve circuit satChenYiHuang5
 
Case Study of Convolutional Neural Network
Case Study of Convolutional Neural NetworkCase Study of Convolutional Neural Network
Case Study of Convolutional Neural NetworkNamHyuk Ahn
 
Building Scalable Producer-Consumer Pools based on Elimination-Diraction Trees
Building Scalable Producer-Consumer  Pools based on Elimination-Diraction TreesBuilding Scalable Producer-Consumer  Pools based on Elimination-Diraction Trees
Building Scalable Producer-Consumer Pools based on Elimination-Diraction TreesGuy Korland
 
Space lattice and crystal structure,miller indices PEC UNIVERSITY CHD
Space lattice and crystal structure,miller indices PEC UNIVERSITY CHDSpace lattice and crystal structure,miller indices PEC UNIVERSITY CHD
Space lattice and crystal structure,miller indices PEC UNIVERSITY CHDPEC University Chandigarh
 
Chapter 5. Data Cube Technology.ppt
Chapter 5. Data Cube Technology.pptChapter 5. Data Cube Technology.ppt
Chapter 5. Data Cube Technology.pptSubrata Kumer Paul
 
06 image features
06 image features06 image features
06 image featuresankit_ppt
 

Similar to Physics for Game Programmers: Spatial Data Structures (20)

Caustic Object Construction Based on Multiple Caustic Patterns
Caustic Object Construction Based on Multiple Caustic PatternsCaustic Object Construction Based on Multiple Caustic Patterns
Caustic Object Construction Based on Multiple Caustic Patterns
 
Algorithm Homework Help
Algorithm Homework HelpAlgorithm Homework Help
Algorithm Homework Help
 
Algorithms Exam Help
Algorithms Exam HelpAlgorithms Exam Help
Algorithms Exam Help
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11
 
PAM.ppt
PAM.pptPAM.ppt
PAM.ppt
 
Data Mining: Concepts and Techniques (3rd ed.) — Chapter 5
Data Mining:  Concepts and Techniques (3rd ed.)— Chapter 5 Data Mining:  Concepts and Techniques (3rd ed.)— Chapter 5
Data Mining: Concepts and Techniques (3rd ed.) — Chapter 5
 
Clustering of graphs and search of assemblages
Clustering of graphs and search of assemblagesClustering of graphs and search of assemblages
Clustering of graphs and search of assemblages
 
So sánh cấu trúc protein_Protein structure comparison
So sánh cấu trúc protein_Protein structure comparisonSo sánh cấu trúc protein_Protein structure comparison
So sánh cấu trúc protein_Protein structure comparison
 
PR-132: SSD: Single Shot MultiBox Detector
PR-132: SSD: Single Shot MultiBox DetectorPR-132: SSD: Single Shot MultiBox Detector
PR-132: SSD: Single Shot MultiBox Detector
 
Paper study: Learning to solve circuit sat
Paper study: Learning to solve circuit satPaper study: Learning to solve circuit sat
Paper study: Learning to solve circuit sat
 
Case Study of Convolutional Neural Network
Case Study of Convolutional Neural NetworkCase Study of Convolutional Neural Network
Case Study of Convolutional Neural Network
 
Building Scalable Producer-Consumer Pools based on Elimination-Diraction Trees
Building Scalable Producer-Consumer  Pools based on Elimination-Diraction TreesBuilding Scalable Producer-Consumer  Pools based on Elimination-Diraction Trees
Building Scalable Producer-Consumer Pools based on Elimination-Diraction Trees
 
Structure prediction
Structure predictionStructure prediction
Structure prediction
 
Space lattice and crystal structure,miller indices PEC UNIVERSITY CHD
Space lattice and crystal structure,miller indices PEC UNIVERSITY CHDSpace lattice and crystal structure,miller indices PEC UNIVERSITY CHD
Space lattice and crystal structure,miller indices PEC UNIVERSITY CHD
 
Real-time lightmap baking
Real-time lightmap bakingReal-time lightmap baking
Real-time lightmap baking
 
05 cubetech
05 cubetech05 cubetech
05 cubetech
 
Chapter 5. Data Cube Technology.ppt
Chapter 5. Data Cube Technology.pptChapter 5. Data Cube Technology.ppt
Chapter 5. Data Cube Technology.ppt
 
Phys 4710 lec 3
Phys 4710 lec 3Phys 4710 lec 3
Phys 4710 lec 3
 
09 placement
09 placement09 placement
09 placement
 
06 image features
06 image features06 image features
06 image features
 

More from ナム-Nam Nguyễn

Application parallelisation Android - Klaas Vangend
Application parallelisation Android - Klaas VangendApplication parallelisation Android - Klaas Vangend
Application parallelisation Android - Klaas Vangendナム-Nam Nguyễn
 
AI in Games- Steering, Wander and Flocking behavior
AI in Games- Steering, Wander and Flocking behaviorAI in Games- Steering, Wander and Flocking behavior
AI in Games- Steering, Wander and Flocking behaviorナム-Nam Nguyễn
 
Improved Alpha-Tested Magnification for Vector Textures and Special Effects
Improved Alpha-Tested Magnification for Vector Textures and Special EffectsImproved Alpha-Tested Magnification for Vector Textures and Special Effects
Improved Alpha-Tested Magnification for Vector Textures and Special Effectsナム-Nam Nguyễn
 
Unite 2013 optimizing unity games for mobile platforms
Unite 2013 optimizing unity games for mobile platformsUnite 2013 optimizing unity games for mobile platforms
Unite 2013 optimizing unity games for mobile platformsナム-Nam Nguyễn
 
China mobile games and entertainment group
China mobile games and entertainment group China mobile games and entertainment group
China mobile games and entertainment group ナム-Nam Nguyễn
 
Success stories compilation of game-based learning initiatives in adults educ...
Success stories compilation of game-based learning initiatives in adults educ...Success stories compilation of game-based learning initiatives in adults educ...
Success stories compilation of game-based learning initiatives in adults educ...ナム-Nam Nguyễn
 
CEO Survey Report of Japanese Video Game Developers - Mirko Ernkvist
CEO Survey Report of Japanese Video Game Developers - Mirko ErnkvistCEO Survey Report of Japanese Video Game Developers - Mirko Ernkvist
CEO Survey Report of Japanese Video Game Developers - Mirko Ernkvistナム-Nam Nguyễn
 
Possibilities of non commercial game
Possibilities of non commercial gamePossibilities of non commercial game
Possibilities of non commercial gameナム-Nam Nguyễn
 
Children's Books, Stories and Songs - Children literature
Children's Books, Stories and Songs - Children literatureChildren's Books, Stories and Songs - Children literature
Children's Books, Stories and Songs - Children literatureナム-Nam Nguyễn
 
The Economics of the “China Price”
The Economics of the “China Price”The Economics of the “China Price”
The Economics of the “China Price”ナム-Nam Nguyễn
 
Playability and Player Experience Research
Playability and Player Experience ResearchPlayability and Player Experience Research
Playability and Player Experience Researchナム-Nam Nguyễn
 
Android Lifecycle Basics in Practice.
Android Lifecycle Basics in Practice.Android Lifecycle Basics in Practice.
Android Lifecycle Basics in Practice.ナム-Nam Nguyễn
 
Practical Source Code for Ship Motions Time Domain Numerical Analysis and Its...
Practical Source Code for Ship Motions Time Domain Numerical Analysis and Its...Practical Source Code for Ship Motions Time Domain Numerical Analysis and Its...
Practical Source Code for Ship Motions Time Domain Numerical Analysis and Its...ナム-Nam Nguyễn
 
Luoc su duc phat thich nu hue ngan core- da chuyen
Luoc su duc phat   thich nu hue ngan core- da chuyenLuoc su duc phat   thich nu hue ngan core- da chuyen
Luoc su duc phat thich nu hue ngan core- da chuyenナム-Nam Nguyễn
 

More from ナム-Nam Nguyễn (20)

Application parallelisation Android - Klaas Vangend
Application parallelisation Android - Klaas VangendApplication parallelisation Android - Klaas Vangend
Application parallelisation Android - Klaas Vangend
 
Getting Native with NDK
Getting Native with NDKGetting Native with NDK
Getting Native with NDK
 
AI in Games- Steering, Wander and Flocking behavior
AI in Games- Steering, Wander and Flocking behaviorAI in Games- Steering, Wander and Flocking behavior
AI in Games- Steering, Wander and Flocking behavior
 
Bullet Physic Engine SDK
Bullet Physic Engine SDKBullet Physic Engine SDK
Bullet Physic Engine SDK
 
Improved Alpha-Tested Magnification for Vector Textures and Special Effects
Improved Alpha-Tested Magnification for Vector Textures and Special EffectsImproved Alpha-Tested Magnification for Vector Textures and Special Effects
Improved Alpha-Tested Magnification for Vector Textures and Special Effects
 
Unite 2013 optimizing unity games for mobile platforms
Unite 2013 optimizing unity games for mobile platformsUnite 2013 optimizing unity games for mobile platforms
Unite 2013 optimizing unity games for mobile platforms
 
China mobile games and entertainment group
China mobile games and entertainment group China mobile games and entertainment group
China mobile games and entertainment group
 
Success stories compilation of game-based learning initiatives in adults educ...
Success stories compilation of game-based learning initiatives in adults educ...Success stories compilation of game-based learning initiatives in adults educ...
Success stories compilation of game-based learning initiatives in adults educ...
 
CEO Survey Report of Japanese Video Game Developers - Mirko Ernkvist
CEO Survey Report of Japanese Video Game Developers - Mirko ErnkvistCEO Survey Report of Japanese Video Game Developers - Mirko Ernkvist
CEO Survey Report of Japanese Video Game Developers - Mirko Ernkvist
 
Abusing mobilegames
Abusing mobilegamesAbusing mobilegames
Abusing mobilegames
 
Possibilities of non commercial game
Possibilities of non commercial gamePossibilities of non commercial game
Possibilities of non commercial game
 
Children's Books, Stories and Songs - Children literature
Children's Books, Stories and Songs - Children literatureChildren's Books, Stories and Songs - Children literature
Children's Books, Stories and Songs - Children literature
 
MGI Big data full report
MGI Big data full reportMGI Big data full report
MGI Big data full report
 
Ai.game.wisdom
Ai.game.wisdomAi.game.wisdom
Ai.game.wisdom
 
The Economics of the “China Price”
The Economics of the “China Price”The Economics of the “China Price”
The Economics of the “China Price”
 
Smoothed Particle Hydrodynamics
Smoothed Particle HydrodynamicsSmoothed Particle Hydrodynamics
Smoothed Particle Hydrodynamics
 
Playability and Player Experience Research
Playability and Player Experience ResearchPlayability and Player Experience Research
Playability and Player Experience Research
 
Android Lifecycle Basics in Practice.
Android Lifecycle Basics in Practice.Android Lifecycle Basics in Practice.
Android Lifecycle Basics in Practice.
 
Practical Source Code for Ship Motions Time Domain Numerical Analysis and Its...
Practical Source Code for Ship Motions Time Domain Numerical Analysis and Its...Practical Source Code for Ship Motions Time Domain Numerical Analysis and Its...
Practical Source Code for Ship Motions Time Domain Numerical Analysis and Its...
 
Luoc su duc phat thich nu hue ngan core- da chuyen
Luoc su duc phat   thich nu hue ngan core- da chuyenLuoc su duc phat   thich nu hue ngan core- da chuyen
Luoc su duc phat thich nu hue ngan core- da chuyen
 

Physics for Game Programmers: Spatial Data Structures

  • 1. Physics for Game Programmers: Spatial Data Structures Gino van den Bergen gino@dtecta.com
  • 2. “I feel like a million tonight … …But one at a time.” Mae West
  • 3. Collision Queries ● Find pairs of objects that are colliding now, … ● … or will collide over the next frame if no counter action is taken. ● Compute data for response.
  • 4. Continuous Collision Queries ● Fast moving objects, such as bullets and photons (for visibility queries), need to be swept in order to catch all potential hits. ● Querying a scene by casting rays (or shapes) is an important ability.
  • 5. Collision Objects ● Static environment (buildings, terrain) is typically modeled using polygon meshes. ● Moving objects (player, NPCs, vehicles, projectiles) are typically convex shapes. ● We focus on convex-mesh collisions. Mesh-mesh is hardly ever necessary.
  • 7. Three Phases ● Broad phase: Determine all pairs of independently moving objects that potentially collide. ● Mid phase: Determine potentially colliding primitives in complex objects. ● Narrow phase: Determine contact between convex primitives.
  • 8. Mid Phase ● Complex objects such as triangle meshes may be composed of lots of primitives. ● Testing all primitives will take too long, especially if only a few may be colliding. ● How to speed things up? Or rather, achieve a more output-sensitive solution?
  • 9. Spatial Coherence ● Expresses the degree in which a set of primitives can be ordered based on spatial location. ● Primitives occupy only a small portion of the total space. ● A location in space is associated with a limited number of primitives.
  • 10. Which Is More Coherent?
  • 11. Divide & Conquer Capture spatial coherence by using some divide & conquer scheme: ● Space Partitioning: subdivide space into convex cells. ● Model Partitioning: subdivide a set of primitives into subsets and maintain a bounding volume per subset.
  • 12. Uniform Grid ● Subdivide a volume into uniform rectangular cells (voxels). ● Cells need not keep coordinates of their position. ● Position (x, y, z) goes into cell where ex, ey, ez are the cell dimensions.       zyx ezeyexkji /,/,/),,( 
  • 13. Uniform Grid (cont’d) Two alternative strategies: ● Add a primitive to all cells that overlap the prim’s bounding box. Overlapping boxes must occupy the same cell. ● Add a primitive to the cell that contains the center of its box. Neighboring cells need to be visited for overlapping boxes, but cells contain fewer objects.
  • 14. Uniform Grid (cont’d) ● Grids work well for large number of primitives of roughly equal size and density (e.g. cloth, fluids). ● For these cases, grids have O(1) memory and query overhead.
  • 15. Spatial Hashing ● Same as uniform grid except that the space is unbounded. ● Cell ID (i, j, k) is hashed to a bucket in a hash table. ● Neighboring cells can still be visited. Simply compute hashes for (i±1, j±1, k±1).
  • 16. Spatial Hashing (cont’d) ● As for grids, spatial hashing only works well for primitives of roughly equal size and density. ● Remote cells are hashed to the same bucket, so exploitation of spatial coherence may not be that great.
  • 17. Binary Space Partitioning ● A BSP is a hierarchical subdivision of space into convex cells by (hyper)planes.
  • 18. k-D Trees ● A k-D trees is a BSP with axis-aligned partitioning planes.
  • 19. BSP Trees versus k-D Trees ● k-D trees have smaller memory footprints and faster traversal times per node. ● BSP Trees need fewer nodes to “carve out” a proper volume representation. ● The volume enclosed by a polygon mesh can be accurately represented by a BSP.
  • 20. Point Queries on a BSP while (!node->isLeaf) { node = node->plane.above(p) ? node->posChild : node->negChild; } return node->inside;
  • 21. Volume Queries on a BSP ● Fat objects may overlap multiple cells, so multiple root paths need to be traversed. ● Shrink the query object to a point and dilate the environment by adding the negate of the query object [Quake2]. ● The dilated environment is better known as Configuration Space Obstacle (CSO).
  • 22. Exact CSO for a Disk
  • 25. Dynamic Plane-shift BSP Trees ● What if we have query objects with different sizes? ● Keep the original BSP and offset the planes while traversing [Melax, MDK2]. ● Since obstacles occur both in the + and - half-space, we offset in both directions.
  • 26. Bounding Volumes ● Should fit the model as tightly as possible. ● Overlap tests between volumes should be cheap. ● Should use a small amount of memory. ● Cost of computing the best-fit bounding volume should be low.
  • 27. Bounding Volumes (cont’d) Good bounding volume types are: ● Spheres ● Axis-aligned bounding boxes (AABBs) ● Discrete-orientation polytopes (k-DOPs) ● Oriented bounding boxes (OBBs)
  • 29. Why AABBs? ● Offer a fair trade-off between storage space and fit. ● Overlap tests are fast. ● Allow for fast construction and update of bounding volumes. ● Storage requirements can be further reduced through compression.
  • 30. Binary AABB Tree ● Each internal node has two children. ● Each leaf node contains one primitive. ● For N primitives we have N leaf nodes and N – 1 internal nodes (2N – 1 AABBs in total). ● Best trees need not be, and usually are not, height-balanced.
  • 31. Volume Queries on an AABB Tree ● Compute the volume’s AABB in the AABB tree’s local coordinate system. ● Recursively visit all nodes whose AABBs overlap the volume’s AABB. ● Test each visited leaf’s primitive against the query volume.
  • 32. Sequential Tree Traversal int i = 0; while (i != nodes.size()) { if (overlap(queryBox, nodes[i].aabb)) { if (nodes[i].isLeaf) primTest(nodes[i]); ++i; } else i += nodes[i].skip; }
  • 33. Memory Considerations ● Nodes are stored in a single array. ● Each left child is stored immediately to the right of its parent. ● The skip field stores the number nodes in the subtree (the number of nodes to skip if the overlap test is negative).
  • 34. Ray Cast on an AABB Tree ● Returns the primitive that is stabbed by the ray earliest. ● Requires a line-segment-versus-box test. ● Each stabbed primitive shortens the ray. ● Traverse the AABB tree testing the AABB closest to the ray’s source first.
  • 35. Line-segment vs. Box Test ● Use a SAT testing the box’s three principal axes and the three cross products of principal axes and the line segment. ● If the line segment is almost parallel to an axis then the cross product is close to zero and the SAT may return false negatives!!!
  • 36. Line-segment Box Test (cont’d) ● The cross product rejection test has the form | rz * cy – ry * cz | > |rz| * ey+ |ry| * ez, where r is the ray direction, and c and e are resp. the center and extent of the AABB. ● The rounding noise in the lhs can get greater than the rhs, resulting in a false negative!!
  • 37. Line-segment Box Test (cont’d) ● The lhs suffers from cancellation, if c is at some distance from the origin. ● The solution is to add an upper bound for the rounding noise to the rhs, which is max(|rz * cy|, |ry * cz|)ε, Here, ε is the machine epsilon of the number type.
  • 38. Shape Casting on an AABB Tree ● Similar to ray casting but now we need to find the primitive that is hit by a convex shape. ● Perform a ray cast on the Minkowski sum of the node’s AABB and the shape’s box: [a, b] – [c, d] = [a - d, b - c]
  • 39. AABB Tree Construction ● AABB Trees are typically constructed top down. ● Bottom-up construction may yield good trees, but takes a lot of processing. ● Start with a set of AABBs of the primitives.
  • 40. Top-down Construction ● Compute the AABB of the set of AABBs. This is the root node’s volume. ● Split the set using a plane. The plane is chosen according to some heuristic. ● AABBs that straddle the plane are added to the dominant side. (AABBs of the two sets may overlap.) ● Repeat until all sets contain one AABB.
  • 41. Median Split Heuristic ● Compute the bounding box of the set of AABB center points. ● Choose the plane that splits the box in half along the longest axis.
  • 42. Median Split Heuristic No Good ● Median splits may not carve out empty space really well.
  • 43. Better Splitting Heuristic ● Off-center splits may do better.
  • 44. Surface Area Heuristic ● Find the splitting plane that minimizes SA(AABBleft) * Nleft + SA(AABBright) * Nright ● Here, SA(AABB) is the surface area of the box, and N is the number of primitives.
  • 45. Surface Area Heuristic (cont’d) ● Determining the best SAH splitting plane can be computationally expensive. ● Sufficiently-good splitting planes can be found quickly by using a binning technique: ● Evaluate a pre-defined set of splitting planes, and pick the best one.
  • 46. Surface Area Heuristic (cont’d) ● Group primitives per cell and compute the AABB of the group. ● Compute AABBleft and AABBright for each splitting plane from these cell AABBs, and compute their SA.
  • 48. Updating AABB Trees ● AABB trees can be updated rather than reconstructed for deformable meshes. ● First recompute the AABBs of the leaves. ● Work your way back to the root: A parent’s box is the AABB of the children’s boxes. ● For extreme deformations reconstruction is better and may not be that slow [Wald].
  • 49. Compressed AABB Trees [Gomez] ● Only 6 of the 12 faces of the child AABBs differ from the parent’s faces. ● Pack the children paired, and only store the coordinates of these new inner faces. ● For each of the 6 inner faces store a bit to denote which child it belongs to.
  • 50. Compressed AABB Trees [Gomez] ● Inner faces are shared among children. ● Sharing need not be even.
  • 51. Compressed AABB Trees [Gomez] ● Encode each of the 6 inner face coordinates by a single byte. ● The byte represents the coordinate as a fraction of the parent’s AABB. ● Lower bounds are rounded down. Upper bounds are rounded up. ● This reduces the memory footprint from 56 to 16 bytes per primitive.
  • 52. Boxtree [Zachmann] ● Since the set of primitives is split along the longest axis, only one of each child’s faces will differ significantly from its parent’s. This face differs significantly from the box’s parent. This face is close to the box’s parent
  • 53. Boxtree [Zachmann] ● Store only the coordinate for the inner faces (similar to k-d tree.) ● The other coordinates are inherited from the parent box. Only these coordinates are stored.
  • 54. Boxtree [Zachmann] The Boxtree (aka Bounding-Interval Hierarchy) has a few benefits over traditional AABB trees: ● Smaller memory footprint. ● Slightly faster build times. ● Faster traversal times due to the fact that the number of axes in the SAT can be further reduced. ● Empty space is captured less greedily, so YMMV!
  • 55. References ● Stefan Gottschalk e.a. OBBTree: A Hierarchical Structure for Rapid Interference Detection. Proc SIGGRAPH, 1996 ● Gino van den Bergen. Efficient Collision Detection of Complex Deformable Models using AABB Trees. JGT, Vol. 2, No. 4, 1997 ● Stan Melax. Dynamic Plane Shifting BSP Traversal. Proc. Graphics Interface, 2000
  • 56. References ● Ingo Wald. On Fast Construction of SAH Based Bounding Volume Hierarchies. Proc. Eurographics, 2007 ● Miguel Gomez. Compressed Axis-Aligned Bounding Box Trees. Game Programming Gems 2, 2001 ● Gabriel Zachmann. Minimal Hierarchical Collision Detection. Proc. VRST, 2002.
  • 57. Thank You! My pursuits can be traced on: ● Web: http://www.dtecta.com ● Twitter: @dtecta ● Or just mail me…