Collision Detection and Resolution - cse.scu.edu

Collision Detection and Resolution
Ming- Hwa Wang, Ph.D.
COEN 396 Interactive Multimedia and Game Programming
Department of Computer Engineering
Santa Clara University
Overview
collision det ect ion and resolut ion can im plem ent solidness of obj ect s in
physical simulation
collision det ect ion will det erm ine if and when t wo obj ect s collide, and
collision resolut ion will figure out where each obj ect should be once a
collision is det ect ed ( calculat ing how obj ect s m ov e aft er t he collision is
the job of the physics)
Collision Detection
som e obj ect s can m ove very fast , and som e obj ect s can have ver y
com plicat ed geom et ry; besides, collision det ect ion is very cost ly because
every obj ect should be t est ed against every ot her obj ect for a possible
collision O(n 2 )
overlap testing
overlap t est ing det ect s w het her a collision has already occurred; if
t wo obj ect s overlap, t hey are in collision; it is a discret e t est and is
the most common techniques, yet exhibits the most error
overlap t est ing is act ually a cont ainm ent problem ; it is t est if any
part of an obj ect is inside any part of anot her obj ect ; it is sim ple
wit h sphere and boxes, but it can be difficult wit h polygons; t he
volum e of polygon obj ect s m ust be approxim at ed by sim pler
geometrics shapes to make the problem tractable in real time
results:
find the time the collision took place by bisection
t he collision norm al vect or ( for com put ing t he collision response
and preventing objects from further interpenetrating)
lim it at ions of overlap t est ing: t he speed of t he fast est obj ect in t he
scene m ult iplied by t he t im e st ep m ust be less t han t he size of t he
sm allest collidable obj ect in t he scene; eit her need t o keep obj ect s
from m oving t oo fast , or sim ulat ion st ep size need t o be reduced t o
satisfy the constraint; both of those options might be undesirable
intersection testing
int ersect ion t est ing predict s if a collision will occur in t he fut ure; t he
sim ulat ion can be carefully m oved forward t o t he t im e of im pact ,
often more accurately and efficiently than overlap testing
intersect ion t est ing can be viewed as a visibilit y problem ; t est t he
geom et ry of an obj ect swept in t he direct ion of t ravel against ot her
swept/extruded geometry
sphere- spher e collision: t he sphere of radius r P m oving from t he
point P 1 at t im e t = 0 t o t he point P 2 at t im e t = 1 collides at t im e t
with another sphere of radius r Q moving from the point Q 1 to Q 2 ; t =
( - ( A B) – ( ( A B) 2 – | B| 2 ( | A| 2 – ( r P + r Q) 2 ))1/2) / | B| 2 , where A = P 1
- Q 1 and B = ( P 2 - P 1 ) – ( Q 2 - Q 1 ) ; t he sm allest dist ance ever
separating the centers of the two spheres is d 2 = |A| 2 - (A B) 2 / |B| 2
limitations:
predict ion m et hods aren’t very com pat ible wit h net worked gam e
because fut ure predicat ions rely on knowing t he exact st at e of
world at t he present t im e; due t o packet lat ency, t he current
state is not always coherent
int ersect ion t est ing assum es a const ant velocit y and zero
acceleration over the simulation step
Dealing with Complexity
sim plified geom et ry: if a com plex obj ect can be r oughly approxim at ed
wit h a sim pler shape, t est ing will be cheaper ( but som e part s of t he
object might come into collision without being able to detect it)
Minkowaski sum: the Minkowaski sum of object X and another object
Y can be created by sweeping the origin of X over all points within Y;
X Y = {A + B: A X and B Y}
det erm ine overlap by t aking t he origin of t he sphere and t est ing
if it is within the new volume
t o perform t he int ersect ion t est ing, t he point becom es a line,
going from t he cent er of t he spher e at t im e t 0 at t he cent er of
t he sphere at time t 1 ; we t hen t est t his line t o see if it is
intersects with the new volume
bounding volum es: a sim ple geom et ric shape t hat fully encapsulat es
an object
if t here is no collision wit h t he bounding volum e ( cheaper t o
t est ) , it is known t hat t here is no collision wit h t he obj ect ; use
bounding volum es when approxim at e collision det ect ion suffices,
or use increasingly com plex bounding volum es when accuracy
matters
t he sim plest bounding volum e is a sphere ( no orient at ion) ; t wo
spheres overlap each ot her if t he dist ance bet ween t heir cent er s
is less t han t he sum of t heir radii; in t he case of visibilit y, an
ext ruded sphere is a capsule, so each of t hese obj ect s can be
tested against the other extruded objects to determine overlap
the next most common bounding volume is a box
o axis- aligned bounding box (AABB): the faces line up with the
3 axes; usually result s in a loose fit around an obj ect , but
the resulting tests against the box are simplified and cheaper
to perform
o orient ed bounding box ( OBB) : a t ight er fit t ing box t hat is
oriented in a manner to best encapsulate the object
if an object is complex, it is often possible to fit several bounding
volumes around its unique parts; besides, there might be several
levels of bounding volumes
achieving linear complexity instead of O( n 2 )
t he world is part it ioned wit h a sim ple grid, each obj ect m ust only be
tested against objects in the same or neighboring grid cells
if there are N collidable objects, a 2D grid will need to be at least
N1/2 x N1/2 in size, and a 3D grid will need t o be at least N1/3 x
N1/3 x N1/3; t his will on average r esult in one obj ect per grid cell,
which should support linear time complexity (on average)
problems:
o if obj ect s vary in size and don’t fit inside a single grid; eit her
increase t he cell size or m ore grid cells “further out” must be
tested
o if all of t he obj ect s m ov e int o t he sam e grid cell, t hen t he
time complexity has reverted back to O( n 2 )
plane sweep: leverages the temporal coherence of objects to roughly
st ay in t he sam e locat ion from fram e t o fram e, t hus reducing t he
problem t o linear; any obj ect s t hat have overlapping bounds in all
axes should be exam ined m ore closely for a collision; t he t im e
consum ing aspect of t his algorit hm is collect ing and t hen sort ing ( in
O( n log n) ) t he bounds on each axis every fram e; since obj ect s
display coherence from fr am e t o fram e, we can sort each axis list
once, and t hen use insert ion sort t o quickly repair any of t he bounds
that became slightly out of order; the result is nearly linear time
Terrain Collision Detection
a flat plane as ground: t he sim plest t ype of t errain; if t he charact er is
st anding, t he bot t om of each foot should rest at t he t errain’s y
coordinat e; if t he charact er j um ps int o t he air and falls t o t he ground,
t he charact er will hit t he ground when his foot at t em pt s t o go below t he
t errain; if it does go below, t he foot is in collision wit h t he t errain and
should be placed at the terrain height
a polygon m esh ( in x- and z- axes) wit h height field ( in y- axis) : t reat t he
terrain as a 2D planar mesh and use the nature of the 2D uniform grid to
locat e t he exact rect angle cell t hat cont ains t he point Q ( represent t he
point of t he obj ect t hat should rest on t he t err ain) ; once we have
ident ified t he rect angle cell, we m ust det erm ine which t riangle cont ains
point Q by com paring wit h t he dividing line ( which divide t he r ect angle
into 2 triangles)
if Qz > Qx , upper left triangle
if Qz = Qx , lower right triangle
if Qz > 1 – Qx , upper right triangle
if Qz = 1 – Qx , lower right triangle
Qy = ( - Nx Qx – NzQz + N P 0 ) / Ny , where P 0 is one of t he t riangle’s
vertices
t riangulat ed irregular net works ( TI Ns) : if t he t errain is a non- uniform
polygonal m esh, use spat ial part it ion t echniques ( e.g., oct rees) t o
narrow down what part of t he t errain we’re int erest ed in, producing a
subset of candidat e polygons, t hen t est each polygon unt il one is found
that contain the point Q
barycent ric coordinat e: represent point Q in t erm s of a weight ed
sum of each triangle vertex; the 3 weights must add up to 1.0, so w 0
= 1 – w1 – w2
| w1 |
| V2 2
-V1 V2 |
| S V1 |
2
2
2
|
| = 1/(V1 V2 – (V 1 V 2 ) ) |
|
|
|
| w2 |
| -V1 V2
V1 2 |
| S V2 |
where S = Q – P 0 , V 1 = P 1 – P 0 , and V 2 = P 2 – P 0
if t he t ext ure coordinat es ( s0 , t 0 ) , ( s1 , t 1 ) , and ( s2 , t 2 ) are associat ed
wit h vert ices P 0 , P 1 , and P 2 , t he t ext ure coordinat es ( s, t ) at t he
point Q are given by s = w 0 s0 + w 1 s1 + w 2 s2 and t = w 0 t 0 + w 1 t 1 +
w 2t 2
Collision Resolution
the procedure for collision resolution has 3 parts:
prologue callback funct ion: if t he collision should not affect t he
posit ion or t raj ect ories of t he obj ect s, t he funct ion ret urns false; t he
prologue m ay also t rigger ot her event s, e.g., sound effect , sending
collision notification, etc.
collision: t he obj ect s will be placed at t he point of im pact and new
velocity will be assigned using physics or some other decision logic
epilogue: any post - collision affect s m ust be propagat ed; by sending
a collision epilogue event to each object, with the object determining
what effects to trigger
resolving overlap testing: 4 steps in resolving the collision:
ext ract t he collision norm al: const ruct ed by using t he t wo closest
point s on each surface ( wit h spheres, t he collision norm al is t he
difference bet ween t he cent ers of each sphere at t he point of
impact)
extract the penetration depth
move the tow objects apart to a penetration depth of zero if needed
compute the new velocities using Newtonian physics
resolving int ersect ion t est ing: sim pler because obj ect s never act ually
penet rat e; w it hout overlap, t here is no need t o det ect t he penet rat ion
depth and move the object apart