physics for games programmers tutorial motion and collision – its all relative squirrel eiserloh...

139
Physics for Games Programmers Tutorial Motion and Collision – It’s All Relative Squirrel Eiserloh [email protected] Lead Programmer Ritual Entertainment www.ritual.com www.algds.org

Upload: jordan-macpherson

Post on 26-Mar-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

Physics for Games Programmers Tutorial

Motion and Collision – It’s All Relative

Squirrel [email protected]

Lead ProgrammerRitual Entertainment

www.ritual.comwww.algds.org

Page 2: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

2

Takeaway A comfortable, intuitive understanding

of:

The Problems of Discrete Simulation Continuous Collision Detection Applying Relativity to Game Physics Configuration Space Collisions in Four Dimensions The Problems of Rotation Why this is all really important even if you’re

doing simple cheesy 2d games at home in your underwear in your spare time

Page 3: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

3

The Problem Discrete physics simulation falls

embarrassingly short of reality. “Real” physics is prohibitively

expensive... ...so we cheat. We need to cheat enough to be able to

run in real time. We need to not cheat so much that

things break in a jarring and unrecoverable way.

Much of the challenge is knowing how and when to cheat.

Page 4: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

4

Overview

Simulation Tunneling Movement Bounds Swept Shapes Einstein Says... Minkowski Says... Rotation

Page 5: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

5

Also, I promise...

No math

Page 6: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

Simulation(Sucks)

Page 7: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

7

Problems with Simulation

Flipbook syndrome

Page 8: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

8

Problems with Simulation

Flipbook syndrome Things can happen

in-between snapshots

Page 9: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

9

Problems with Simulation

Flipbook syndrome Things mostly

happen in-between snapshots

Page 10: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

10

Problems with Simulation

Flipbook syndrome Things mostly

happen in-between snapshots

Curved trajectories treated as piecewise linear

Page 11: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

11

Problems with Simulation

Flipbook syndrome Things mostly

happen in-between snapshots

Curved trajectories treated as piecewise linear

Terms often assumed to be constant throughout the frame

Page 12: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

12

Problems with Simulation

Flipbook syndrome Things mostly happen

in-between snapshots Curved trajectories

treated as piecewise linear

Terms often assumed to be constant throughout the frame

Error accumulates

Page 13: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

13

Problems with Simulation (cont’d)

Rotations are often assumed to happen instantaneously at frame boundaries

Page 14: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

14

Problems with Simulation (cont’d)

Rotations are often assumed to happen instantaneously at frame boundaries

Energy is not always conserved Energy loss can be

undesirable Energy gain is evil

Simulations explode!

Page 15: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

15

Problems with Simulation (cont’d)

Rotations are often assumed to happen instantaneously at frame boundaries

Energy is not always conserved Energy loss can be

undesirable Energy gain is evil

Simulations explode!

Tunneling (Also evil!)

Page 16: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

16

Overlapping Objects

Question #1: Do A and B overlap?

Plenty of reference material to help solve this, but...

...this is often the wrong question to ask (begs tunneling).

Page 17: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

Tunneling(Sucks)

Page 18: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

18

Tunneling

Small objects tunnel more easily

Page 19: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

19

Tunneling (cont’d)

Possible solutions Minimum size requirement?

Inadequate; fast objects still tunnel

Page 20: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

20

Tunneling (cont’d)

Fast-moving objects tunnel more easily

Page 21: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

21

Tunneling (cont’d)

Possible solutions Minimum size requirement?

Inadequate; fast objects still tunnel Maximum speed limit?

Inadequate; since speed limit is a function of object size, this would mean small & fast objects (bullets) would not be allowed

Smaller time step? Helpful, but inadequate; this is essentially the

same as a speed limit

Page 22: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

22

Tunneling (cont’d)

Besides, even with min. size requirements and speed limits and a small timestep, you still have degenerate cases that cause tunneling

Page 23: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

23

Tunneling (cont’d)

Tunneling is very, very bad – this is not a “mundane detail” Things falling through world Bullets passing through people or walls Players getting places they shouldn’t Players missing a trigger boundary

Okay, so tunneling really sucks. What can we do about it?

Page 24: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

Movement Bounds

Page 25: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

25

Movement Bounds

Disc / Sphere

Page 26: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

26

Movement Bounds

Disc / Sphere

AABB (Axis-Aligned Bounding Box)

