• Non ci sono risultati.

constants: they don’t (usually) change

N/A
N/A
Protected

Academic year: 2021

Condividi "constants: they don’t (usually) change"

Copied!
21
0
0

Testo completo

(1)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 1

Newtonian dynamics:

summary

Actual object location

Rate of change of 

(d / dt )

“with mass”

(momentum)

What changes the rate of change (d2/ dt2)

“with mass”

Position 𝑝 𝑝 = (x,y,z)

Velocity 𝑣⃗

𝑣⃗ = 𝑝̇

( |𝑣⃗| = “speed” )

Momentum 𝑣⃗ 𝑚

Acceleration 𝑎⃗ = 𝑣⃗̇ = 𝑝̈

Force 𝑓⃗

𝑓⃗ = 𝑎⃗ 𝑚

Orientation (e.g. quaternion)

Angular velocity 𝜔 Angular momentum 𝜔 𝐼

𝐼 = moment of inertia (for axis)

(“rotational inertia”)

Angular acc. α Torque τ τ = 𝑎⃗ 𝐼

(“mechanic momentum”)

Change the state

(no memory)

state (is kept! inertia!)

(changes, but only continuously)

A few constants per object

A few quantities associated to each object

constants: they don’t (usually) change

input of the physical simulation, not output

Mass:

resistance to change of velocity

Moment of Inertia:

resistance to change of angular velocity

Barycenter:

the center of mass

(2)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 2

Mass

resistance to change of velocity

inertial mass

also, incidentally:

ability to attract every other object

gravitational mass

happens to be the same

what you measure with a scale

Unity of measure:

kg, g…

Moment of inertia

Resistance to change of angular velocity

(an object rotates around its barycenter)

high

low

(3)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 3

Moment of inertia

Scalar

moment of inertia

Resistance to change of angular velocity

Depends on the mass, and on its distribution

the farthest one sub-mass from the axis, the > the resistance

In 3D: its different for each axis of rotation

It can be computed for any axis, thanks to…

Moment of inertia as a 3x3 Matrix

a matrix A used to extract the scalar, for any given axis

given an axis a (a = unit vector), the moment of inertia is

a

T

A a

matrix A can be computed once and for all for a rigid object

how: that’s beyond this course

in practice: use given formulas for common shapes

or sum the contributions for each sub-mass

Barycenter

Aka the center of mass

(a position)

In the discrete setting:

simply the weighted average of the positions of the subparts composing an object

(literally “weighted”: with their masses)

Does not necessarily coincide with

the origin of the local frame of that object

(but it can)

(4)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 4

State of an object

current

current rates of change

constants

updated by

physics

Point position

Rotation orientation Vector velocity

Rotation angular_velocity Scalar mass

Matrix moment_of_inertia Point barycenter

Scalar drag

setup at initialization, (rarely) changed e.g. by scripts

Note: acceleration/forces/torques are not part of the state frictions;

see later

In

part of Transform component

the RigidBody component

Point position

Rotation orientation Vector velocity

Rotation angular_velocity Scalar mass

Matrix moment_of_inertia Point barycenter

Scalar drag

Adding a “RigidBody” component to a Game Object is to say:

“please let the Phys. engine take care of this object”

(5)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 5

In (using Unity terminology)

part of Transform component

the RigidBody component

Vector3 position Quaternion rotation Vector3 velocity

Quaternion angularVelocity float mass

Vector3 centerOfMass float drag

note: speed = velocity.magnitude

moment of inertia matrix the Vector3 = a diagonal matrix D by rotating it RTDR the final matrix note: they are the components

of the global transformation!

the barycenter (in object space) Vector3 inertiaTensor

Quaternion inertiaTensorRotation

per second (not per frame)

State of a point-particle

not used !

Point position

Rotation orientation Vector velocity

Rotation angular_velocity Scalar mass

Matrix moment_of_inertia Point barycenter

Scalar drag

One trend in game phys engines is to simulate point-particles only.

Much simpler!

