calculations in physics and game engines games fundamentals © by jarek francik kingston university,...

43
Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Upload: aron-pope

Post on 21-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Calculations in Physicsand Game EnginesGames Fundamentals

© by Jarek FrancikKingston University, London

2007 - 2013

Page 2: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013
Page 3: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Simple Stuff

Uniform Motion (constant speed or average speed)

If my average speed is 60mph, how long does it take to Bristol (120 miles)?

vtx v

xt

The answer is:

260

120t

h

hm

m

/

t

xv

v – velocity or speedx – distance or displacementt – time

Bristol 120mi

v

xt

Page 4: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Simple Stuff

Uniform Motion (constant speed or average speed)

Rocket is travelling in straight line from the Earth to the Moon (roughly 360,000 km).How long does it take if its average speed is 10 km/s?

vtx v

xt

t

xv

v – velocity or speedx – distance or displacementt – time

The answer is:

s

skm

kmt

/36000

10

360000

Page 5: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Not so trivial, but still simple

Accelerated Motion (uniformly increasing speed)

0vatv 2

2

0

attvx

t

va

Dv – increase of velocitya – accelerationv0 – initial speed

Rocket is taking off and gaining the speed with the acceleration of a=10 m/s2.How long does it take to achieve its maximum speed of 10 km/s?

00 v

a

vt

atv

110

10t

2/

/

sm

skm

0vatv

Page 6: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Not so trivial, but still simple

Accelerated Motion (uniformly increasing speed)

0vatv

2

2

0

attvx

t

va

Dv – increase of velocitya – acceleration

Rocket is taking off and gaining the speed with the acceleration of a=10 m/s2.How long does it take to achieve its maximum speed of 10 km/s?

00 v

a

vt

atv

s

sm

sm

sm

smt

/

/

/

/1000

10

10000 2

2

0vatv

Page 7: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Not so trivial, but still simple

Accelerated Motion (uniformly increasing speed)

0vatv 2

2

0

attvx

t

va

Dv – increase of velocitya – acceleration

A new model of the rocket accelerates at a=10 m/s2 to the mid-point between the Earth and Moon (180,000 km away). What is the maximum speed now?

00 v

2

2atx atv

a

xav2

a

xt

2

a

xt

22

axv 2

0vatv 2

2

0

attvx

Page 8: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Not so trivial, but still simple

Accelerated Motion (uniformly increasing speed)

0vatv 2

2

0

attvx

t

va

Dv – increase of velocitya – acceleration

A new model of the rocket accelerates at a=10 m/s2 to the mid-point between the Earth and Moon (180,000 km away). What is the maximum speed now?

180000000102 3600000000

000,6036000,10

s

m

s

m2

2

axv 2

s

kmv 60

Page 9: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Facts

• Apollo 11 top speed was 11.08 km/s(39,897 kmh or 24,791 mph)

• Average Earth – Moon distance is 384,400 km(238,850 miles)

We took 360,000 to simplify the calculations

• Realistic trajectory of a lunar flight is far from the straight line

Page 10: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Facts

• Apollo 11 top speed was 11.08 km/s(39,897 kmh or 24,791 mph)

• Average Earth – Moon distance is 384,400 km(238,850 miles)

We took 360,000 to simplify the calculations

• Realistic trajectory of a lunar flight is far from the straight line

Page 11: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Game Physics Exercise

Page 12: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

"Minecraft" is a trademark of Notch Development AB

In a physically realistic mod for a game similar to Minecraft there is a small cart of mass 10 kg, standing still in a distance of 6 meters from a solid, vertical wall. Suddenly, the cart acquires an impulse (a hit) J = 40 Ns perpendicularly towards the wall. It moves forward, bounces from the wall and then moves back. How far from the wall is it when it eventually stops due to friction forces?• the friction coefficient f = 0.1• coefficient of restitution e = 0.5• gravitational acceleration in this world is similar to the Earth: g = 10 m/s2.

Page 13: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

mvpJ Initially, the cart was standing still, so v0 is velocity just after the hit:

smmJv /410/40/0

2/1 smfgm

fmg

m

Fa F

2

2

0

attvx

246

2tt solve in regard to t to find the bounce time

2/10

5.0

1.0

40

6

10

smg

e

f

sNJ

mx

kgm

deceleration due to friction