Page 27: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

27

Movement Bounds

Disc / Sphere

AABB (Axis-Aligned Bounding Box)

OBB (Oriented Bounding Box)

Page 28: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

28

Movement Bounds

Question #2: Could A and B have collided during the frame?

Better than Question #1 (solves tunneling!), but...

Page 29: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

29

Movement Bounds

Question #2: Could A and B have collided during the frame?

Better than Question #1 (solves tunneling!), but...

...even if the answer is “yes”, we still don’t know for sure (false positives).

Page 30: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

30

Movement Bounds

Conclusion Good: They prevent tunneling! (i.e. no

false negatives)

Bad: They don’t actually tell us whether A and B collided (still have false positives).

Good: They can be used as a cheap, effective early rejection test.

Page 31: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

Swept Shapes

Page 32: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

32

Swept Shapes

Swept disc / sphere (n-sphere): capsule

Page 33: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

33

Swept Shapes

Swept disc / sphere (n-sphere): capsule

Swept AABB: convex polytope (polygon in 2d, polyhedron in 3d)

Page 34: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

34

Swept Shapes

Swept disc / sphere (n-sphere): capsule

Swept AABB: convex polytope (polygon in 2d, polyhedron in 3d)

Swept triangle / tetrahedron (simplex): convex polytope

Page 35: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

35

Swept Shapes

Swept disc / sphere (n-sphere): capsule

Swept AABB: convex polytope (polygon in 2d, polyhedron in 3d)

Swept triangle / tetrahedron (simplex): convex polytope

Swept polytope: convex polytope

Page 36: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

36

Swept Shapes (cont’d)

Like movement bounds, only with a perfect fit!

Page 37: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

37

Swept Shapes (cont’d)

Like movement bounds, only with a perfect fit!

Still no false negatives (tunneling).

Page 38: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

38

Swept Shapes (cont’d)

Like movement bounds, only with a perfect fit!

Still no false negatives (tunneling).

Finally, no false positives, either!

Page 39: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

39

Swept Shapes (cont’d)

Like movement bounds, only with a perfect fit!

Still no false negatives (tunneling).

Finally, no false positives, either!

No, wait, nevermind. Still have ‘em. Rats.

Page 40: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

40

Swept Shapes (cont’d)

Conclusion Suck? Can be used as early rejection test, but... ...movement bounds are better for that. If you’re not too picky... ...they DO solve a large number of nasty

problems (especially tunneling) ...and can serve as a poor man’s

continuous collision detection for a basic engine.

Page 41: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

41

Page 42: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

42

Einstein Says...

Coordinate systems are relative

Page 43: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

Relative Coordinate Systems

Page 44: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

44

Relative Coordinate Systems

World coordinates

Page 45: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

45

Relative Coordinate Systems

World coordinates A’s local

coordinates

Page 46: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

46

Relative Coordinate Systems

World coordinates A’s local

coordinates B’s local

coordinates

Page 47: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

47

Relative Coordinate Systems

x2 + y2 = r2(x-h)2 + (y-k)2 = r2

Math is often nicer at the origin.

Page 48: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

48

Einstein Says...

Coordinate systems are relative Motion is relative

Page 49: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

Relative Motion

Page 50: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

50

Relative Motion

"Frames of Reference"

World frame

Page 51: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

51

Relative Motion

"Frames of Reference"

World frame A's frame

Page 52: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

52

Relative Motion

"Frames of Reference"

World frame A's frame B's frame

Page 53: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

53

Relative Motion

"Frames of Reference"

World frame A's frame B's frame Inertial frame

Page 54: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

54

Relative Motion

A Rule of Relativistic Collision Detection:

It is always possible to reduce a collision check between two moving objects to a collision check between a moving object and a stationary object (by reframing)

Page 55: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

55

(Does Not Suck)

Page 56: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

Relative Collision Bodies

Page 57: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

57

Relative Collision Bodies

Collision check equivalencies (disc)

Page 58: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

58

Relative Collision Bodies

Collision check equivalencies (disc)

...AABB

Page 59: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

59

Relative Collision Bodies

Collision check equivalencies (disc)

...AABB Can even reduce

one body to a singularity

Page 60: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

60

Relative Collision Bodies

Collision check equivalencies (disc)

...AABB Can even reduce

one body to a singularity