E.g. no rotation needed!

We will see later how to still get complex objects back (with “PBD”) For now,

we focus on this simpler case.

(6)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 6

Dynamics (Newtonian)

dt v

p p

dt a

v v

m f

a

p fun

f

 

0 0

/

,...) (

Dynamics (Newtonian)

dt v

p p

dt a v

v

m f

a

p fun f

 

0 0

/

,...)

(

forces

acceler.

velocity positions

(7)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 7

Dynamics (Newtonian)

C C

t C

t C

C C

C C

dt t

v p

t p

dt t

a v

t v

m t

f t

a

t p fun t

f

0 0

0 0

) ( )

(

) ( )

(

/ ) (

) (

),...) (

( )

(

 

pos, acc, vel, forces:

in function of current time

t

C

An (obvious) precisation

= virtual time != real time

e.g.:

game paused  t costant.

Fast forward, replay,

rallenty, reverse  change of speed/flow direction of t

occasionally,

gameplay exploit this difference in spectacular ways!

PoP – the sands of times serie (Ubisoft, 2003-…) Braid (Jonathan Blow, 2008)

t

C

(8)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 8

Computing physics evolution

Analytical solutions:

state = function( t )

Given force functions (and acc), find the functions (pos, vel,…) in the specified relations:

Numerical solutions:

1. state

( t = 0)

 init 2. state

( t + 1)

evolve( state

t

) 3. goto 2

C C

t C

t C

C C

C C

dt t v p t

p

dt t a v

t v

m t f t a

t p funz t

f

0 0

0 0

) ( )

(

) ( )

(

/ ) ( ) (

),...) (

( )

(

Simple example:

analytical solution

 

 

 

y x

v v 

0

v

 

 

  0 0 p

0

 

 

 

 9 . 8

m 0 f 

x

y

a is in this specific case:

one constant,

does not depend on pos

«ballistic shooting»

of a mass,

in 2D, ignoring friction...

(9)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 9

Simple example:

analytical solution

Solving…

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

2 0

0 0

0

2 / 8 . 9 8

. 0 9

) 0 ( )

(

8 . 8 9

. 9 ) 0

(

8 . 9 / 0

) ( )

(

8 . 9 ) 0

(

C C

y

C x t

y x t

C

C y

x t

y x C

C C

C

t t

v

t dt v

t v

dt v t

v p

t p

t v

dt v v

t v v

m t

f t

a

m t

f

C C

C

 

C C

t C

t C

C C

C C

dt t v p t

p

dt t a v

t v

m t f t a

t p fun t

f

0 0

0 0

) ( )

(

) ( )

(

/ ) ( ) (

),...) (

( )

(

Simple example:

analytical solution

Final result:

 

 

 

 

 

 

 

 

 

 

 

 

2

2

/ 8 . ) 9

(

8 . ) 9

(

8 . 9 ) 0

(

8 . 9 ) 0

(

C C

y

C x C

C y

x C

C C

t t

v

t t v

p

t v

t v v

t a

m t

f

x y

)

( t

C

p

(10)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 10

Some numerical methods

Forward Euler method

(simple and direct)

Leapfrog method

Verlet method

(position based dynamics)

Numerical method features

How efficient / expensive

must be at least soft real-time

(if from time to time computation delayed to next frame, ok)

How accurate

must be at least plausible

(if stays plausible, differences from reality are acceptable)

How robust

rare completely wrong results

(and never crash)

How generic

Which phenomena / constraints / object types is it able to recreate?

needs dependent from the context (ex: gameplay)

(11)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 11

Euler method integration

(1) Evaluate the force (on each particle)

as a function of position(even of other particles)

(2) acceleration

of each particle given by:

forceson it and its mass

(3) Update velocitywith acceleration

(4) Update positionwith velocity

(state / variables) , (temp variables)

For each step:

dt v

p p

dt a

v v

m f

a

p fun

f

 

0 0

/

,...) (

Euler methods

dt v

p p

dt a

v v

m f

a

p fun f

v p

 

/

...) ,

( ...

init

...

state

one

step

t  t  dt

(12)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 12

Simple example:

numerical solution

 

 

 

 

 

 

4 2

0

y x

v v  v

 

 

  0 0 p

0

 

 

 

 1

m 0 f 

x

y

constant (in this specific case not

dependent from pos) Same phenomena of previous example

 1 dt

here, for instance,

Simple example:

numerical solution

dt v p p

dt a v v

m f a

m f





/

1 0

init

step: 0 1 2 3 4 5 6 7

pos: (0,0) (2,3) (4,5) (6,6) (8,6) (10,5) (12,3) (14,0) vel: (2,4) (2,3) (2,2) (2,1) (2,0) (2,-1) (2,-2) (2,-3)

x y

0 1

2 3 4

5 6

7

step step step step step step step step

(13)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 13

Physics evolution computation

Analytical solutions:

Numerical solutions:

x y

0 1

2 3 4

5 6

7

x y

) ( C

y

x function t p

p 



Physics evolution computation

Analytical solutions:

Super efficient!

Close form solution

Accurate

Only simple systems

formulas found case by case

(often not existing!)

NO

(but, for instance, useful to allow the AI to make

predictions)

Numerical solutions:

Expensive (iterative)

but interactive

Integration errors

Flexible

Generic

YES

(14)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 14

Errors (of integration)

Dependent from dt

Small dt = more steps (with equal virtual time)

= more computationally expensive, but less error, more accurate simulation

(less difference from analytical solution, exact)

dt = 1.0 sec / FPS of physics simulation

(recall: not necessarily same rendering frame rate)

How much is the error increasing when dt change?

«Order» of the simulation

Euler is 1st order: the error is O( dt1 ) (but usually much less)

Keep on accumulating with time

(dependent also from )

Forces

In general, function of current position

Gravity

Friction

(these can be simulated using damping – later)

Opposition of solid materials

(But these well simulated using constraints… later)

Wind, electrical, magnetic, Archimedes force, springs, shock waves (explosions), etc etc …

“False” / “Magic” control forces

(added for controlling the evolution, not justified)

...

...) ,

( ...

p

fun

f  

(15)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 15

Forces: Springs

Simplified model for elastic forces

1 spring connects two particles Va and Vb

Characterized by:

Rest length L

Elastic constant K

Force:

counteract stretching

and compression

Va

Vb

Exerted on Va and on Vb:

Given by:

• direction: the spring one

• magnitude: ( distance(Va,Vb) – L )* K

• direction: the other endpoint if distance(Va,Vb) > L or the opposite otherwise

Forces: Springs

Simplified model for elastic forces

1 spring connects two particles Va and Vb

Characterized by:

Rest length L

Elastic constant K

Force:

counteract stretching

and compression

Va

Vb

b a

b b a

a

v v

v L v

v v

K

f 

 

 ( )

)

 (

(16)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 16

Forces: Springs

Useful for deformable objects, for instance cloth, ropes, hairs…

Extra springs, for resistance

to bending

Forces: Springs

Useful for deformable objects, for instance cloth, ropes, hairs…

img by msqrt (pauli kemppinen)

(17)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 17

Forces: Springs

Spring composition:

Elastic deformable objects

(tend to go back to original shape)

Idea: change rest length

(in response to compression/stretching)

Plastic deformable objects

(take permanently the deformed pose)

Rigid objects?

Similar concept, but using constraints, not springs

(“hard constraints”)

Later!

Forces: control

Example:

The player presses the forward button:

A forward force is applied to his/her avatar

(no justification)

(“Don’t ask questions, physics engine”)

According to some:

it’s better when that’s not done

Only physically justified forces (usually hard to obtain)

plausibility VS realism

(18)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 18

Continuity of pos and vel

In theory, in a physics system pos and vel can changed only continuously

In practice, sometimes is useful to insert discontinuous changes in both:

in positions: “teleportation”

in velocity: “impulses”

model very big forces exerted in a very small time interval

ex.: impacts, v. collision handling

(those are not necessary variations justified by forces)

Implulse

Force (continuous)

Continuous application

every frame

 

...

/ ...

dt m

f v

v      

Impulse

Infinitesimal time

una tantum

 

...

/ ...

m i

v

v   

direct change (discontinuous!) of state (velocity)

it models a very short and intense force (ex: impacts)

(19)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 19

Impulses VS Forces

Force :

determines acceleration

acc determines a change (continuous!) of vel

physically correct

Impulse :

(discontinuous!) change of vel

ex: implulse caused by the rebound of a tennis ball against a tennis racket

a force with:

application time dt approaching zero

but magnitude approaching infinity

physically correct?

approximation.

model phenomena that happen at dt much smaller than simulated ones

(what does truly happen when the ball is hit by the racket?)

very strong forces in very short time (but not instantaneous)

Euler pseudo code

Vec3 position = … Vec3 velocity = … void initState(){

position = … velocity = … }

void physicStep( float dt ) {

position += velocity * dt;

Vec3 acceleration = compute_force( position ) / mass;

velocity += acceleration * dt;

}

void main(){

initState();

while (1) do physicStep( 1.0 / FPS );

}

(20)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 20

Numerical methods:

integration steps

dt : delta of virtual time from last step

the “temporal resolution” of the simulation!

if small: less efficiency

more steps for simulating same amount of virtual time

if big: less accuracy

Specially with strong forces and/or high velocities

Common values: 1 / 60 sec -- 1 / 30 sec

note: not necess. same refresh rate of rendering task

order of the simulation:

how much the error is increasing when dt increase

Euler Method: limitations

bad ratio efficiency / accuracy

error accumulated over time = linear in dt

Even plausibility could be compromised

No energy conservation !

ex: divergent oscillationts!

patch: damping of velocity (at each step)

float damp = 1.0 – SMALL_CONST ; velocity *= damp;

Continuous loss of kinetic energy

Official excuse: “friction with… everything (air included)”

(21)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 21

Trick: velocity «Damping»

(or «Drag», resistance e.g. air resistance)

Vec3 position = … Vec3 velocity = … void initState(){

position = … velocity = … }

void physicStep( float dt ) {

Vec3 acceleration = force( positions ) / mass;

velocity += acceleration * dt;

position += velocity * dt;

}

void main(){

initState();

while (1) do physicStep( 1.0 / FPS );

}

velocity *= (1.0 – DRAG * dt);

Artificial reduction of kinetic energy:

• robustness

(avoids energy incr. due to num. errors) (ex. avoid divergent oscillations)

• Not modelled dispersions (various frictions)

• high damp = like moving in molasses

• aka DRAG (often: DRAG = 1-DAMP)

Sub problem:

If I want to decrease velocity of a percentage every second

ex 20%

DRAG = 0.2 (or DAMPING = 0.8)

how long do I have to multiply vel every dt ?

for enough small DRAG, well approx. by

Drag

dt

v

v     ( 1  ) ) 1

( dt Drag v

v      

1/FPS sec

Riferimenti

Documenti correlati

 perché sono più intuitivi (e pure più compatti). 

even the system does not (explicitly) handle rotations or angular velocities (works in 3D as well!).. Position

 If different parts of mesh associated to different textures: decompose the object in sub-mesh.. Texture maps assets and

 Strategy 1: use local lighting, but feed it a position dependent light environment.  precomputed

Marco Tarini - 2014 1 Game Engines+. Master Game Dev

– come sappiamo, cumulabile con qualunque altra trasf..

li stituti ecnici costituiscono un’articolazione dell’istruzione tecnica e rofessionale dotata di una ro ria identit culturale, che fa riferimento al profilo educativo,

Le valutazioni attribuite ai compiti scritti svolti a casa e ritirati occasionalmente ad alcuni alunni per monitorare l’impegno, l’applicazione delle competenze e delle