38
Chapter 4.2 Collision Detection and Resolution

Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

Chapter 4.2Collision Detection and Resolution

Page 2: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

2

Collision Detection

Complicated for two reasons

1. Geometry is typically very complex,

potentially requiring expensive testing

2. Naïve solution is O(n2) time complexity,

since every object can potentially collide

with every other object

Page 3: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

Collision Detection

Two basic techniques

1. Overlap testing

Detects whether a collision has already

occurred

2. Intersection testing

Predicts whether a collision will occur in the

future

Page 4: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

4

Overlap Testing

Facts

Most common technique used in games

Exhibits more error than intersection testing

Concept

For every simulation step, test every pair of objects to see if they overlap

Easy for simple volumes like spheres, harder for polygonal models

Page 5: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

5

Overlap Testing:

Useful Results

Useful results of detected collision

Time collision took place

Collision normal vector

Page 6: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

6

Overlap Testing:

Collision Time

Collision time calculated by moving object

back in time until right before collision

Bisection is an effective technique

B B

t1

t0.375

t0.25

B

t0

Iteration 1

Forward 1/2

Iteration 2

Backward 1/4

Iteration 3

Forward 1/8

Iteration 4

Forward 1/16

Iteration 5

Backward 1/32

Initial Overlap

Test

t0.5t0.4375 t0.40625

BB B

A

A

A

AA A

Page 7: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

7

Overlap Testing:

Limitations

Fails with objects that move too fast

Unlikely to catch time slice during overlap

Possible solutions

Design constraint on speed of objects

Reduce simulation step size

t0t-1 t1 t2

bullet

window

Page 8: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

8

Intersection Testing

Predict future collisions

When predicted:

Move simulation to time of collision

Resolve collision

Simulate remaining time step

Page 9: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

9

Intersection Testing:

Swept Geometry

Extrude geometry in direction of movement

Swept sphere turns into a “capsule” shape

t0

t1

Page 10: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

Reminder about nomenclature

A (bolded variables are vectors)

A (italicized variables are scalars)

In cases where the name is the same, the scalar is

the magnitude of the Vector (Pythagoras).

Page 11: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

11

Intersection Testing:

Special Case, Sphere-Sphere

Q1

Q2

P1

P2

P

Q

t=0

t=0

t=1

t=1

t

,

2

2222

B

rrΑBt

QPBAΒΑ

.QQPPB

QPA

1212

11

Page 12: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

12

Intersection Testing:

Sphere-Sphere Collision

Smallest distance ever separating two

spheres:

If

there is a collision

2

2

22

B

BAA=d

22

QP rrd

Page 13: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

13

Intersection Testing:

Limitations

More costly then object overlap

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 14: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

14

Dealing with Complexity

Two issues

1. Complex geometry must be simplified

2. Reduce number of object pair tests

Page 15: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

15

Dealing with Complexity:

Simplified Geometry

Approximate complex objects with

simpler geometry, like this ellipsoid

Or multiple spheres

Page 16: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

16

Dealing with Complexity:

Minkowski Sum

Two complex shapes might take

dozens of test to determine if they

overlap.

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 17: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

17

Dealing with Complexity:

Minkowski Sum

Y}B and :{ XABAYX

XY =YX XY =

Page 18: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

18

Dealing with Complexity:

Minkowski Sum

t0

t1

t0

t1

Page 19: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

19

Dealing with Complexity:

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 20: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

20

Dealing with Complexity:

Box Bounding Volumes

Axis-Aligned Bounding Box Oriented Bounding Box

Page 21: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

21

Dealing with Complexity:

Achieving O(n) Time Complexity

One solution is to partition space

Game Entities – Identification (Hash Maps)

UID's allow multiple different lists or data structure over same object set.

Observer model (objects could register their current quadrant with CD object)

Page 22: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

Dealing with Complexity:

Achieving O(n) Time Complexity

Another solution is the plane sweep algorithm

C

B

R

A

x

y

A0 A1 R0 B0 R1C0 C1B1

B0

B1

A1

A0

R1

R0

C1

C0

1. Find bounds in the X,

Y and Z planes.

2. Add values to

appropriate lists.

3. Lists are sorted initially

with quicksort

Θ(n(log(n))

4. Object coherence says

that objects from

frame to frame won't

move much.

5. Use bubblesort to do

fast update Θ(n).

Page 23: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

Terrain Collision Detection:

Height Field Landscape

Polygonal mesh with/without height field

23

Top-Down View

Perspective View

Top-Down View (heights added)

Perspective View (heights added)

Page 24: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

Terrain Collision Detection:

Locate Triangle on Height Field

Q is the heel of the foot of the character.

With triangle located determine height.24

Q

R

Q

Qz >Qx

Qz <=Qx

z

x

Rz > 1 -Rx

Rz <= 1 -Rx

R

Page 25: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

Flashback

Remember:

Dot product of two perpendicular vectors is 0.

Cross product of two vectors is a vector perpendicular

to the other two vectors.

Planes in 3D

Given a 3D point P<x,y,x> and a point N<A,B,C> we

can define a plane Q as the set of all points Q such

that the line from P to Q is perpendicular to the line

from P to N.

cos V W V W

Page 26: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

Definition of a plane restated

Definition of a plane:

The set of points Q such that:

(N – P ) ∙ (Q – P) = 0

Note: We commonly reduce N to a distance vector

and when w do the equation becomes:

N ∙ (Q – P) = 0

Your book persists in calling N a normal vector, which

would only make sense if the plane is already

defined.

Page 27: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

27

Terrain Collision Detection:

Locate Point on Triangle

Plane equation:

A, B, C are the x, y, z components of the

plane’s normal vector

Where

with one of the triangles

vertices being

Giving:

0 DCzByAx

0PND

0P

00 PNNNN zyx zyx

Page 28: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

28

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

zzxx

yN

PNQNQNQ 0

Page 29: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

29

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 30: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

30

Collision Resolution:

Parts

Resolution has three parts

1. Prologue

2. Collision

3. Epilogue

Page 31: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

31

Prologue

Collision known to have occurred

Check if collision should be ignored

Other events might be triggered

Sound effects

Send collision notification messages

Page 32: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

32

Collision

Place objects at point of impact

Assign new velocities

Using physics

Vector mathematics

Using some other decision logic

Page 33: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

33

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 34: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

34

Collision Resolution:

Resolving Overlap Testing

1. Extract collision normal

2. Extract penetration depth

3. Move the two objects apart

4. Compute new velocities

Page 35: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

35

Collision Resolution:

Extract Collision Normal

Find position of objects before impact

Use two closest points to construct the

collision normal vector

Page 36: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

36

Collision Resolution:

Extract Collision Normal

Sphere collision normal vector

Difference between centers at point of collision

t0

t0

t0.25

t0.5

t0.25

t0.5

t0.75

t0.75

t1

t1

Collision Normal

Page 37: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

37

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 38: Chapter 4.2 Collision Detection and Resolutionmeyer/CISC3600/Materials/4_2Collision... · Forward 1/8 Iteration 4 Forward 1/16 Iteration 5 Backward 1/32 Initial Overlap Test t 0.5

The End