“Tracing” or “Rubbing” collision bodies together

Page 61: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

61

Relative Collision Bodies

Collision check equivalencies (disc)

...AABB Can even reduce one

body to a singularity “Tracing” or

“Rubbing” collision bodies together

Spirograph-out the reduced body’s origin

Page 62: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

62

Relative Collision Bodies (cont’d)

Disc + disc

Page 63: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

63

Relative Collision Bodies (cont’d)

Disc + disc AABB + AABB

Page 64: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

64

Relative Collision Bodies (cont’d)

Disc + disc AABB + AABB Triangle + AABB

Page 65: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

65

Relative Collision Bodies (cont’d)

Disc + disc AABB + AABB Triangle + AABB AABB + triangle

Page 66: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

66

Relative Collision Bodies (cont’d)

Disc + disc AABB + AABB Triangle + AABB AABB + triangle Polytope +

polytope

Page 67: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

67

Relative Collision Bodies (cont’d)

Disc + disc AABB + AABB Triangle + AABB AABB + triangle Polytope +

polytope Polytope + disc

Page 68: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

68

Relative Collision Bodies (cont’d)

Things start to get messy when combining bodies explicitly / manually.

(Especially in 3d.) General solution?

Page 69: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

Minkowski Arithmetic

Page 70: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

70

Minkowski Sums

The Minkowski Sum (A+B) of A and B is the result of adding every point in A to every point in B.

Page 71: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

71

Minkowski Sums

The Minkowski Sum (A+B) of A and B is the result of adding every point in A to every point in B.

Minkowski Sums are commutative:A+B = B+A

Minkowski Sum of convex objects is convex

Page 72: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

72

Minkowski Differences

The Minkowski Difference (A-B) of A and B is the result of subtracting every point in B from every point in A (or A + -B)

Page 73: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

73

Minkowski Differences

The Minkowski Difference (A-B) of A and B is the result of subtracting every point in B from every point in A

Resulting shape is different from A+B.

Page 74: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

74

Minkowski Differences (cont’d)

Minkowski Differences are not commutative:A-B != B-A

Minkowski Difference of convex objects is convex (since A-B = A+ -B)

Page 75: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

75

Minkowski Differences (cont’d)

Minkowski Differences are not commutative:A-B != B-A

Minkowski Difference of convex objects is convex (since A-B = A+ -B)

Minkowski Difference produces the same shape as “Spirograph”

Page 76: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

76

Minkowski Differences (cont’d)

If the singularity is outside the combined body, A and B do not overlap.

Page 77: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

77

Minkowski Differences (cont’d)

If the singularity is outside the combined body, A and B do not overlap.

If the singularity is inside the combined body (A-B), then A and B overlap.

Page 78: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

78

Minkowski Differences (cont’d)

Aorigin vs. Borigin

-Borigin -Borigin

___ ___(A-B)origin vs. 0

Page 79: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

79

Minkowski Differences (cont’d)

In world space, A-B is “near” the origin

Page 80: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

80

Minkowski Differences (cont’d)

Since the singularity point is always at the origin (B-B), we can say...

If (A-B) does not contain the origin, A and B do not overlap.

Page 81: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

81

Minkowski Differences (cont’d)

If (A-B) contains the origin, A and B overlap.

In other words, we reduce A vs. B to:

combined body (A-B) vs.point (B-B, or origin)

Page 82: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

82

Minkowski Differences (cont’d)

If A and B are in the same coordinate system, the comparison between A-B and the origin is said to happen in configuration space

...in which case A-B is said to be a configuration space obstacle (CSO)

Page 83: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

83

Minkowski Differences (cont’d)

Translations in A or B simply translate the CSO

Page 84: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

84

Minkowski Differences (cont’d)

Rotations in A or B mutate the CSO

Page 85: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

85

Minkowski Sum vs. Difference

Lots of confusion over Minkowski “Sum” vs. “Difference”.

Sum is used to “fatten” an object by “adding” another object (in local coordinates) to it

Difference is used to put the objects in configuration space, i.e. A-B vs. origin.

Difference sometimes called Sum since A-B can be expressed as A+(-B)!

Page 86: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

86

Minkowski Sum vs. Difference (cont’d)

Difference is the same as “Spirograph” or “rubbing”

Difference is not commutative! A-B != B-A

Difference and sum produce different-shaped results

Difference produces CSO (configuration space obstacle)

