2522437 fisika lecture 5 basic physics
TRANSCRIPT
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
1/32
John E. Laird and Sugih Jamin
September 17, 2007
Based on The Physics of the Game, Chapter 13 of Teach
Yourself Game Programming in 21 Days,
pp. 681-715
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
2/32
Some games dont need any physics
ames ase on t e rea wor s ou oo rea st c,
meaning realistic action and reaction
sliding through a turn in a racecar, sports games, flight simulation, etc.
Running and jumping off the edge of a cliff
Two types of physics: Elastic, rigid-body physics, F = ma, e.g., pong
- , , ,whip, chain, hair, volcanoes, liquid, boomerang
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
3/32
Approximate real-world physics
We dont want just the equations
Assume fixed discrete simulation constant time step
Must account for actual time passed for variable simulation
Assumptions: 2D physics, usually easy to generalize to 3D (addz)
g o es no e orma on Will just worry about center of mass
Not accurate for all physical effects
Constant time step
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
4/32
Modeling the movement of objects with velocity
Assume distance unit is in pixels
Position at time tfor an ob ect movin at velocitv, from starting positionx0:
x(t) = x0 + vx t 0 y
Incremental computation per frame, assumingconstant time ste and no acceleration: vx and vy constants, pre-compute
x += vx, y += vy
vyv: velocity
vx
(x0, y0)
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
5/32
Acceleration (a): change in velocity per unit time
Acceleration
e oc y
Approximate
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
6/32
= = x x y y
Variable acceleration: use table looku based on other factors:
acceleration = acceleration_value(gear, speed, pedal_pressure)
Cheat a bit: acceleration = acceleration_value(gear, speed) * pedal_pressure*x
ay = sin (v) * acceleration
-
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
7/32
Gravity is a force between two objects: Force F = G (m1m2)/ D
G = 6.67 x 10-11Nm2kg-2
mi: the mass of the two objects
= s ance e ween e wo o ec s
So both objects have same force applied to them
F=ma --> a=F/m
On earth, assume mass of earth is so large it doesnt
move, andD is constant
ssume un orm acce era on Position of falling object at time t:
x(t) = x0 y(t) = y0 + 1/2 * 9.8 m/s
2 * t2
Incrementally,y += gravity (normalized to frame rate)
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
8/32
Gravity
Inf uences ot o ies
Can have two bodies orbit each other
Onl si nificant for lar e mass ob ects
ConsiderN-body problem
What ha ens after ou a l a force to an ob ect?
What happens when you shoot a missile from a moving
object?
What types of controls do you expect to have on a
s ace shi ?
What about a flying game?
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
9/32
Objects represented by their center of mass, not
u e p ys e e s
Center of mass (xc, yc) for a polygon with n vertices:
xc = ximi/mi, i = 0 .. n
yc = yimi/mi, i = 0 .. n
For sprites, put center of mass where pixels are densest
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
10/32
Conversion of kinetic energy into heat
friction
m = mass, g = 9.8 m/s2
,= frictional coefficient = amount of force to maintain a constant speed
mass(m)Fpush Ffriction
Factual = Fpush - Ffrictionm*g
are u a r c on oesn cause your o ec o move ac war Consider inclined plane
Usually two frictional forces Static friction when at rest (ve ocity = 0). No movement un ess overcome.
Kinetic friction when moving (k
< s)
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
11/32
Non-linear acceleration
es ng r c on > ro ng r c on
Rolling friction < sliding friction
What controls do ou ex ect to have for a racin ame? Turning requires forward motion!
a a ou o er ypes o rac ng games Boat? Hovercraft?
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
12/32
Forces
vi = vi cos()
W: wind
W: wind resistance
viy = vi sin()
Reaches apex at t = vi sin()/g,
g: gravityr
vi = initial velocity
hits ground atx = vix * viy g
With wind:
m: mass of projectile
x = vix
y += viy
: angle of inclination
vix += Wrx
v += W + normalizedy y
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
13/32
Start with lots of point objects (1-4 pixels)
Initialize with random velocities based on velocity ofob ect ex lodin
Apply gravity
Transform color intensity as a function of time
Destroy objects upon collision or after fixed time
Can a vapor tra erent co or, et me, w n
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
14/32
Modeling liquid (Shrek,
in ing emo
Movement of clothing
Movement of hair
(Monster Inc.) Fire/Explosion effects
Reverse Kinematics
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
15/32
Havok, AGEIA PhysX, Tokamak, etc.
trengt s Do all of the physics for you as a package
ea nesses Can be slow when there are many objects (use PPU?)
Ma have trouble with small vs. bi ob ect interactions
Have trouble with boundary cases
Source: AGEIA
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
16/32
Steps of analysis for different types of collisions
,
Two circles/spheres Rigid bodies
e orma e
Model the simplest - dont build a general engine
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
17/32
Detect that a collision has occurred
Determine the time of the collision So can back up to point of collision
Determine where the objects were at time of collision
Determine the collision angle off the collision normal
Determine the velocity vectors after collision
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
18/32
Simplest case
-
Assume circle hitting an immovable barrier
Detect that a collision occurred
Reformulate as a point about to hit a bigger wall
If vertical and horizontal walls, simple test of x, y
r
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
19/32
What if more complex background: pinball?
For comp ex surfaces, pre-compute an fi an array wit
collision points (and surface normals)
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
20/32
Determine the time of collision (tc):
x1,y1
xh
c = i h- 1 2- 1
ti = initial timet= time increment collision normal
Determine where the objects are when they touch = - - * -
2, 2
c 1 1 2 c i
Determine the collision angle against collision normal Collision normal is the surface normal of the wall in this case
Compute angle of line using (x1-xh) and (y1-yc)
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
21/32
Determine the velocity vectors after collision
=
angle equal and opposite off the surface normal If surface is co-linear with thex- ory-axes:
er ca - c ange s gn o x ve oc y
Horizontal - change sign ofy velocity
Corner - change sign of both
Use t - tc to calculate new position from collision point
Determine chan es in rotation None!
Is t s wort t Depen s on spee o s mu at on,
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
22/32
- Another important special case
Good step for your games
Many techniques developed here can be
Assume elastic collisions: Conservation of momentum
Conservation of kinetic energy
Non-elastic collision converts kinetic
energy into heat and/or mechanicaldeformations
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
23/32
If the distance between two circles is less than the sum of
Trick: avoid square root in computing distance! Instead of checking (r1+r2) > D, whereD = sqrt((x1-x2)
2 + (y1-y2)2)
ec r1 r2 x1-x2 y1-y2
x1, y1
x2, y2
r21
Unfortunately, this is still O(N2) comparisons,Nnumber of
objects
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
24/32
With non-circles, gets more complex and more
expens ve or eac pa r-w se compar son
Use bounding circles/spheres and check for overlap Pretty cheap
Not great for thin objects
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
25/32
General approach:
O servations: co isions are rare
Most of the time, objects are not colliding
Use various filters to remove as many objects as possible
from the comparison set
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
26/32
Avoid most of the calculations by using a grid:
ze o ce = ameter o ggest o ect
Test objects in cells adjacent to objects center Can be com uted usin mods of ob ects coordinates:
bin sort
Linear in number of objects
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
27/32
Alternative if many different sizes
Ce size can e ar itrary
E.g., twice size of average object
Must determine all the cells the object touches
Works for non-circles also
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
28/32
- Determine the time of the collision
Interpolate based on old and newpositions of objects
Determine where objects are when
they touch
Determine the collision normal
through the colliding intersection collision normal
collision surface
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
29/32
- Determine the velocity: assume elastic, no
r ct on, ea on co s on
Conservation of Momentum (mass * velocity): m1v1 + m2v2 = m1v1 + m2v2
Conservation of Ener Kinetic Ener : m1v1
2 + m2v22 = m1v1
2 + m2v22
Final Velocities v1 = (2m2v2 + v1(m1-m2))/(m1+m2)
v2 = (2m1v1 + v2(m1-m2))/(m1+m2) at equa mass, m1 = m2 What if m2 is infinite mass?
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
30/32
ir l - ir l lli i n R nFor non-head on collision, but still no friction:
Velocity change: Maintain conservation of momentum
collision surface
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
31/32
Round-off error in floating point arithmetic can throw off
computat on Careful with divides
spec a y w o ec s o very eren masses
-
8/3/2019 2522437 Fisika Lecture 5 Basic Physics
32/32
For simple collisions, dont do the math
Two i entica a s swap ve ocities
For collisions between dissimilar objects rea e a co s on ma r x
paddle
ball
side
brick
bottom