08-12-01 Simulation Engines 2008, Markus Larsson 1
Physics Simulation Engines 2008 Chalmers University of Technology - - PowerPoint PPT Presentation
Physics Simulation Engines 2008 Chalmers University of Technology - - PowerPoint PPT Presentation
Physics Simulation Engines 2008 Chalmers University of Technology Markus Larsson markus.larsson@slxgames.com 08-12-01 Simulation Engines 2008, Markus Larsson 1 Administrative stuff No lecture on Wednesday 08-12-01 Simulation Engines 2008,
08-12-01 Simulation Engines 2008, Markus Larsson 2
Administrative stuff
No lecture on Wednesday
08-12-01 Simulation Engines 2008, Markus Larsson 3
Administrative stuff
Group report
The group report should describe the work done as
well as the final result
14-18 pages in total Filename
groupreport_group_X.pdf
08-12-01 Simulation Engines 2008, Markus Larsson 4
Administrative stuff: Contents of group report
Conclusion
Introduction
Purpose
Goals
Prestudy
How does the system look today?
What did you want to make better?
Project plan and division of themes
Specification of demands (for each extension)
Functional demands (functionality)
Non-functional demands (properties)
Analysis
Conceptual model
Architecture
Design (for each extension)
Classes
Interaction
Implementation (for each extension)
Integration and testing
Tech demo
Description
Design
Implementation
Results
Screenshots
Performance
Conclusions
List of sources
Appendixes: Extension proposals
08-12-01 Simulation Engines 2008, Markus Larsson 5
Administrative stuff: Individual report
6-8 pages
Contents
Description of work tasks assigned to you and your role in the
group's work
Description of your own contributions to the end result Evaluation of your own work Evaluation of the group's work Description of the experiences and knowledge you have acquired
in planning, designing, group dynamics and technical knowledge
Project diary as an appendix
File name
report_your_name.pdf
08-12-01 Simulation Engines 2008, Markus Larsson 6
Administrative stuff: Other stuff to do
Source code and data
Should be in the group's file area when you hand in
your reports
Put a readme.txt in the root of the file area
How to compile your code How to run your tech demo Anything else you can think of to aid me in accessing
your work
08-12-01 Simulation Engines 2008, Markus Larsson 7
Introduction
Today's lecture will be about physics Not so long ago physics in games were nothing
but pure cheating
Physics in modern games are extremely
advanced and there is a lot of interest in the topic at the moment
Third party solutions are used most of the time
PhysX, Havok, Newton, Bullet, ODE, etc...
08-12-01 Simulation Engines 2008, Markus Larsson 8
Why let physics do your physics
Why are we bothering with simulating real-world physics in a game?
Only interesting for simulations?
Verisimilitude
Players want computer games to “feel” realistic in some way in
- rder to suspend their disbelief
The atmosphere of the game will be quite spoiled if characters
and objects in the game do not behave at all as you would expect them to
It’s important to note that verisimilitude literally means “possessing
the appearance of truth”
We do not mind if what we model is not real as long as it feels
real – in fact, we seldom want to model the exact real properties of a physical process
08-12-01 Simulation Engines 2008, Markus Larsson 9
Why let physics do your physics
Productivity
Just like in the case with AI characters, we can use
scripting to make objects in our world behave realistically
Would take a lot of effort and fine-tuning to make it look
good
If we introduce a general physics framework into
- ur game, we just have to specify physical
properties of our game objects and essentially get their behaviors for “free”
08-12-01 Simulation Engines 2008, Markus Larsson 10
Demo
Bullet integrated into the Blender project
08-12-01 Simulation Engines 2008, Markus Larsson 11
Using a physics engine
Keywords
Callbacks Bounding volumes Convex hulls
Different physics engines interface quite
differently, but they tend to be callback-based
08-12-01 Simulation Engines 2008, Markus Larsson 12
Fundamentals
In order to understand physics engines we must
understand the fundamentals of mechanics
We will be using SI-units in our treatment of
physics
Length
meter: m
Mass
kilogram: kg
Time
second: s
Electric current
ampere: A
Thermodynamic temperature
kelvin: K
Amount of substance
mole: mol
Luminous intensity
candela: cd
To us, length, mass and time are interesting
08-12-01 Simulation Engines 2008, Markus Larsson 13
Newton's laws of motion
Sir Isaac Newton formulated three basic laws of motion in his Philosophiae Naturalis Principia Mathematica
A body tends to remain at rest or continue to move in a
straight line at constant velocity unless it is acted upon by an external force. This is the concept of inertia.
The acceleration of a body is proportional to the resultant
force acting on the body, and this acceleration is in the same direction as the resultant force.
For every force acting on a body (action) there is an equal
and opposite reacting force (reaction) in which the reaction is collinear to the acting force.
Main lesson: F = ma
08-12-01 Simulation Engines 2008, Markus Larsson 14
Vectors and coordinate systems
We use a right-handed Cartesian coordinate space for both 2D and 3D
Vectors are denoted as F, where F is the magnitude, and Fx, Fy, and Fz are the components along the coordinate axes
Important vector operations include the cross product and dot product
To find the normal of a plane, we use N = F1 x F2
To find the shortest distance from a point to a plane in space, we use the dot product
08-12-01 Simulation Engines 2008, Markus Larsson 15
Mass properties
Formally speaking, our treatment on mechanics will focus on rigid bodies
Bodies made up of particles where each particle maintains a fixed position relative to its neighbors with no rotation or translation
I.e. bodies that do not change shape
Mass
The amount of matter in a body; a measure of a body’s resistance to linear motion
Center of mass
The point in a body around which its mass is evenly distributed; also the single point where applying a force will not result in a rotation of the body
Moment of inertia
Radial distribution of mass of a body about a given axis of rotation; a measure of a body’s resistance to rotational motion
08-12-01 Simulation Engines 2008, Markus Larsson 16
Mass
Finding the mass of a body entails summing up the mass
- f the individual particles making up the body
The mass of each particle is then the product of its density
and its volume
Given a body of uniform density, the total mass of the body
is simply the product of the density and the volume
In most cases, our bodies are not of uniform density,
however; a car consists of many different parts with different shapes and densities
Complex bodies are approximated using a collection of
simple bodies and sum up their masses for a total mass for the whole body
08-12-01 Simulation Engines 2008, Markus Larsson 17
Center of mass
Deriving the center of mass of a body is a little more complex than deriving its mass
We again need to split the body into
infinite particles and sum up their first moment in the local coordinate system and then dividing by the total mass to get the actual coordinates
Since we are dealing with finite bodies
making up more complex bodies, we can simply sum them up using the formula below
08-12-01 Simulation Engines 2008, Markus Larsson 18
Moment of inertia
The moment of inertia describes the radial distribution of mass in the body
Calculated by summing the second moment of mass elements in
the body
The lever is no longer the distance to the center of mass along the
coordinate axis, but the perpendicular distance from the coordinate axis for which we are calculating the moment of inertia
The second moment is m * d2
08-12-01 Simulation Engines 2008, Markus Larsson 19
Moment of inertia
We will not go into detail about this computation
Calculating the moment of inertia for an arbitrary object is
difficult – often we simplify a complex body into a set of simple bodies for which formulas have already been derived
The moment of inertia is defined for rotations around a specific axis
Represented by a scalar in 2D In 3D, a body can rotate around an arbitrary axis, so we use
a 3x3 matrix (inertia tensor) called I
A tensor is a mathematical entity that has both a magnitude and a direction
A vector is a first-rank tensor A 3D inertia matrix is a second-rank tensor
08-12-01 Simulation Engines 2008, Markus Larsson 20
Force
Forces are everywhere It affects all bodies and influences the acceleration of
- bjects according to Newton's laws of motion
One of the first tasks of describing a mechanics problem is
to isolate the forces acting upon a body in order to model it in a simulation
This is also the main task when defining objects in a
computer game with a physics engine
Note that forces have both a magnitude (measured in
Newton; 1 N = 1 kg m/s2) and a direction; thus we use vector notation to describe a force: F.
08-12-01 Simulation Engines 2008, Markus Larsson 21
Force and torque
Force and torque (also called moment) must be distinguished
Force
Cause of linear acceleration
Torque
Cause of rotational acceleration
Torque is defined as force times distance (and thus the unit is Newton-meters, Nm); to calculate it, multiply the perpendicular distance from the axis of rotation (the lever) times the magnitude of the force.
In 3D, we can calculate the torque M using the force vector
F and the local coordinates of the application point of the force r: M = r x F
08-12-01 Simulation Engines 2008, Markus Larsson 22
Force: Springs
Springs are structural elements that connect two
- bjects, applying equal and opposite forces to each
- bject
The forces are proportional to their extension Hook's law L is the current length of the spring and r is the
length of the spring at rest
Fs = ks(L – r) A damper is a structural element that acts to slow the
relative velocity between two connected objects
Fd = kd(v1 - v2)
08-12-01 Simulation Engines 2008, Markus Larsson 23
Force: Springs
It is worth nothing that we must also pay
attention to the direction of the force Fs
In fact, we can express springs and dampers in a
combined formula using vector notation
Newton's third law gives us: F2 = -F1 Springs and dampers can be used to model
non-rigid bodies by connecting rigid bodies or particles into a spring-damper system
08-12-01 Simulation Engines 2008, Markus Larsson 24
Force: Friction
Forces that arise from friction always resist
movement and are caused by interacting surfaces (a so-called contact force)
Friction forces are always parallel to the
contacting surfaces
The magnitude depends on the normal force
between the two surfaces and the roughness of the surface Ffr = µsN
08-12-01 Simulation Engines 2008, Markus Larsson 25
Describing the force situation
One important part of solving mechanics problems for a body is to describe the force situation for the body
This involves isolating the body from its environment and
plotting all the forces acting upon it as well as their application point
In the example above, we present the force situation for a simple car model.
08-12-01 Simulation Engines 2008, Markus Larsson 26
Describing the force situation
For the purposes of solving a kinetics problem,
we would employ the following strategy
Derive the resulting force
Sum up all the force vectors. The single resulting vector
R can be put into Newton’s second law to derive the new acceleration vector of the body.
Derive the resulting torque
Given the application position of each force, we can
easily compute the resulting torque as the sum of the individual torques for each force. This vector can then be used in our kinetics solver to derive the angular acceleration vector of the car.
08-12-01 Simulation Engines 2008, Markus Larsson 27
Kinematics
Kinematics is the study of motion of bodies without regard
to the forces acting on the body
More specifically we are dealing with the parameters
position, velocity and acceleration and how they change
- ver time
Velocity v = s' = ds / dt Acceleration a = v' = dv / dt = s'' = d2s / dt2 Using these basic formulas and ignoring the force
situation, we can easily simulate particles moving in both 2D and 3D
08-12-01 Simulation Engines 2008, Markus Larsson 28
Kinematics
We can easily derive formulas for constant
acceleration using the previously mentioned formulas v = v0 + at v2 = 2a(s – s0) + v02 s = s0 + vt (at2) / 2
08-12-01 Simulation Engines 2008, Markus Larsson 29
Rigid-body kinematics
The linear motion formulas for position, velocity and acceleration are useful for modeling particle-sized elements with negligible size
If we are dealing with rigid bodies which do have a size, we must also take angular motion into account
The corresponding parameters for angular motion is angular
- rientation, angular velocity and angular acceleration
08-12-01 Simulation Engines 2008, Markus Larsson 30
Kinetics
Kinetics is the study of the motion of bodies under the
influence of external forces acting on them
We can either choose to study particles or rigid bodies For particles, we only have to consider linear motion,
whereas rigid bodies also undergo angular motion
For linear motion, the basic formula we will consider is
Newton's second law
F = ma For angular motion in rigid bodies, we can derive a similar
formula
08-12-01 Simulation Engines 2008, Markus Larsson 31
Rigid-body kinetics
Rigid-body kinetics consists of two stages
Tracking the linear motion of the body's center of mass by treating
it as a particle
Tracking the angular motion of the body using angular velocity
and acceleration
The effect of the normal force between ball and the rolling plane will cause the ball to start moving down the plane according to F = ma. The friction will make the ball roll according to
08-12-01 Simulation Engines 2008, Markus Larsson 32
Vehicle modeling
We now have the knowledge to start looking at
how to model some real-world phenomena in
- ur physics engine
Since we are designing games and simulations,
an interesting example is to simulate various kinds of vehicles
In the following slides we will look at how to
simulate aircraft, ships and cars in a suitable way
08-12-01 Simulation Engines 2008, Markus Larsson 33
Aircraft
For a flight simulator, the physical flight model is one of the most important aspects in the entire simulator
This naturally depends on what kind of flight simulator we
are talking about, but here we assume we are dealing with a high-realism flight simulator and not a watered-down arcade game
There are four main forces acting upon an airplane in flight
Gravity Lift Thrust Drag
These four parameters are not independent, they are closely influenced by each other.
08-12-01 Simulation Engines 2008, Markus Larsson 34
Aircraft: forces
Gravity
The Earth’s gravitational field that ensures that whatever goes up, eventually comes down
Lift
Lifting force generated by the wings that keeps the airplane flying.
Thrust
Forward-propelling force generated by the aircraft’s engines to push the aircraft forward
Lift is usually directly influenced by the thrust
Drag
Resisting force that counteracts the thrust, generated by friction against the air itself
08-12-01 Simulation Engines 2008, Markus Larsson 35
Aircraft: Control surfaces
Above is a simplified version of a common
airplane with its control surfaces
These are the points on the aircraft which can exert
force on the surrounding environment, allowing the pilot to control the aircraft in flight
08-12-01 Simulation Engines 2008, Markus Larsson 36
Aircraft: Lift
When an airfoil moves through a fluid such as air, lift is generated
The equation we use to describe this phenomenon is Bernoulli’s Law
Proposed by Daniel Bernoulli in 1738 States that in a frictionless incompressible fluid flow, the
pressure in the fluid will go down locally in proportion to the speed
By constructing the airfoil suitable, the air’s velocity on top of the wing will be faster than under the wing, causing a local pressure drop over the wing and generating lift
08-12-01 Simulation Engines 2008, Markus Larsson 37
Aircraft: More information
For more information about flight simulation,
check out the following links
FlightGear (http://www.flightgear.org)
Open Source flight simulator project with an
exchangeable flight engine
JSBSim (http://www.jsbsim.org/)
Open Source highly configurable and precise flight
model engine.
08-12-01 Simulation Engines 2008, Markus Larsson 38
Ships
For a ship, the most important property is obviously the capability to float
In addition, the ship must float upright and not capsize
Archimedes’ principle tells us that the weight of an object floating in a fluid is equal to the weight of the volume of fluid displaced by the object
In order for a ship to float, it must have sufficient volume to
displace water of an amount equal to the weight of the ship
08-12-01 Simulation Engines 2008, Markus Larsson 39
Ships: Buoyancy
We can calculate the buoyancy of an object as a function of the density of the fluid, the gravitational constant and the volume of the submerged part of the object
The center of buoyancy is the geometric center of the submerged part of the object
When the ship sits at rest in the water, the center of buoyancy will be located directly below the center of mass
As the ship rolls or pitches, the center of buoyancy moves
- ut from under the center of mass, creating torque
In order to avoid capsizing, the ship must be stable
The metacenter (intersection between centerline and line of
action of center of buoyancy) must be located below the center of mass.
08-12-01 Simulation Engines 2008, Markus Larsson 40
Ships: Hull geometry and volume
In order to perform the mentioned calculations
we need to know the volume of the ship's hull
In fact, we need a full geometrical description of the
ship's hull in order to perform our simulations
One common way to do this is to specify the
ship's hull as a collection of ordered cross- sections at even intervals along the full length
- f the hull
This allows us to build a tetrahedral 3D model of the
hull as well as (relatively) easy calculate its volume
08-12-01 Simulation Engines 2008, Markus Larsson 41
Ships: Resistance and virtual mass
Ships moving on the surface of water experience drag in the same way as an aircraft moving through the air (air can be regarded as a fluid too)
For ships, the interface between air and water cause special
effects that we may also have to take into account
Another aspect that must be taken into account when simulating a ship’s movement in the water is the concept of virtual mass
Defined as the mass of the ship plus the mass of the water
that is accelerated with the ship
This causes extra inertia in the ship For example, large tanker ships may take several thousand
meters to come from full speed to a full stop
08-12-01 Simulation Engines 2008, Markus Larsson 42
Cars
The normal forces on each wheel depends on
the distribution of weight in the car
This distribution might change dynamically as the
car moves through the environment
08-12-01 Simulation Engines 2008, Markus Larsson 43
Cars: Air resistance
We have included the factor WF which is the resistance
from the air as the car moves through the fluid
We use a drag formula of the following form to model
this factor
p is the density of the fluid, v is the velocity of the car, Sp
is the projected frontal area of the car normal to the direction vector and Cd is the drag coefficient
About 0.29 to 0.4 for sports cars, 0.6 to 0.9 for trucks,
etc
08-12-01 Simulation Engines 2008, Markus Larsson 44
Cars: Friction and turning
We have already discussed how to model the contact friction
GFx for each wheel
A function of the normal force Nx for the wheel and a
frictional coefficient µs that is specific to the surface and the traction of the wheels
It is also friction that allows for the car to be controlled When the driver turns the steering wheel, the wheels turn
and the side friction between the wheels and the road exert a force directed to the side that will cause the car to turn
We also must keep track of the fact that the driver might
be trying to turn too quickly, in which case the side friction will not be enough and the car will lose traction
08-12-01 Simulation Engines 2008, Markus Larsson 45
Real-time simulation
This is the general approach to solving a
kinetics problem
- 1. Calculate the body’s mass properties (mass, center of mass, moment of
inertia).
- 2. Identify and quantify all forces and moments acting on the body (describe
the force situation).
- 3. Derive the resulting force and moments.
- 4. Solve the equations for linear and angular acceleration.
- 5. Integrate with respect to time to find linear and angular velocity. (Numerical
integration.)
- 6. Integrate with respect to time to find linear and rotational displacement.
(Numerical integration.)
08-12-01 Simulation Engines 2008, Markus Larsson 46
Integrating motion
We are dealing with a finite real-time simulation, meaning that
- ur calculation loop will be called with an interval of dt
Given the current resulting force R on a body, we can derive
its current acceleration as a = R/m
We then use this number to update the velocity of the body given the old velocity
We use this value to derive the next position of the body
This is essentially Euler's method for integration
The size of the time step dt governs the accuracy of the
result
08-12-01 Simulation Engines 2008, Markus Larsson 47
Euler's method
Euler’s method approximates a new value at the current step for a function f(x) by extrapolating in the direction of the derivative f0(x) at the previous step
This assumes that the derivative stays constant during the whole step size x, which generally is not true
Higher-order terms are lost to truncation error, but normally these terms are small and have little impact on the final result
One concern with Euler’s method and other numerical methods is when higher order terms actually have an impact on the final result, and the method does not converge to the exact value
The method is said to be unstable
Solution is to decrease the step size substantially
“The improved Euler's method” calculates two estimates of f(x + dx), one using the step size dx and the other using two steps of the size dx/2 and comparing the difference in result
If the truncation error is large, the step size is decreased accordingly
08-12-01 Simulation Engines 2008, Markus Larsson 48
Other numerical methods
Euler’s Method uses the first derivative term of the Taylor
Series to approximate the value of a function for the next step, discarding higher-order terms as truncation errors with the motivation that they have little impact on the final result
A more advanced example is the Runge-Kutta method
which reduces the truncation error down to the order of (dx)5
The cost is more performance cost per calculation. There exist other numerical integrations in addition to
these two but we will not go into detail on these in this lecture
08-12-01 Simulation Engines 2008, Markus Larsson 49
Game physics and 3D databases
Obviously, a physics engine requires access to
the 3D world in order to be able to function
To a large part it needs other information than what
the renderer needs
We have observed the same pattern for many
- ther areas of interest such as AI, network, etc
How to properly share data between different
components is a very advanced topic that you may need to spend a lot of time with
Especially in a multithreaded environment
08-12-01 Simulation Engines 2008, Markus Larsson 50
Summary
Physical simulation is now a common marketing trick used for new computer games
Using real physics gives realism and cuts down on the scripting necessary for realistic gameplay.
Newton’s laws of motion lie at the center of our physical simulation efforts.
The mass properties of a body include mass, center of mass, and moment of inertia.
Force causes linear motion, torque causes angular motion. There exists kinetic formulas for both types of motion.
Real-time simulation involves numerically integrating velocity and position over time;
the simplest way to do this is using Euler’s method.
Numerical methods in general and Euler’s Method in particular can be unstable for large step-sizes; adaptive methods which correct the step-size depending on the truncation error works best.
Runge-Kutta is a more accurate numerical integration method.
08-12-01 Simulation Engines 2008, Markus Larsson 51
Next lecture
Monday, 8 December
Swedish Game Awards
Wednesday, 10 December