Page 87: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

87

(Does Not Suck)

Page 88: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

Relative Everything

Page 89: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

89

Relative Everything

Let’s combine: Relative Coordinate Systems Relative Motion Relative Collision Bodies

Page 90: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

90

Relative Everything (cont’d)

A vs. B in world frame

Page 91: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

91

Relative Everything (cont’d)

A vs. B in world frame

A vs. B, inertial frame

Page 92: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

92

Relative Everything (cont’d)

A vs. B in world frame

A vs. B, inertial frame

A is moving, B is still

Page 93: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

93

Relative Everything (cont’d)

A vs. B in world frame

A vs. B, inertial frame

A is moving, B is still

A is CSO, B is point

Page 94: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

94

Relative Everything (cont’d)

A vs. B in world frame

A vs. B, inertial frame

A is moving, B is still

A is CSO, B is point A is moving CSO, B

is still point

Page 95: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

95

Relative Everything (cont’d)

A vs. B in world frame

A vs. B, inertial frame

A is moving, B is still A is CSO, B is point A is moving CSO, B

is still point A is still CSO, B is

moving point

Page 96: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

96

Relative Everything (cont’d)

Question #3: Did A and B collide during the frame?

Yes! We can now get an exact answer.

No false negatives, no false positives!

However, we still don’t know WHEN they collided...

Page 97: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

97

Relative Everything (cont’d)

Why does the exact collision time matter? Outcomes can be

different Order of events (e.g.

multiple collisions) is relevant

Collision response is easier when you can reconstruct the exact moment of impact

Page 98: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

98

Relative Everything (cont’d)

The Minkowski Difference (A-B) / CSO can also be thought of as “the set of all translations [from the origin] that would cause a collision”.

A.K.A. the set of “inadmissible translations”.

Page 99: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

Determining Collision Time

Page 100: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

100

Determining Collision Time

Method #1: Frame Subdivision

Page 101: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

101

Subdividing Movement Frame

If a swept-shape (or movement bounds) test says “yes”:

Page 102: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

102

Subdividing Movement Frame

If a swept-shape (or movement bounds) test says “yes”:

Cut the frame in half; perform two separate tests (first half first, second half second).

First positive test is when the collision occurred.

Page 103: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

103

Subdividing Movement Frame (cont’d)

Can recurse (1/2, 1/4, 1/8...) to the desired level of granularity

Page 104: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

104

Subdividing Movement Frame (cont’d)

Can recurse (1/2, 1/4, 1/8...) to the desired level of granularity

If both tests negative, no collision (was a false positive).

Still inexact (minimizing, not eliminating, false positives)

Gets expensive

Page 105: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

105

Determining Collision Time

Method #1: Frame Subdivision Method #2: 4D* Continuous Collision

Detection *(N+1 dimensions; 3D for 2D physics,

etc.)

Page 106: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

Spacetime

Page 107: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

107

Spacetime

Spacetime is a Physics construct which combines N-dimensional space with an extra dimension for time, yielding a unified model with N+1 dimensions.

Space (1D) + time (1D) = spacetime (2D) Space (2D) + time (1D) = spacetime (3D) Space (3D) + time (1D) = spacetime (4D)

Page 108: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

108

Spacetime Diagrams

1D space + time = 2D Just an X vs. T

graph!

Page 109: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

109

Spacetime Diagrams

1D space + time = 2D Just an X vs. T

graph! 2D space + time =

3D No problem.

Page 110: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

110

Spacetime Diagrams

1D space + time = 2D Just an X vs. T

graph! 2D space + time =

3D No problem.

Another example (2d space + time = 3D)

Page 111: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

111

Spacetime Diagrams

1D space + time = 2D Just an X vs. T graph!

2D space + time = 3D No problem.

Another example (2d space + time = 3D)

3D space + time = 4D Brainbuster!

?

Page 112: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

112

Spacetime Diagrams (cont’d)

Note that an N-dimensional system in motion is the same as a still snapshot in N+1 dimensions

1D animation = 2D spacetime still image 2D animation = 3D spacetime still image 3D animation = 4D spacetime still image

Page 113: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

113

Spacetime Diagrams (cont’d)

2D spacetime still image

1D animation

Page 114: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

114

Spacetime Diagrams (cont’d)

3D spacetime still image

2D animation

Page 115: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

115

