Top Banner
1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn
52
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

1

Computer Game Physics

CIS 487/587

Bruce R. Maxim

UM-Dearborn

Page 2: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

2

Game Physics

• Not trying to build a perfect physical model• Most things can be approximated assuming

Newtonian physics and rigid bodies• Use discrete simulation (constant step)

techniques• Just worry about center of mass for most

things

Page 3: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

3

The next 6 slides come from the Rabin text

Page 4: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

4

Why Physics?

• The Human Experience– Real-world motions are physically-based– Physics can make simulated game worlds appear

more natural– Makes sense to strive for physically-realistic

motion for some types of games

• Emergent Behavior– Physics simulation can enable a richer gaming

experience

Page 5: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

5

Why Physics?

• Developer/Publisher Cost Savings– Classic approaches to creating realistic motion:

• Artist-created keyframe animations• Motion capture• Both are labor intensive and expensive

– Physics simulation:• Motion generated by algorithm• Theoretically requires only minimal artist input• Potential to substantially reduce content development

cost

Page 6: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

6

High-level Decisions

• Physics in Digital Content Creation Software:– Many DCC modeling tools provide physics– Export physics-engine-generated animation as

keyframe data– Enables incorporation of physics into game

engines that do not support real-time physics– Straightforward update of existing asset creation

pipelines– Does not provide player with the same emergent-

behavior-rich game experience– Does not provide full cost savings to

developer/publisher

Page 7: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

7

High-level Decisions

• 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– 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 8: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

8

High-level Decisions

• License vs. Build Physics Engine:– License middleware physics engine

• Complete solution from day 1• Proven, robust code base (in theory)• Most offer some integration with DCC tools• Features are always a tradeoff

Page 9: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

9

High-level Decisions

• License vs. Build Physics Engine:– Build physics engine in-house

• Choose only the features you need• Opportunity for more game-specific optimizations• Greater opportunity to innovate• Cost can be easily be much greater• No asset pipeline at start of development

Page 10: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

10

Position and Velocity

• Where is object at time t (using pixels)?

• Equationsplayer_x(t) = t * x_velocity + x_initial

player_y(t) = t * y_velocity + y_initial

• Computationplayer_x = player_x + x_velocity

player_y = player_y + y_velocity

Page 11: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

11

Acceleration

• Computationx_velocity = x_velocity + x_acceleration

y_velocity = y_velocity + y_acceleration

• Use piecewise linear approximation to continuous functions

Page 12: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

12

Gravity

• Force of attraction between objectsF = G * (M1 * M2) / D2

G = gravitational constantD = distance between objects

• Free falling objects on Earth– Equation

V(t) = 1/2 * g * t2

g = 9.8 m/sec2

– Computationx_velocity = x_velocity + 0y_velocity = y_velocity + gravity

Page 13: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

13

Projectile Motion

• X = x + Vx + W

• Y = y + Vy

• Vxi = cos(A) * Vi

• Vyi = sin(A) * Vi

• Vx = Vx - WR(Vx)

• Vy - WR(Vy) + G

• W = wind• A = inclination angle

• Vi = initial velocity

• WR = wind resistance

• G = gravity

Page 14: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

14

Friction

• Conversion of kinetic energy into heat• Equation

– Frictional Force = C * G * M• C = force required to maintain constant speed • G = gravity• M = mass

• Computationwhile (velocity > 0)

velocity = velocity - friction

Page 15: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

15

The next 21 slides come from the Rabin text

Page 16: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

16

Collision Detection

Complicated for two reasons1. 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 17: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

Collision Detection

Two basic techniques1. Overlap testing

• Detects whether a collision has already occurred

2. Intersection testing• Predicts whether a collision will occur in the

future

Page 18: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

18

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 19: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

19

Overlap Testing:Useful Results

• Useful results of detected collision– Time collision took place– Collision normal vector

Page 20: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

20

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

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 21: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

21

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

b u lle t

w in d o w

Page 22: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

22

Intersection Testing

• Predict future collisions

• When predicted:– Move simulation to time of collision– Resolve collision– Simulate remaining time step

