Part 7: Collision Detection Virtuelle Realität Wintersemester 2007/08 Prof. Bernhard Jung Overview Bounding Volumes Separating Axis Theorem Using Bounding Volume Hierarchies Efficient Collision Detection for Several Hundreds of Objects Further Reading Akenine-Möller & Haines. Real-Time Rendering. 2nd edition. AK Peters. 2002. Chapters 13 + 14 Free collision detection software http://www.cs.unc.edu/~geom/collide/index.shtml http://www.cs.unc.edu/~geom/v_collide/ 1
Collisions: In General Without collision detection (CD), it is practically impossible to e.g., games, movie production tools Three major parts Collision detection: do objects collide? Collision determination: where? Collision response This lecture: collision detection + determination Collision response involves physically-based animation Use rays for simple applications Use Bounding Volume Hierarchies (BVHs) to test two complex objects against each other But what if several hundreds of objects? Typical Bounding Volumes Sphere Box Axis-aligned Bounding Box (AABB) Oriented (OBB) k-dop "discrete oriented polytype" generalizes AABB (in 3D): AABB = 6-DOP Different tightness and different costs for intersection testing 2
k-dops source: Unreal developer network http://udn.epicgames.com/two/collisiontutorial Simple Tests: Ray/Box Intersection Boxes and spheres often used as bounding volumes also: "hit-boxes" in games hit-box (wikipedia) A slab is the volume between two parallel planes: A 3D box is the logical intersection of three slabs (2 in 2D): BOX 3
Simple Tests: Ray/Box Intersection (2) Intersect the 2 planes of each slab with the ray Ray: r(t)=o+td (o origin, d direction) max t y min t x min t y max t x Keep max of t min and min of t max If t min < t max then we got an intersection Special case when ray parallel to slab Simple Tests: Sphere-Sphere trivial: r2 if distance of centers > sum of radii then disjoint r1 c1 d c2 4
Simple Tests: AABB-AABB trivial: project boxes to axes if no overlap in any of {x,y,z}-axes then disjoint ("separating axis") Simple Tests: k-dop k-dop generalizes AABB-AABB intersection test project bounding volumes to k/2 axes disjoint if not overlapping on at least one ("separating") axis for moderate values of k, e.g. k=18, an order of magnitude faster than OBB-OBB intersection testing min min min min 5
Separating Axis Theorem (SAT) Summary: Given two convex shapes, if we can find an axis along which the projection of the two shapes does not overlap, then the shapes don't overlap Concretely: Two convex polyhedrons, A and B, are disjoint if any of the following axes separate the objects: An axis orthogonal to a face of A An axis orthogonal to a face of B An axis formed from the cross product of one edge from each of A and B axis faces A and B overlap on this axis SAT example: Triangle-AABB (2D case) Box is axis-aligned test x, y axes Triangle: two edges parallel to x, y test axis perpendicular to the one non-aligned edge http://www.harveycartel.org/ 6
SAT example: Triangle-Box Box is axis-aligned 1) test the axes that are orthogonal to the faces of the box That is, x, y, and z here: no overlap in x disjoint Triangle-Box with SAT (2) Assume that they overlapped on x, y, z Must continue testing 2) Axis orthogonal to face of triangle axis Triangle seen from side here: overlap in selected axis must continue testing 7
Triangle-Box with SAT (3) If still no separating axis has been found 3) Test axis: t=e box x e triangle Example: x-axis from box: e box =(1,0,0) e triangle =v 1 -v 0 Test all such combinations If there is at least one separating axis, then the objects do not collide Else they do overlap OBB OBB with SAT Project to 15 potential separating axes: 3 from object-1 x 1, y 1, z 1 3 from object-2 x 2, y 2, z 2 9 from combinations of edges from two objects x 1 x 2 x 1 y 2 x 1 z 2 y 1 x 2 can stop testing when first separating axis is found potential separating axis 8
Rules of Thumb for Intersection Testing Acceptance and rejection test Try them early on to make a fast exit Postpone expensive calculations if possible Use dimension reduction E.g. 3 one-dimensional tests instead of one complex 3D test, or 2D instead of 3D Share computations between objects if possible Do timing comparisons Dynamic Separating Axis Theorem (advanced topic) SAT: tests one axis at a time for overlap Same with DSAT, but: Need to adjust the projection on the axis so that the interval moves on the axis as well Need to test same axes as with SAT Same criteria for overlap/disjoint: If no overlap on axis => disjoint If overlap on all axes => objects overlap 9
For many, many objects Test BV (bounding volume) of each object against BV of other object Works for small sets, but not very clever Reason assume moving n objects number of tests in naïve approach: if m static objects, then: n nm + 2 n = n( n 1) / 2 = O( n 2 2 ) there are smarter ways, e.g. sweep-and-prune (last part of lecture) Collision detection with rays Imagine a car is driving on a road sloping upwards Could test all triangles of all wheels against road geometry For certain applications, we can approximate, and still get a good result Idea: approximate a complex object with a set of rays 10
CD with rays, cont d Put a ray at each wheel Compute the closest intersection distance, t, between ray and road geometry If t=0, then car is on the road If t>0, then car is flying above road If t<0, then car is plowing deep in the road Use values of t to compute a simple collision response Another simplification Sometimes 3D can be turned into 2D operations Example: maze A human walking in maze, can be approximated by a circle Test circle against lines of maze Or even better, move walls outwards with circle radius test center of circle against moved walls 11
A collision detection system for accurate detection and for many objects covered here: pruning and exact CD Simulation is how objects move General Hierarchical Collision Detection Model is represented as a hierarchy of some kind of bounding volumes A hierarchy that is commonly used is a data structure called a k-ary tree typical BVHs: binary trees At each internal node, there is a BV that encloses all of its children in its volume At each leaf there are one or more primitives (e.g. triangles) 12
Hierarchy Building Three ways: Bottom-up by combining a number of primitives and finding a BV for them Top-down by finding a BV for all primitives of the model, then divide-and-conquer is applied Incremental tree-insertion starts with an empty tree and then all other primitives and their BVs are added one at a time to this tree Insertion point should be selected so that the total tree volume increase is minimized OBBTree Hierarchy Building OBBTree: binary tree with each internal node holding an OBB and each external node holding only one triangle top-down hierarchy building approach is divided into finding a tight-fitting OBB and splitting this along one axis of the OBB details see (Akenine-Möller&Haines, 2002) 13
Pseudo code for BVH against BVH Pseudocode deals with 4 cases: 1) Leaf against leaf node 2) Internal node against internal node 3) Internal against leaf 4) Leaf against internal Comments on pseudocode The code terminates when it finds the first triangle pair that collided collision detection Simple to modify code to continue traversal and put each pair in a list collision determination Reasonably simple to include rotations for objects as well Note that if we use AABB for both BVHs, then the AABB-AABB test becomes a AABB-OBB test 14
Tradeoffs Cost parameters for collision detection: Choice of BV Sphere, AABB, OBB, k-dop In general, the tighter BV, the slower test Less tight BV, gives more triangle-triangle tests in the end Model rotation may requires updates of AABB, k-dop not: OBB, sphere Cost function: k-dop update due to rotation Multiple Objects CD: Pruning Why needed? Consider several hundreds of rocks tumbling down a slope Include a first rough but simple test on whether objects can potentially collide This system is often called First-Level CD We execute this system because we want to execute the 2 nd system less frequently Assume high frame-to-frame coherency Means that object is close to where it was previous frame Reasonable 15
Sweep-and-prune algorithm [by Ming Lin] Assume objects may translate and rotate Then we can find a minimal cube (AABB), which is guaranteed to contain object for all rotations alternatively, may use bounding spheres Do collision overlap three times One for x, y, and z-axes Let s concentrate on one axis at a time Each cube on this axis is an interval, from s i to e i, where i is cube number Sweep-and-prune algorithm Sort all s i and e i into a list Traverse list from start to end When an s is encountered, mark corresponding interval as active in an active_interval_list When an e is encountered, delete the interval in active_interval_list All intervals in active_interval_ list are overlapping! 16
Sweep-and-prune algorithm Sorting is expensive (on unsorted lists): O(n log n) But, exploit frame-to-frame coherency! a.k.a. "temporal" coherence the list is not expected to change much Therefore, re-sort with insertion-sort (for nearly sorted lists O(n) ) Expected time complexity for sorting: O(n) Frame t Frame t+1 (object 2 moved) Sweep-and-prune algorithm Keep a Boolean for each pair of intervals If all Booleans for all three axes are true objects overlap Expected time complexity of complete sweep-and-prune algorithm: O(n+k) where k is the number of pairwise overlaps Frame t Frame t+1 (object 2 moved) 17
Collision Detection Conclusions Very important part of games! Many different algorithms to choose from Trend: hardware-acceleration (GPU) e.g. CULLIDE, UNC, 2003, http://gamma.cs.unc.edu/cullide/ 18