Spacetime Diagrams (cont’d)

How do you envision a 4D object?

Use 2D animation -> 3D spacetime diagram as a mental analogy.

Fun reading: Flatland by Edwin Abbott

Think about a 4D object that you’re already familiar with.

(The universe in motion!)

Page 116: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

116

Spacetime Diagrams (cont’d)

Invented by Hermann Minkowski Also called “Minkowski Diagrams”

Page 117: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

117

(Rules)

Page 118: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

Time-Swept Shapes

Page 119: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

119

Time-Swept Shapes

Sweep out shapes, but do it over time in a spacetime diagram

Define time over frame as being in the interval [0,1]

As before, we can play around with lots of relativistic variations:

Page 120: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

120

Time-Swept Shapes (cont’d)

A vs. B in world frame

Page 121: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

121

Time-Swept Shapes (cont’d)

A vs. B in world frame

A is moving, B is still

Page 122: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

122

Time-Swept Shapes (cont’d)

A vs. B in world frame

A is moving, B is still

A is CSO, B is point

Page 123: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

123

Time-Swept Shapes (cont’d)

A vs. B in world frame

A is moving, B is still

A is CSO, B is point A is still CSO, B is

moving (swept) point

Page 124: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

124

Time-Swept Shapes (cont’d)

To solve for collision time, we intersect the point-swept ray against the CSO

The ‘t’ coordinate at the intersection point is the time [0,1] of collision

Collision check is done in N+1 dimensions

Which means, in a 3D game, we collide a 4D ray vs. a 4D body! (What?)

Page 125: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

125

Time-Swept Shapes (cont’d)

Wait, it gets easier... When we view this

diagram (CSO vs moving point) down the time axis, i.e. from “overhead”:

Since CSO is not moving, it looks 2D from overhead...

Page 126: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

126

Time-Swept Shapes (cont’d)

We can reduce this back down to N dimensions (from N+1) since we are looking down the time axis!

So it becomes an N-dimesional ray vs. N-dimensional body again.

Which means, in a 3D game, we collide a 3D ray vs. a 3D body.

Page 127: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

127

Page 128: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

128

Time-Swept Shapes (cont’d)

Question #4: When, during the frame, did A and B collide?

Finally, the right question - and we have a complete answer!

With fixed cost, and with exact results (no false anything).

Page 129: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

129

Time-Swept Shapes (cont’d)

BTW, this is essentially the same as solving for the fraction of the singularity-translation ray from our original Minkowski Difference “inadmissible translations” picture!

Page 130: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

Quality vs. Quantity

or

“You Get What You Pay For”

Page 131: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

131

Quality vs. Quantity

The more you ask, the more you pay. Question #1: Do A and B overlap? Question #2: Could A and B have

collided during the frame? Question #3: Did A and B collide

during the frame? Question #4: When, during the

frame, did A and B collide?

Page 132: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

Rotations(Suck)

Page 133: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

133

Rotations

Continuous rotational collision detection sucks

Rotational tunneling alone is problematic

Page 134: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

134

Rotations

Continuous rotational collision detection sucks

Rotational tunneling alone is problematic

Methods we’ve discussed here often don’t work on rotations, or their rotational analogue is quite complex

Page 135: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

135

Rotations (cont’d)

However: Rotational tunneling is usually not as

jarring as translational tunneling Rotational speed limits are actually

feasible Can do linear approximations of swept

rotations Can use bounding shapes to contain pre-

and post-rotated positions This is something that many engines

never solve robustly

Page 136: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

Summary

Page 137: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

137

Summary The nature of simulation causes us real

problems... problems which can’t be ignored. Have to worry about false negatives (tunneling!)

as well as false positives. Knowing when a collision event took place can

be very important (especially when resolving it). Sometimes a problem (and math) looks easier

when we look at it from a different viewpoint. Can combine bodies in cheaty ways to simplify

things even further.

Page 138: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

138

Summary (cont’d)

Einstein and Minkowski are cool. Rotations suck. Doing real-time collision detection in

4D spacetime doesn’t have to be hard.

Or expensive. Or confusing.

Page 139: Physics for Games Programmers Tutorial Motion and Collision – Its All Relative Squirrel Eiserloh squirrel@eiserloh.net Lead Programmer Ritual Entertainment

139

Questions?

Feel free to reach me by email at:

[email protected] or

[email protected]