Page 23: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

23

Intersection Testing:Swept Geometry

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

t0

t1

Page 24: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

24

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

Page 25: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

25

Intersection Testing:Sphere-Sphere Collision

• Smallest distance ever separating two spheres:

• If

there is a collision

2

222

BAd

BA

22QP rrd

Page 26: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

26

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 27: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

27

Dealing with Complexity

Two issues1. Complex geometry must be simplified

2. Reduce number of object pair tests

Page 28: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

28

Dealing with Complexity:Simplified Geometry

• Approximate complex objects with simpler geometry, like this ellipsoid

Page 29: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

29

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 30: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

30

Dealing with Complexity: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 31: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

31

Dealing with Complexity:Achieving O(n) Time Complexity

One solution is to partition space

Page 32: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

32

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 33: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

33

Collision Resolution:Parts

• Resolution has three parts1. Prologue

2. Collision

3. Epilogue

Page 34: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

34

Collision Resolution:Prologue

• Collision known to have occurred

• Check if collision should be ignored

• Other events might be triggered– Sound effects– Send collision notification messages

Page 35: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

35

Collision Resolution:Collision

• Place objects at point of impact

• Assign new velocities– Using physics or– Using some other decision logic

Page 36: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

36

Collision Resolution: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 37: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

37

Collision Resolution:Resolving Overlap Testing

1. Extract collision normal

2. Extract penetration depth

3. Move the two objects apart

4. Compute new velocities

Page 38: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

38

Collision Resolution:Extract Collision Normal

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

collision normal vector

Page 39: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

39

Collision Resolution:Extract Collision Normal

• 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 40: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

40

Collision Resolution:Resolving Intersection Testing• Simpler than resolving overlap testing

– No need to find penetration depth or move objects apart

• Simply1. Extract collision normal

2. Compute new velocities

Page 41: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

41

Simple Collision Handling • Detect that collision has occurred

(bounding box)

• Determine the time of the collision (may need to back up to point of collision)

• Determine where objects are when they touch• Determine the collision normal

(angle of incidence = angle of reflection)

• Determine the velocity vectors after the collision• Determine any changes in motion

Page 42: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

42

Simple Kinematics

• Forward kinematic problem– find position of P from

theta1, theta2, L1, L2– use the 2D translation

and rotation matrices

– (TL2*Rtheta2)* (TL1*Rtheta1)

– generalizes to any number of links

P(x, y)

theta1

theta2

L1

L2

Page 43: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

43

Particle System Explosions

• Start with lots of small objects (1 to 4 pixels)• Initialize particles with random velocities

based on velocity of exploding object• Apply gravity• Transform color intensity as a function of time• Destroy objects when they collide or after a

fixed amount of time

Page 44: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

44

The next 7 slides come from the Rabin text

Page 45: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

45

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 46: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

46

Generalized Rigid Bodies – Simulation

• Angular Kinematics– Orientation, 3x3 matrix R or quaternion, q– Angular velocity, – As with translational/particle kinematics, all properties are

measured in world coordinates• Additional Object Properties

– Inertia tensor, J– Center-of-mass

• Additional State Properties for Simulation– Orientation– Angular momentum, L=J– Corresponding state derivatives

Page 47: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

47

Generalized Rigid Bodies - Simulation

• Torque– Analogous to a force– Causes rotational acceleration

• Cause a change in angular momentum

– Torque is the result of a force (friction, collision response, spring, damper, etc.)

rF

P

= C en ter-o f-M ass

= r F

Page 48: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

48

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• 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 49: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

49

Collision Response

• Two Basic Approaches (continued)– 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 50: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

50

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 51: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

51

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

Page 52: 1 Computer Game Physics CIS 487/587 Bruce R. Maxim UM-Dearborn.

52

Time-Based Modeling

• Time t is used in all kinematic equations that move objects (to avoid discontinuities caused by “slower” frame rates)

• This involves scaling dx and dy based on elapsed time (rather than a virtual clock)

• This allow constant movement regardless of processor slow downs