• Non ci sono risultati.

# constants: they don’t (usually) change

N/A
N/A
Protected

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

### 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)

### the center of mass

(2)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 2

### (an object rotates around its barycenter)

high

low

(3)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 3

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

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

### (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

## 

0 0

### (

forces

acceler.

velocity positions

(7)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 7

C C

t C

t C

C C

C C

0 0

0 0

### 

pos, acc, vel, forces:

in function of current time

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

### state = function( t )

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

( t = 0)

( t + 1)

t

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

) ( )

(

) ( )

(

/ ) ( ) (

),...) (

( )

(

y x

0

0

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

2 0

0 0

0

C C

y

C x t

y x t

C

C y

x t

y x C

C C

C

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

) ( )

(

) ( )

(

/ ) ( ) (

),...) (

( )

(

2

C C

y

C x C

C y

x C

C C

C

### p

(10)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 10

### 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:

## 

init

state

one

step

### t  t  dt

(12)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 12

0

y x

0

### y

constant (in this specific case not

dependent from pos) Same phenomena of previous example

###  1 dt

here, for instance,

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

x y

0 1

2 3 4

5 6

7

x y

) ( C

y

x function t p

p 



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

### 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 )

### Friction

(these can be simulated using damping – later)

### Opposition of solid materials

(But these well simulated using constraints… later)

### “False” / “Magic” control forces

(added for controlling the evolution, not justified)

### f  

(15)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 15

### Characterized by:

Rest length L

Elastic constant K

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

### Characterized by:

Rest length L

Elastic constant K

Va

Vb

b a

b b a

a

###  (

(16)

Video Game Dev - Univ Insubria 2017/2018

Marco Tarini

Game Physics - 2 16

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

Extra springs, for resistance

to bending

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

### A forward force is applied to his/her avatar

(no justification)

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

### 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)

### 

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

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

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

### (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)

dt

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