Beruflich Dokumente
Kultur Dokumente
John E. Laird
Based on The Physics of the Game, Chapter 13 of Teach
Yourselff Game Programming
g
g in 21 Days,
y,
pp. 681-715
Updated 10/18/2010
Why Physics?
Some games dont need any physics
G
Games bbased
d on th
the reall world
ld should
h ld look
l k realistic,
li ti
meaning realistic action and reaction
More complex games need more physics:
sliding through a turn in a racecar, sports games, flight simulation, etc.
Game Physics
Approximate real-world physics
We dont want just the equations
We want efficient ways to compute physical values
Assumptions:
2D pphysics,
y
, usuallyy easyy to generalize
g
to 3D (add
(
z))
Rigid bodies (no deformation)
Will just worry about center of mass
Not accurate for all physical effects
v: velocity
vy
vx
(x0, y0)
Acceleration
Acceleration (a): change in velocity per unit time
Acceleration
l
i
V l it
Velocity
Approximate
Acceleration
Constant acceleration: vx +
+= ax, vy +
+= ay
Variable acceleration:
use table lookup based on other factors:
acceleration = acceleration_value(gear, speed, pedal_pressure)
Cheat a bit: acceleration = acceleration_value(gear, speed) * pedal_pressure
Piece
Piece-wise
wise linear approximation to continuous functions
Gravity
Gravity is a force between two objects:
Force
F
F = G ((m1m2)/ D2
G = 6.67 x 10-11 Nm2kg-2
mi: the mass of the two objects
D = distance between the two objects
On earth,
Mass
Objects represented by their center of mass, not
accurate for all physical effects
Center of mass (xc, yc) for a polygon with n vertices:
Attach a mass to each vertex
xc = ximi/ mi, i = 0 .. n
yc = yimi/ mi, i = 0 .. n
Friction
Conversion of kinetic energy into heat
Frictional force Ffriction = m g
mass(m)
m*g
Ffriction
Non-linear acceleration
R ti friction
Resting
f i ti > rolling
lli friction
f i ti
Rolling friction < sliding friction
Centripetal force?
What
Wh t about
b t other
th types
t
off racing
i games
Motor Boat?
Sailboat
Hovercraft?
Ice boat
Projectile Motion
Forces
vix = vi cos()
viy = vi sin()
W: wind
Wr: wind resistance
g: gravity
vi = initial velocity
m: mass of projectile
y += viy
With wind resistance and gravity:
: angle of inclination
vix += Wrx
viy += Wry + g, g normalized
Can
C add
dd vapor trail
il (different
(diff
color,
l lifetime,
lif i
wind)
i d)
Advanced Physics
Modeling liquid (Shrek,
F d
Finding
Nemo)
N
)
Movement of clothing
Movement of hair
(Monster Inc.)
Fire/Explosion effects
Reverse Kinematics
http://www.youtube.com/watch#!v
=qg0ydRQyPE8&feature=fvw
http://www.youtube.com/watch#!v
http://www
youtube com/watch#!v
=wWlaD_2gsIM&feature=fvw
Physics Engines
Strengths
Do all of the p
physics
y
for yyou as a ppackage
g
Weaknesses
Can be slow when there are many objects
Source: AGEIA
Back to Collisions
Non-elastic
Hit something with a bullet
Run over a prize
Elastic
Pong, breakout
Elastic Collisions
Steps of analysis for different types of elastic collisions
Collisions:
Si Steps
Six
S
off A
Analysis
l i
1. Detect that a collision has occurred
2. Determine the time of the collision
r
r
r2
x2, y2
x1, y1
xa, ya
x1 < xa < x2
y1 < ya < y2
x2, y2
Still O(N
( 2) comparisons,
p
, N number of objects
j
Area of Interest
Avoid most of the calculations by using a grid:
Size
Si off cell
ll = diameter
di
t off biggest
bi
t object
bj t
Detect collision
Sort objects by one dimension.
Bin objects within maximum size
j scan)) objects
j
in a bin.
Sort ((or just
Collisions:
Si Steps
Six
S
off A
Analysis
l i
1. Detect that a collision has occurred
2. Determine the time of the collision
xh
x1,y1
collision normal
x2,y
y2
Determine
ete
e changes
c a ges in rotation
otat o
None!
Is
I this
hi worthh it?
i ? Depends
D
d on speedd off simulation,
i l i
Circle-circle
Circle
circle Collision
Another important special case
Good step for your games
Many techniques developed here can be
used for other object types
Circle-circle
Circle
circle Collision Response
Determine the time of the collision
Interpolate based on old and new
positions of objects
collision normal
Circle-circle
Circle
circle Collision Response
Determine the velocity: assume elastic, no
f i i head
friction,
h d on collision
lli i
y)
Conservation of Momentum ((mass * velocity):
m1v1 + m2v2 = m1v1 + m2v2
Conservation of Energy
gy ((Kinetic Energy):
gy)
m1v12 + m2v22 = m1v12 + m2v22
Final Velocities
Ve oc es
v1 = (2m2v2 + v1(m1-m2))/(m1+m2)
v2 = (2m1v1 + v2(m1-m2))/(m1+m2)
Wh
Whatt if equall mass, m1 = m2
What if m2 is infinite mass?
Circle-circle
Circle
circle Collision Response
For non-head on collision, but still no friction:
Velocity change:
Maintain conservation of momentum
Change of velocity reflect against the collision normal
collision surface
Must be careful
Round-off error in floating point arithmetic can throw off
computation
i
Careful with divides
Especially
E
i ll with
ith objects
bj t off very different
diff
t masses