collision detection

40
Collision Detection CSE 191A: Seminar on Video Game Programming Lecture 3: Collision Detection UCSD, Spring, 2003 Instructor: Steve Rotenberg

Upload: alvis

Post on 12-Feb-2016

87 views

Category:

Documents


0 download

DESCRIPTION

Collision Detection. CSE 191A: Seminar on Video Game Programming Lecture 3: Collision Detection UCSD, Spring, 2003 Instructor: Steve Rotenberg. Collision Detection. Geometric intersection detection Main subjects Intersection testing Optimization structures Pair reduction. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Collision Detection

Collision DetectionCSE 191A: Seminar on Video Game Programming

Lecture 3: Collision DetectionUCSD, Spring, 2003

Instructor: Steve Rotenberg

Page 2: Collision Detection

Collision DetectionGeometric intersection detectionMain subjects

Intersection testingOptimization structuresPair reduction

Page 3: Collision Detection

Intersection Testing

Page 4: Collision Detection

Intersection TestingGeneral goals: given two objects with current and previous orientations specified, determine where and when the two objects will first intersectAlternative: given two objects with only current orientations, determine if they intersectSometimes, we need to find all intersections. Other times, we just want the first one. Sometimes, we just need to know if the two objects intersect and don’t need the actual intersection data.

Page 5: Collision Detection

Triangle Normalsn=(v1-v0)×(v2-v0)

Length of n is twice the area of the triangle (ABsinθ)

v0

v1

v2

v1-v0

v2-v0n

Page 6: Collision Detection

Segment vs. Triangle

a

b

Does segment (ab) intersect triangle (v0v1v2) ?

First, compute signed distances of a and b to planeda=(a-v0)·n db=(b-v0)·n

Reject if both are above or both are below triangleOtherwise, find intersection point

x=(b*da-a*db)/(da-db)

x

Page 7: Collision Detection

Segment vs. TriangleIs point x inside the triangle?

(x-v0)·((v2-v0)×n) > 0

Test all 3 edges

xv0

v1

v2

v2-v0

(v2-v0)×n

x-v0

Page 8: Collision Detection

Faster WayReduce to 2D: remove smallest dimensionCompute barycentric coordinatesx' =x-v0

e1=v1-v0

e2=v2-v0

α=(x'×e2)/(e1×e2)β=(x'×e1)/(e1×e2)Reject if α<0, β<0 or α+β >1

xv0

v1

v2

α

β

Page 9: Collision Detection

Segment vs. MeshTo test a line segment against a mesh of triangles, simply test the segment against each triangleSometimes, we are interested in only the ‘first’ hit. Other times, we want all intersections.We will look at ways to optimize this later

Page 10: Collision Detection

Segment vs. Moving MeshM0 is the object’s matrix at time t0

M1 is the matrix at time t1

Compute delta matrix:M1=M0·MΔ

MΔ= M0-1·M1

Transform A by MΔ

A'=A·MΔ

Test segment A'B against object with matrix M1

Page 11: Collision Detection

Triangle vs. TriangleGiven two triangles: T1 (u0u1u2) and T2 (v0v1v2)

u0

u2

u1

v0

v1

v2

T1

T2

Page 12: Collision Detection

Triangle vs. TriangleStep 1: Compute plane equations

n2=(v1-v0)×(v2-v0)

d2=-n2·v0

v0

v1

v2

v1-v0

v2-v0n

Page 13: Collision Detection

Triangle vs. TriangleStep 2: Compute signed distances of T1 vertices to

plane of T2:

di=n2·ui+d2 (i=0,1,2)

Reject if all di<0 or all di>0

Repeat for vertices of T2 against plane of T1

d0

u0

Page 14: Collision Detection

Triangle vs. TriangleStep 3: Find intersection pointsStep 4: Determine if segment pq is inside triangle or intersects triangle edge

p q

Page 15: Collision Detection

Mesh vs. MeshGeometry: points, edges, facesCollisions: p2p, p2e, p2f, e2e, e2f, f2fRelevant ones: p2f, e2e (point to face & edge to edge)Multiple collisions

Page 16: Collision Detection

Moving Mesh vs. Moving MeshTwo options: ‘point sample’ and ‘extrusion’Point sample:

If objects intersect at final positions, do a binary search backwards to find the time when they first hit and compute the intersectionThis approach can tend to miss thin objects

Extrusion:Test ‘4-dimensional’ extrusions of objectsIn practice, this can be done using only 3D math

Page 17: Collision Detection

Moving Meshes: Point SamplingRequires instantaneous mesh-mesh intersection testBinary search

Page 18: Collision Detection

Moving Meshes: ExtrusionUse ‘delta matrix’ trick to simplify problem so that one mesh is moving and one is staticTest moving vertices against static faces (and the opposite, using the other delta matrix)Test moving edges against static edges (moving edges form a quad (two triangles))

Page 19: Collision Detection

Convex Geometry: V-ClipTracks closest featuresFails when objects intersectRequires pairwise updates

Page 20: Collision Detection

Box vs. BoxSeparating Axis Theorem

If boxes A and B do not overlap, then there should exist a separating axis such that the projections of the boxes on the axis don’t overlap. This axis can be normal to the face of one object or connecting two edges between the two objects.Up to 15 axes must be tested to check if two boxes overlap

Page 21: Collision Detection

Triangle vs. Box1. Test if triangle is outside any of the 6 box

planes2. Test if the box is entirely on one side of

the triangle plane3. Test separating axis from box edge to

triangle edge

Page 22: Collision Detection

Intersection IssuesPerformanceMemoryAccuracyFloating point precision

Page 23: Collision Detection

Optimization Structures

Page 24: Collision Detection

Optimization StructuresBV, BVH (bounding volume hierarchies)OctreeKD treeBSP (binary separating planes)OBB tree (oriented bounding boxes- a popular form of BVH)K-dopUniform grid

Page 25: Collision Detection

Testing BVH’sTestBVH(A,B) {

if(not overlap(ABV, BBV) return FALSE;else if(isLeaf(A)) {if(isLeaf(B)) { for each triangle pair (Ta,Tb)if(overlap(Ta,Tb)) AddIntersectionToList();}else { for each child Cb of BTestBVH(A,Cb);}}else {for each child Ca of A TestBVH(Ca,B)}

}

Page 26: Collision Detection

Bounding Volume Hierarchies

Page 27: Collision Detection

Octrees

Page 28: Collision Detection

KD Trees

Page 29: Collision Detection

BSP Trees

Page 30: Collision Detection

OBB Trees

Page 31: Collision Detection

K-Dops

Page 32: Collision Detection

Uniform Grids

Page 33: Collision Detection

Optimization StructuresAll of these optimization structures can be used in either 2D or 3DPacking in memory may affect caching and performance

Page 34: Collision Detection

Pair Reduction

Page 35: Collision Detection

Pair ReductionReduce number of n^2 pair testsPair reduction isn’t a big issue until n>50 or so…

Page 36: Collision Detection

Uniform GridAll objects are tracked by their location in a uniform gridGrid cells should be larger than diameter of largest object’s bound sphereTo check collisions, test all objects in cell & neighboring cellsHierarchical grids can be used also

Page 37: Collision Detection

Hashing GridCells don’t exist unless they contain an objectWhen an object moves, it may cross to a new cell

Page 38: Collision Detection

Conclusion

Page 39: Collision Detection

Preview of Next Week

PhysicsParticlesRigid bodiesVehicles

Page 40: Collision Detection

Reading Assignment

“Real Time Rendering”Chapter 14