?)6(21

24

2

412164

642/1

0642

1

2

2

notwhya

bt

acb

cba

tt

st 2at the bounce time:

Page 14: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

mvpJ Initially, the cart was standing still, so v0 is velocity just after the hit:

smmJv /410/40/0

2/1 smfgm

fmg

m

Fa F

2

2

0

attvx

246

2tt

deceleration due to friction

st 2at the bounce time:

smatvv /2240

smevv /1' after the bounce!

time until the stand still:

statv 1'

distance travelled:

mat

x 5.02

11

2

22

But wait a moment, all that may be done in a much simpler way!

2/10

5.0

1.0

40

6

10

smg

e

f

sNJ

mx

kgm

Page 15: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

mvpJ Initially, the cart was standing still, so v0 is velocity just after the hit:

smmJv /410/40/0

2/1 smfgm

fmg

m

Fa F

2

2

0

attvx

246

2tt

deceleration due to friction

st 2at the bounce time:

smatvv /2240

smevv /1' after the bounce!

time until the stand still:

statv 1'

distance travelled:

mat

x 5.02

11

2

22

But wait a moment, all that may be done in a much simpler way!

solve in regard to t to find the bounce time

?)6(21

24

2

412164

642/1

0642

1

2

2

notwhya

bt

acb

cba

tt

2/10

5.0

1.0

40

6

10

smg

e

f

sNJ

mx

kgm

Page 16: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

mvpJ Initially, the cart was standing still, so v0 is velocity just after the hit:

smmJv /410/40/0

2/1 smfgm

fmg

m

Fa F

2/10

5.0

1.0

40

6

10

smg

e

f

sNJ

mx

kgm

You may be familiar with the following formula:

axvv 220

2 from which you find the velocity immediately:

2462422 vvand further as previously... axvv

xat

tv

attvavv

taatvvv

atvv

atvv

2

2

)2

