75
CSCE 590E Spring 2007 Collision Detection By Jijun Tang

CSCE 590E Spring 2007 Collision Detection By Jijun Tang

  • View
    221

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

CSCE 590E Spring 2007

Collision Detection

By Jijun Tang

Page 2: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Announcements

Final game demo will be held at 2:00pm, Tuesday, May 8th

Homework 4 will be given today Second presentation will be held on April 16t

h and 18th

April 16th: Space Banditos, Slackers, Psychosoft April 18th: Project Gnosis, Cheeze Puffs!, Team

Swampus

Page 3: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Needs to do

Two groups need to send me the first presentation after the class

Some people didn’t send me the members in their small project.

Please send the membership of the small project to [email protected]

Page 4: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Real-time Physics in Game at Runtime:

Enables the emergent behavior that provides player a richer game experience

Potential to provide full cost savings to developer/publisher

Difficult May require significant upgrade of game engine May require significant update of asset creation pipelines May require special training for modelers, animators, and

level designers Licensing an existing engine may significantly

increase third party middleware costs

Page 5: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Engines

Commercial Game Dynamics SDK (Havok.com) Renderware Physics (renderware.com) NovodeX SDK (novodex.com)

Free Open Dynamic Engine (ODE) (ode.org) Tokamak Game Physics SDK (tokamakphysics.

com) Newton Game Dynamics SDK (newtondynamics.

com)

Page 6: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Particle Physics

What is a Particle? A sphere of finite radius with a perfectly smooth,

frictionless surface Experiences no rotational motion (or assume the

sphere has no size) Particle Kinematics

Defines the basic properties of particle motion Position, Velocity, Acceleration

Page 7: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Location of Particle in World Space SI Units: meters (m)

Changes over time when object moves

Particle Position

zyx ppp ,,p

Page 8: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Particle Velocity and Acceleration

Velocity (SI units: m/s) First time derivative of position:

Acceleration (SI units: m/s2) First time derivative of velocity Second time derivative of position

)()()(

lim)(0

tdt

d

t

tttt

tp

ppV

)()()(2

2

tdt

dt

dt

dt pVa

Page 9: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Newton’s 2nd Law of Motion

Paraphrased – “An object’s change in velocity is proportional to an applied force”

The Classic Equation:

m = mass (SI units: kilograms, kg) F(t) = force (SI units: Newtons)

tmt aF

Page 10: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

What is Physics Simulation?

The Cycle of Motion:

Force, F(t), causes acceleration Acceleration, a(t), causes a change in velocity Velocity, V(t) causes a change in position

Physics Simulation:

Solving variations of the above equations over time to emulate the cycle of motion

Page 11: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Concrete Example: Target Practice

F = w eig ht = m gTarget

Projectile LaunchPosition, pinit

Page 12: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Choose Vinit to Hit a Stationary Target ptarget is the stationary target location We would like to choose the initial velocity, Vinit, required to hit the target at s

ome future time, thit. Here is our equation of motion at time thit:

Solution in general is a bit tedious to derive… Infinite number of solutions! Hint: Specify the magnitude of Vinit, solve for its direction

Target Practice

2

2

1inithitinithitinitinittarget tttt gVpp

Page 13: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

969.31

Example 1

Vinit = 25 m/sValue of Radicand of tan equation:Launch angle : 19.4 deg or 70.6 deg

0.00

5.00

10.00

15.00

20.00

25.00

30.00

35.00

40.00

45.00

0.00 20.00 40.00 60.00

Horizontal Position (m)

Ver

tica

l P

osi

tio

n (

m) Projectile Launch

Position

Target Position

Trajectory 1 - HighAngle, Slow Arrival

Trajectory 2 - LowAngle, Fast Arrival

Page 14: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Finite Difference Methods

The Explicit Euler Integrator:

Properties of object are stored in a state vector, S Use the above integrator equation to incrementally update S over

time as game progresses Must keep track of prior value of S in order to compute the new For Explicit Euler, one choice of state and state derivative for particle:

)( 2

derivative statestateprior statenew

tOtdt

dtttt

SSS

pVS ,m VFS ,dtd

Page 15: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Finite Difference Methods

The Verlet Integrator:

Must store state at two prior time steps, S(t) and S(t-t) Uses second derivative of state instead of the first Valid for constant time step only (as shown above) For Verlet, choice of state and state derivative for a particle:

pS aFS mdtd /22

derivative state

2

22

2 stateprior 1 stateprior state new

)(2

t

dt

dtttttt SSSS

Page 16: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Generalized Rigid Bodies

Key Differences from Particles Not necessarily spherical in shape Position, p, represents object’s center-of-mass location Surface may not be perfectly smooth

Friction forces may be present Experience rotational motion in addition to translational

(position only) motion

Center of Mass

worldX

worldZ

objectX

objectZ

Page 17: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Additional forces

Linear Spring Viscous Damping Aerodynamic Drag Friction …

Page 18: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Linear Springs

dllkF restspring )(

Page 19: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Viscous Damping

ddVVcF epepdamping ))(( 12

Page 20: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Aerodynamic Drag

S: projected front area

CD: drag coefficient

Page 21: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Friction

Page 22: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Collision Detection and Resolution

Page 23: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

What is Collision Detection

A fundamental problem in computer games, computer animation, physically-based modeling, geometric modeling, and robotics.

Including algorithms: To check for collision, i.e. intersection, of

two given objects To calculate trajectories, impact times and

impact points in a physical simulation.

Page 24: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Collision Detection

Complicated for two reasons Geometry is typically very complex, potentially

requiring expensive testing Naïve solution is O(n2) time complexity, since

every object can potentially collide with every other object

Two basic techniques Overlap testing: Detects whether a collision has

already occurred Intersection testing: Predicts whether a collision

will occur in the future

Page 25: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Overlap Testing (a posteriori)

Overlap testing: Detects whether a collision has already occurred, sometime is referred as a posteriori

Facts Most common technique used in games Exhibits more error than intersection testing

Concept For every (small) simulation step, test every pair

of objects to see if they overlap Easy for simple volumes like spheres, harder for

polygonal models

Page 26: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Overlap Testing Results

Useful results of detected collision Pairs of objects will have collision Time of collision to take place Collision normal vector

Collision time calculated by moving object back in time until right before collision Bisection is an effective technique

Page 27: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Bisect Testing: collision detected

B B

t1

t0.375

t0.25

B

t0

I te r a tio n 1F o r w ar d 1 /2

I te r a tio n 2Bac k w ar d 1 /4

I te r a tio n 3F o r w ar d 1 /8

I te r a tio n 4F o r w ar d 1 /1 6

I te r a tio n 5Bac k w ar d 1 /3 2

I n itia l O v er lapT es t

t0.5t0.4375 t0.40625

BB B

A

A

A

AA A

Page 28: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Bisect Testing: Iteration I

B B

t1

t0.375

t0.25

B

t0

I te r a tio n 1F o r w ar d 1 /2

I te r a tio n 2Bac k w ar d 1 /4

I te r a tio n 3F o r w ar d 1 /8

I te r a tio n 4F o r w ar d 1 /1 6

I te r a tio n 5Bac k w ar d 1 /3 2

I n itia l O v er lapT es t

t0.5t0.4375 t0.40625

BB B

A

A

A

AA A

Page 29: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Bisect Testing: Iteration II

B B

t1

t0.375

t0.25

B

t0

I te r a tio n 1F o r w ar d 1 /2

I te r a tio n 2Bac k w ar d 1 /4

I te r a tio n 3F o r w ar d 1 /8

I te r a tio n 4F o r w ar d 1 /1 6

I te r a tio n 5Bac k w ar d 1 /3 2

I n itia l O v er lapT es t

t0.5t0.4375 t0.40625

BB B

A

A

A

AA A

Page 30: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Bisect Testing: Iteration III

B B

t1

t0.375

t0.25

B

t0

I te r a tio n 1F o r w ar d 1 /2

I te r a tio n 2Bac k w ar d 1 /4

I te r a tio n 3F o r w ar d 1 /8

I te r a tio n 4F o r w ar d 1 /1 6

I te r a tio n 5Bac k w ar d 1 /3 2

I n itia l O v er lapT es t

t0.5t0.4375 t0.40625

BB B

A

A

A

AA A

Page 31: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Bisect Testing: Iteration IV

B B

t1

t0.375

t0.25

B

t0

I te r a tio n 1F o r w ar d 1 /2

I te r a tio n 2Bac k w ar d 1 /4

I te r a tio n 3F o r w ar d 1 /8

I te r a tio n 4F o r w ar d 1 /1 6

I te r a tio n 5Bac k w ar d 1 /3 2

I n itia l O v er lapT es t

t0.5t0.4375 t0.40625

BB B

A

A

A

AA A

Page 32: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Bisect Testing: Iteration V

B B

t1

t0.375

t0.25

B

t0

I te r a tio n 1F o r w ar d 1 /2

I te r a tio n 2Bac k w ar d 1 /4

I te r a tio n 3F o r w ar d 1 /8

I te r a tio n 4F o r w ar d 1 /1 6

I te r a tio n 5Bac k w ar d 1 /3 2

I n itia l O v er lapT es t

t0.5t0.4375 t0.40625

BB B

A

A

A

AA A

Time right before the collision

Page 33: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Overlap Testing: Limitations

Fails with objects that move too fast (compared to the object) Thin glass vs. bulltes Unlikely to catch time slice during overlap

t0t -1 t1 t2

b u lle t

w in d o w

Page 34: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Solution for This Limitation

Speed of the fastest object multiplies the time step should be smaller than the smallest objects in the scene

Possible solutions Design constraint on speed of objects

Hard to apply without affecting the play Reduce simulation step size

Too expensive

Page 35: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Intersection Testing (a priori)

Predict future collisions When predicted:

Move simulation to time of collision Resolve collision Simulate remaining time step

Page 36: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Intersection Testing:Swept Geometry

Extrude geometry in direction of movement Swept sphere turns into a “capsule” shape

t0

t1

Page 37: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Intersection Testing:Sphere-Sphere Collision

Q 1

Q 2

P 1

P 2

P

Q

t= 0

t= 0

t= 1

t= 1

t

,

2

2222

B

rrΑBt

QP

BAΒΑ .QQPPB

QPA

1212

11

d

Page 38: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Special Cases

No collision:

B2 = 0: both objects are stationary, or they are traveling at parallel

When will collision occur?

02222 QP rrΑBBA

02222 QP rrΑBBA

Page 39: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Intersection Testing:Sphere-Sphere Collision

Smallest distance ever separating two spheres:

If

there is a collision

2

222

BAd

BA

22QP rrd

Page 40: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Intersection Testing:Limitations

Issue with networked games Future predictions rely on exact state of

world at present time Due to packet latency, current state not

always coherent Assumes constant velocity and zero

acceleration over simulation step Has implications for physics model and

choice of integrator

Page 41: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Dealing with Complexity

Two issues1. Complex geometry must be simplified

2. Reduce number of object pair tests

Page 42: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Simplified Geometry

Approximate complex objects with simpler geometry, like this ellipsoid or bounding boxes

Page 43: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Minkowski Sum

By taking the Minkowski Sum of two complex volumes and creating a new volume, overlap can be found by testing if a single point is within the new volume

Page 44: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Minkowski Sum

Y}B and :{ XABAYX

X Y =YX X Y =

Page 45: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Using Minkowski Sum

t0

t1

t0

t1

Page 46: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Bounding Volumes

Bounding volume is a simple geometric shape Completely encapsulates object If no collision with bounding volume, no

more testing is required Common bounding volumes

Sphere Box

Page 47: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Box Bounding Volumes

Ax is - Alig n ed Bo u n d in g Bo x O r ien ted Bo u n d in g Bo x

Page 48: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

More Examples

Page 49: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Using Bounding Box in Game

Complex objects can have multiple bounding boxes Human object can have one big bounding box

for the whole body Human object can have one bounding box per

limb, head, etc Bounding box can be hierarchical:

Test the big first if possible collision, test the smaller ones

Page 50: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Reduce Number of Detections

O(n) Time Complexity can be achieved.

One solution is to partition space

Page 51: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Achieving O(n) Time Complexity

Another solution is the plane sweep algorithm

Requires (re-)sorting in x (y) coordinate

C

B

R

A

x

y

A 0 A 1 R 0 B0 R 1 C 0 C 1B1

B0

B1A 1

A 0

R 1

R 0

C 1

C 0

Page 52: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Terrain Collision Detection:Height Field Landscape

T o p - D o wn Vie w

P e r sp e c t iv e Vie w

T o p - D o wn Vie w ( h e igh t s a dde d)

P e r sp e c t iv e Vie w ( h e igh t s a dde d)

Page 53: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Locate Triangle on Height Field

Q

R

Q

Q z > Q x

Q z < = Q x

z

x

R z > 1 - R x

R z < = 1 - R x

R

Essentially a 2D problem

Page 54: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Locate Point on Triangle

Plane equation: A, B, C are the x, y, z components of

the triangle plane’s normal vector Where

with one of the triangles

vertices being Giving:

0 DCzByAx

0PN D

0P

00 PNNNN zyx zyx

Page 55: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Terrain Collision Detection:Locate Point on Triangle

The normal can be constructed by taking the cross product of two sides:

Solve for y and insert the x and z components of Q, giving the final equation for point within triangle:

0201 PPPPN

y

zzxxy N

PNQNQNQ 0

Page 56: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Treating Nonuniform Polygon Mesh

Hard to detect the triangle where the point lies in

Using Triangulated Irregular Networks (TINs)

Barycentric Coordinates

Page 57: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Barycentric Coordinates

Even with complex data structure, we still have to test each triangle (in a sub region) to see if the point lies in it

Using Barycentric Coordinates to do the test

P 1

P 2

P 0

Q

P o in t = w 0 P 0 + w 1 P 1 + w 2 P 2

Q = ( 0 ) P 0 + ( 0 .5 ) P 1 + ( 0 .5 ) P 2

R = ( 0 .3 3 ) P 0 + ( 0 .3 3 ) P 1 + ( 0 .3 3 ) P 2R

Page 58: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Terrain Collision Detection:Locate Point on Triangle

Calculate barycentric coordinates for point Q in a triangle’s plane

If any of the weights (w0, w1, w2) are negative, then the point Q does not lie in the triangle

2

1

2121

212

22

212

22

12

1 1

VS

VS

VV

VV

VV V

V

VVw

w

022

011

0

PPV

PPV

PQS

210 1 www

Page 59: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Collision Resolution:Examples

Two billiard balls strike Calculate ball positions at time of impact Impart new velocities on balls Play “clinking” sound effect

Rocket slams into wall Rocket disappears Explosion spawned and explosion sound effect Wall charred and area damage inflicted on nearby

characters

Character walks through wall Magical sound effect triggered No trajectories or velocities affected

Page 60: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Collision Resolution:Parts

Resolution has three parts1. Prologue

2. Collision

3. Epilogue

Page 61: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Prologue

Collision known to have occurred Check if collision should be ignored Other events might be triggered

Sound effects Send collision notification messages

Page 62: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Collision

Place objects at point of impact Assign new velocities

Using physics or Using some other decision logic

Page 63: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Epilogue

Propagate post-collision effects Possible effects

Destroy one or both objects Play sound effect Inflict damage

Many effects can be done either in the prologue or epilogue

Page 64: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Collision Resolution:Resolving Overlap Testing

1. Extract collision normal

2. Extract penetration depth

3. Move the two objects apart

4. Compute new velocities

Page 65: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Extract Collision Normal

Find position of objects before impact Use two closest points to construct the

collision normal vector

Page 66: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Extract Collision Normal for Sphere Collision

Sphere collision normal vector Difference between centers at point of collision

t0

t0

t0 . 2 5

t0 . 5

t0 . 2 5

t0 . 5

t0 . 7 5

t0 . 7 5

t1

t1

C o llis io n

N o rm a l

Page 67: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Depth

Gilbert-Johnson-keerthi (GJK) bisection to extract the depth of convex objects

Minkowski supporting lines

Page 68: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Collision Resolution:Resolving Intersection Testing

Simpler than resolving overlap testing No need to find penetration depth or

move objects apart Simply

1. Extract collision normal

2. Compute new velocities

Page 69: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Collision Response

Why? Performed to keep objects from interpenetrating To ensure behavior similar to real-world objects

Two Basic Approaches Approach 1: Instantaneous change of velocity at time of

collision Approach 2: Gradual change of velocity and position over

time, following collision

Page 70: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Approach 1

Approach 1: Instantaneous change of velocity at time of collision Benefits:

Visually the objects never interpenetrate Result is generated via closed-form equations, and is

perfectly stable

Difficulties: Precise detection of time and location of collision can

be prohibitively expensive (frame rate killer) Logic to manage state is complex

Page 71: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Approaches 2

Approach 2: Gradual change of velocity and position over time, following collision Benefits

Does not require precise detection of time and location of collision

State management is easy Potential to be more realistic, if meshes are adjusted to

deform according to predicted interpenetration Difficulties

Object interpenetration is likely, and parameters must be tweaked to manage this

Simulation can be subject to numerical instabilities, often requiring the use of implicit finite difference methods

Page 72: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Final Comments

Instantaneous Collision Response Classical approach: Impulse-momentum equations

See text for full details Gradual Collision Response

Classical approach: Penalty force methods Resolve interpenetration over the course of a few integration

steps Penalty forces can wreak havoc on numerical integration

Instabilities galore Implicit finite difference equations can handle it

But more difficult to code Geometric approach: Ignore physical response equations

Enforce purely geometric constraints once interpenetration has occurred

Page 73: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Final Comments

Simple Games Closed-form particle equations may be all you

need Numerical particle simulation adds flexibility

without much coding effort Collision detection is probably the most difficult

part of this Generalized Rigid Body Simulation

Includes rotational effects and interesting (non-constant) forces

See text for details on how to get started

Page 74: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Final Comments

Full-Up Simulation The text and this presentation just barely touch the

surface Additional considerations

Multiple simultaneous collision points Articulating rigid body chains, with joints Friction, rolling friction, friction during collision Mechanically applied forces (motors, etc.) Resting contact/stacking Breakable objects Soft bodies Smoke, clouds, and other gases Water, oil, and other fluids

Page 75: CSCE 590E Spring 2007 Collision Detection By Jijun Tang

Homework 4

Page 390 problem 1 and 5 Handwriting or printing using Word/Lat

ex Turn in before class No need to upload to dropbox

5 points total (2.5 each) Due on this Wednesday