Simulation Engines TDA571|DIT030 Physics Tommaso Piazza 1 - - PowerPoint PPT Presentation

simulation engines tda571 dit030 physics
SMART_READER_LITE
LIVE PREVIEW

Simulation Engines TDA571|DIT030 Physics Tommaso Piazza 1 - - PowerPoint PPT Presentation

Simulation Engines TDA571|DIT030 Physics Tommaso Piazza 1 Administrative stuff Group project presentation Friday, 18 December at 10.00 sharp Presentation using i.e. Powerpoint The extensions made by every member of the group


slide-1
SLIDE 1

Simulation Engines TDA571|DIT030 Physics

Tommaso Piazza

1

slide-2
SLIDE 2

IDC | Interaction Design Collegium

Administrative stuff

  • Group project presentation Friday, 18 December at 10.00

sharp

  • Presentation using i.e. Powerpoint
  • The extensions made by every member of the group
  • The simulation engine that has been put together
  • Tech demo
  • Use terms that make it easy for people from the outside to

understand your work

  • Use plenty of screenshots
  • Diagram of system architecture
  • Show movie if applicable
  • Walkthrough of the tech demo
  • 15 minutes including questions

2

slide-3
SLIDE 3

IDC | Interaction Design Collegium

Administrative stuff

  • Group report
  • The group report should describe the work done as

well as the final result

  • 14-18 pages in total
  • Filename
  • simEngines-g#-GroupPresentation
  • in your group’s folder on Google Docs
  • Deadline
  • Monday, 11 January 24.00

3

slide-4
SLIDE 4

IDC | Interaction Design Collegium

Administrative stuff: Contents of group report

  • Conclusion
  • Introduction
  • Purpose
  • Goals
  • Pre-study
  • 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

Link to the code repository on Google Code

4

slide-5
SLIDE 5

IDC | Interaction Design Collegium

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
  • simEngines-g#-YourName-Report
  • in your group’s folder on Google Docs
  • Deadline
  • Monday, 11 January 24.00

5

slide-6
SLIDE 6

IDC | Interaction Design Collegium

Administrative stuff: Other stuff to do

  • Source code and data
  • Should be Google Code, put the link in you group

report

  • 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

6

slide-7
SLIDE 7

IDC | Interaction Design Collegium

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...

7

slide-8
SLIDE 8

IDC | Interaction Design Collegium

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

8

slide-9
SLIDE 9

IDC | Interaction Design Collegium

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”

9

slide-10
SLIDE 10

IDC | Interaction Design Collegium

Demo movies - Havok

10

slide-11
SLIDE 11

IDC | Interaction Design Collegium

Demo movies - Havok

11

slide-12
SLIDE 12

IDC | Interaction Design Collegium

Demo movies - Physix

12

slide-13
SLIDE 13

IDC | Interaction Design Collegium

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

13

slide-14
SLIDE 14

IDC | Interaction Design Collegium

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

14

slide-15
SLIDE 15

IDC | Interaction Design Collegium

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

15

slide-16
SLIDE 16

IDC | Interaction Design Collegium

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

16

slide-17
SLIDE 17

IDC | Interaction Design Collegium

Mass

  • Finding the mass of a body entails summing up the mass of

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

17

slide-18
SLIDE 18

IDC | Interaction Design Collegium

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 on the right

18

slide-19
SLIDE 19

IDC | Interaction Design Collegium

Moment of inertia

  • The moment of inertia describes the radial distribution of

mass in the body. It measures an object's resistance to changes in its rotation rate

  • 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

19

slide-20
SLIDE 20

IDC | Interaction Design Collegium

Moment of inertia

  • We will not go into detail about this computation
  • Calculating the moment of inertia for an arbitrary object is difficult –
  • ften 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

20

slide-21
SLIDE 21

IDC | Interaction Design Collegium

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

21

slide-22
SLIDE 22

IDC | Interaction Design Collegium

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

22

slide-23
SLIDE 23

IDC | Interaction Design Collegium

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)

23

slide-24
SLIDE 24

IDC | Interaction Design Collegium

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

24

slide-25
SLIDE 25

IDC | Interaction Design Collegium

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

  • f the surface

Ffr = µsN

25

slide-26
SLIDE 26

IDC | Interaction Design Collegium

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.

26

slide-27
SLIDE 27

IDC | Interaction Design Collegium

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

  • ur kinetics solver to derive the angular acceleration

vector of the car.

27

slide-28
SLIDE 28

IDC | Interaction Design Collegium

Break

http://xkcd.com

28

slide-29
SLIDE 29

IDC | Interaction Design Collegium

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 over 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

29

slide-30
SLIDE 30

IDC | Interaction Design Collegium

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

30

slide-31
SLIDE 31

IDC | Interaction Design Collegium

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 orientation, angular velocity and angular acceleration

31

slide-32
SLIDE 32

IDC | Interaction Design Collegium

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

32

slide-33
SLIDE 33

IDC | Interaction Design Collegium

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

33

slide-34
SLIDE 34

IDC | Interaction Design Collegium

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

34

slide-35
SLIDE 35

IDC | Interaction Design Collegium

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

35

slide-36
SLIDE 36

IDC | Interaction Design Collegium

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

36

slide-37
SLIDE 37

IDC | Interaction Design Collegium

Aircraft: Control surfaces

  • Below 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

37

slide-38
SLIDE 38

IDC | Interaction Design Collegium

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 suitably, 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

38

slide-39
SLIDE 39

IDC | Interaction Design Collegium

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.

39

slide-40
SLIDE 40

IDC | Interaction Design Collegium

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
  • bject 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

40

slide-41
SLIDE 41

IDC | Interaction Design Collegium

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 out 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

41

slide-42
SLIDE 42

IDC | Interaction Design Collegium

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 of the hull

  • This allows us to build a tetrahedral 3D model of the

hull as well as (relatively) easy calculate its volume

42

slide-43
SLIDE 43

IDC | Interaction Design Collegium

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

43

slide-44
SLIDE 44

IDC | Interaction Design Collegium

Cars

  • The normal forces on each wheel depends
  • n the distribution of weight in the car
  • This distribution might change dynamically as the

car moves through the environment

44

slide-45
SLIDE 45

IDC | Interaction Design Collegium

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

45

slide-46
SLIDE 46

IDC | Interaction Design Collegium

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

46

slide-47
SLIDE 47

IDC | Interaction Design Collegium

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)

47

slide-48
SLIDE 48

IDC | Interaction Design Collegium

Integrating motion

  • We are dealing with a finite real-time simulation, meaning

that our 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

48

slide-49
SLIDE 49

IDC | Interaction Design Collegium

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

49

slide-50
SLIDE 50

IDC | Interaction Design Collegium

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

  • f (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

50

slide-51
SLIDE 51

IDC | Interaction Design Collegium

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

51

slide-52
SLIDE 52

IDC | Interaction Design Collegium

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.

52