(2

2

)(

20

2

2

0

2

020

2

220

20

2

20

2

0

Page 17: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

mvpJ Initially, the cart was standing still, so v0 is velocity just after the hit:

smmJv /410/40/0

2/1 smfgm

fmg

m

Fa F

2/10

5.0

1.0

40

6

10

smg

e

f

sNJ

mx

kgm

You may be familiar with the following formula:

axvv 220

2 from which you find the velocity immediately:

2462422 vvand further as previously...

but this is not yet exactly what I mean...

Page 18: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

mvpJ Initially, the cart was standing still, so v0 is velocity just after the hit:

smmJv /410/40/0

2/1 smfgm

fmg

m

Fa F

2/10

5.0

1.0

40

6

10

smg

e

f

sNJ

mx

kgm

t [s]

v [m/s]

0

1

1

2

3

4

2 3 4 56)(

2

0 dttvx

5.3)(1

0 dttvx

5.0)(3

2 dttvx

Page 19: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

back now to...

the rocket flying to the Moonbut

this timeTHE HARDCORE WAY

Page 20: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Hardcore StuffThe rocket is now burning its fuel, and the further it’s flying, the lighter it is.Initial mass is m0 = 20,000 kg and the acceleration a0 = 5 m/s2, but the rocketis losing m=1 kg/s during the flight. The jet force is constant.What will be the speed and distance from the Earth after the first hour of the flight?

Firstly, the easy thing: the jet force can be found from the initial conditions:

kNmaF 10000

Now, the mass of the rocket as the function of time:

tmtm 0)(Next step, the acceleration:

tm

Fta

0

)(

The acceleration is changing over the time, and the velocity calculation won’t be easy!

Page 21: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Physics of Motion

Average acceleration is change of velocity over time:

Exact acceleration – is the derivative of velocity:

To calculate velocity (as a function of time), you simply need to integrate velocity in regard to time:

t

va

dt

dv

t

tvttva

t

)()(lim

0

t

dttav0

)(

Page 22: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Physics of Motion

To calculate velocity (as a function of time), you simply need to integrate velocity in regard to time:

– if acceleration is constant:

– if acceleration changes over time – integrate!

t

dttav0

)(

atvadtvt

00

Page 23: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Physics of Motion

Average velocity is displacement in time:

Exact velocity – is the derivative of displacement:

To calculate displacement, you need to integrate velocity in regard to time:

t

xv

dt

dx

t

txttxv

t

)()(lim

0

1

0

)(t

tdttvx

Page 24: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Physics of MotionTo calculate displacement, you need to integrate velocity in regard to time:

– for Uniform Motion:

– for Accelerated Motion:

– for any other type of motion – simply integrate!

1

0

)(t

tdttvx

vtvdtxvtvt

0)(

2)()(

2

00 00

attvdtatvxatvtv

t

Page 25: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Hardcore Stuff

tm

Fta

0

)(

ttdt

tm

Fdttatv

00

0)()(

tm

mFvtv

0

00 ln)(

s

kmvt 8.19

360020000

20000ln

1

100000)3600(3600

skg

kgm

kNF

/1

000,20

100

0

After an hour of flight (3600 secs):

Knowing acceleration, calculate velocity:

Page 26: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Hardcore Stuff

tm

mFtv

0

0ln)(

skg

kgm

kNF

/1

000,20

100

0

After an hour of flight (3600 secs):

Knowing velocity, calculate displacement:

1

0

1

0

)ln()()(0

0t

t

t

tdt

tm

mFdttvtx

tm

mtm

tF

tx 0

00 ln)(

)(

kmx 540,34)3600(

Page 27: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Hardcore Stuff

• Let’s go to the next level of difficulty

– the rocket is affected by the Earth gravity.In outer space, this is non-linear and dropswith the square of the distance from the Earth

– the rocket is also affected by the Moon gravity.It’s non-linear and rises with the square of the distance to the Moon

– the jet force is not constant: it is controlled by the astronaut and may change in any time, irregularly

Page 28: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Numerical Integration

• Wait a moment, there must be an easier way:– Game Developers are smart,

but they may not like integrating that much– Methods of analytical integration are not even

covered in this module!– Logarithms are hard to calculate for computers

So?

Of course there is!

Page 29: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Numerical Integration

t [s]

v [m/s]

skg

kgm

kNF

/1

000,20

100

0

tmm 01

11 m

Fa

tavv 101

20000;0;0 000 mvt

ttt 01

tmm 12

22 m

Fa

tavv 212

ttt 12

approximated value!good if Dt is small enough

Page 30: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Numerical Integration

t [s]

v [m/s]

skg

kgm

kNF

/1

000,20

100

0

tmm 12

22 m

Fa

tavv 212

ttt 12

tmm 23

33 m

Fa

tavv 323

ttt 23

Page 31: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Numerical Integration

t [s]

v [m/s]

skg

kgm

kNF

/1

000,20

100

0

tmm 23

33 m

Fa

tavv 323

ttt 23

tmm nn 1

nn m

Fa

tavv nnn 1

ttt nn 1

difference equations – equations that determine the difference between the (n)th and the (n+1)th

Page 32: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Numerical Integration

t [s]

v [m/s]

skg

kgm

kNF

/1

000,20

100

0

tmm nn 1

nn m

Fa

tavv nnn 1

ttt nn 1

nt

n dttvx0

)(

n

n

n t

t

tdttvdttv

1

1

)()(0

tvdttv n

tn 1

0)(

tvxx nnn 1

Page 33: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Numerical Integration

t [s]

v [m/s]

skg

kgm

kNF

/1

000,20

100

0

tmm nn 1

nn m

Fa

tavv nnn 1

ttt nn 1

nt

n dttvx0

)(

n

n

n t

t

tdttvdttv

1

1

)()(0

tvdttv n

tn 1

0)(

tvxx nnn 1

Page 34: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Numerical Integration

tmm nn 1

nn m

Fa

tavv nnn 1

ttt nn 1

tvxx nnn 1

double v = rocket.GetSpeed();double m = rocket.GetMass();m -= mi;double a = F / m;v += a;rocket.SetSpeed(v);rocket.SetMass(m);rocket.Update(GetTime());

this function updates the sprite’s location

Dt is often considered to be 1

Page 35: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Numerical Integration

• Numerical integration is based on repeating simple operation (addition) many times

• Numerical Integration is approximate,but with small values of Dt it is sufficiently good

• If you have any concerns, play Angry Birds(or any other game with good physics)

Page 36: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Numerical Integration

• Most people are not very good in integration• Mathematicians are good in symbolic integration,

but computers find it challenging• Computers are very good in numerical

integration, but people very ineffective in it!

Page 37: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Numerical Integration

• Integrator – any component (hardware or software) calculating a single sample in Numerical Integration

• GFC OnUpdate / Update functions are examples of integrators

• CSprite::Update function uses the current velocity to update the position of the sprite moving it by Dx = vDt, and Dt=1/60 sec.

• Another example of basic integration step is modelling the gravity:sprite.SetVelocity(sprite.GetXVelocity(), sprite.GetYVelocity() - 10);

Page 38: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Numerical Integration

• Interpolating Functions used in NI:– rectangle rule

– trapezoidal rule

– Simpson’s rule (parabolic)

Page 39: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Today’s Workshop Task

In Your Rocket:– throttle is controlled by the player– the more thrust, the more fuel is burnt– the total rocket mass is the net mass plus the fuel– the rocket operates in the overlapping gravitational

fields of the Earth and the Moon– the net force acting on the rocket is a sum of the two

gravitational forces and the engine thrust– calculate the acceleration from the 2nd law of motion

Page 40: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Isaac Newton’s Law of Universal Gravitation

Every point mass in the universe attracts every other point mass with a force that is directly proportional to the product of their masses and inversely proportional to the square of the distance between them

Page 41: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Isaac Newton’s Law of Universal Gravitation

Every point mass in the universe attracts every other point mass with a force that is directly proportional to the product of their masses and inversely proportional to the square of the distance between them

2r

MmGF

r

M

m

22111067.6 kgNmG

Page 42: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

Values

Gravitational constant G is one of the fundamental constants of our Universe

G Gravitational Constant 6.67 10∙ -11 Nm2kg-2

Mass of the Earth 5.97 10∙ 24 kg

Mass of the Moon 7.35 10∙ 22 kg (0.0123 Earths)

Radius of the Earth 6378 km

Radius of the Moon 1737 km (0.273 Earth)

Average Earth-Moon distance 384,400 km

Average Earth-Sun distance 149,597,870 km

Page 43: Calculations in Physics and Game Engines Games Fundamentals © by Jarek Francik Kingston University, London 2007 - 2013

ValuesIn a Game Engine

rarely there is a need to use real world valuesGM value for the Earth 12000

GM value for the Moon 3000 (0.25 Earths)

Radius of the Earth 225 pixels

Radius of the Moon 180 pixels

X coordinate for the Earth -100

X coordinate for the Moon 900

Earth-Moon distance 1000 pixels

own mass of the rocket 120 – plus up to 100 fuel

thrust multiplier 25

these are simply the values that worked fine for mea very much different set of values may be fine for you