Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
15 views17 pages

Game Physics for 3D Developers

The document outlines a course on 3D video game physics, focusing on the Verlet integration method and its applications in game physics. It discusses characteristics, caveats, and the implementation of positional constraints, including examples of equidistance constraints and their use in modeling rigid bodies and other phenomena. Additionally, it highlights the advantages of using Position Based Dynamics for enforcing constraints in a flexible and robust manner.

Uploaded by

Aamir Shafique
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views17 pages

Game Physics for 3D Developers

The document outlines a course on 3D video game physics, focusing on the Verlet integration method and its applications in game physics. It discusses characteristics, caveats, and the implementation of positional constraints, including examples of equidistance constraints and their use in modeling rigid bodies and other phenomena. Additionally, it highlights the advantages of using Position Based Dynamics for enforcing constraints in a flexible and robust manner.

Uploaded by

Aamir Shafique
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

3D Video Games 2020-04-23

05: Game Physics - part3

Course Plan

lec. 1: Introduction 
lec. 2: Mathematics for 3D Games 
lec. 3: Scene Graph 
lec. 4: Game 3D Physics  + 
lec. 5: Game Particle Systems 
lec. 6: Game 3D Models 
lec. 7: Game Textures 
lec. 8: Game 3D Animations 
lec. 9: Game 3D Audio 
lec. 10: Networking for 3D Games 
lec. 11: Artificial Intelligence for 3D Games 
lec. 12: Game 3D Rendering Techniques 
90

Verlet integration method

 Idea: remove velocity from state


 Current velocity is implicit
 It’s defined from: 𝐩
 current pos 𝐩
𝐩
 last pos 𝐩
which we need to record

𝐩 =𝐩 + 𝑣⃗ · 𝑑𝑡 Euler & friends

𝑣⃗ = (𝐩 −𝐩 )/𝑑𝑡 Verlet

91

Marco Tarini
Università degli studi di Milano 1
3D Video Games 2020-04-23
05: Game Physics - part3

Verlet integration method

init 𝐩 =...
state 𝐩 = ...

𝑓⃗ = 𝑓𝑢𝑛(𝐩 ,…)
one
𝑎⃗ = 𝑓⃗/𝑚 expanding
this…
step 𝑣⃗ = (𝐩 − 𝐩 )/𝑑𝑡
𝑣⃗ = 𝑣⃗ + 𝑎⃗ ⋅ 𝑑𝑡
𝐩 =𝐩 + 𝑣⃗ ⋅ 𝑑𝑡

92

Verlet method

init 𝐩 ⟵ ...
state 𝐩 ⟵ ...

𝑓⃗ ⟵ 𝑓𝑢𝑛(𝐩 )
𝐩 ⟵𝐩
one 𝑎⃗ ⟵ 𝑓⃗/𝑚
step
𝐩 ⟵𝐩
𝐩 ⟵ 2𝐩 −𝐩 + 𝑎⃗ ⋅ 𝑑𝑡

93

Marco Tarini
Università degli studi di Milano 2
3D Video Games 2020-04-23
05: Game Physics - part3

Verlet: characteristics

 Velocity is kept implicit


 but that doesn’t save RAM:
we need so store previous position instead
 Good efficiency / accuracy ratio
 per step error: linear with dt
 accumulated error: order of dt2 (second order method)
 Extra bonus: reversibility
 it’s possible to go backward in t and
reach the initial state
 that, in theory… careful with implementation details

94

Verlet: caveats

 it assumes a constant dt (time-step duration)


 if it varies: corrections are needed! (which ones?)
 Q: how to act on velocity (which is now implicit)?
 e.g. to apply impulses
 A: change 𝐩 instead
 Q: how to act of positions w/o impacting velocity?
 e.g. to apply teleports / kinematic motions
 A: displace both 𝐩 and 𝐩 by the same amount
 Q: how to apply velocity damps?
 A: act on 𝐩 or 𝐩 (see below)
95

Marco Tarini
Università degli studi di Milano 3
3D Video Games 2020-04-23
05: Game Physics - part3

dt updates in Verlet
(if they are not constant)

Problem:
if 𝑑𝑡 now changes to a new 𝑑𝑡′
then, all pold must be updated to some 𝐩

current velocity 𝑣⃗
Find p'old : 𝑣⃗ = (𝐩 −𝐩 )/𝑑𝑡
and position 𝐩
𝑣⃗ = (𝐩 −𝐩 )/𝑑𝑡′ must not change

𝐩 =𝐩 ⋅ (𝑑𝑡 − 𝑑𝑡′)/𝑑𝑡 + 𝐩 ⋅ 𝑑𝑡′/𝑑𝑡

96

Velocity damping in Verlet


implicit

 Velocity at next frame: 𝑣⃗ = (𝐩 −𝐩 )/𝑑𝑡

 We want to multiply 𝑣⃗ a factor 𝑐 e.g. 0.98


 before applying accelerations obtained as
(1-𝑑𝑡·𝑐 )
 We can do that using a more general formula for 𝐩

𝐩 = 2 ⋅𝐩 − 1 ⋅𝐩 + 𝑑𝑡 ⋅ 𝑎⃗

𝐩 = 1+𝑐 ⋅𝐩 −𝑐 ⋅𝐩 + 𝑑𝑡 ⋅ 𝑎⃗
98

Marco Tarini
Università degli studi di Milano 4
3D Video Games 2020-04-23
05: Game Physics - part3

Velocity damping in Verlet


(geometric interpretation)
a bit shorter

𝑣⃗ 0.98𝑣⃗
𝐩 𝐩
𝑣⃗ 𝑣⃗
𝐩 𝐩

𝐩 𝐩

𝐩 = 2 ⋅𝐩 − 1 ⋅𝐩 𝐩 = 1.98 ⋅ 𝐩 − 0.98 ⋅ 𝐩

That is , That is ,
𝐩 is an extrapolation 𝐩 is a different extrapolation
of 𝐩 , 𝐩 : of 𝐩 , 𝐩 :

𝐩 = 𝑚𝑖𝑥( 𝐩 , 𝐩 , 2) 𝐩 = 𝑚𝑖𝑥( 𝐩 , 𝐩 , 1.98)

99

Verlet with PBD


“Position Based Dynamics”
init 𝐩 ⟵ ...
state 𝐩 ⟵ ...

𝑓⃗ ⟵ 𝑓𝑢𝑛(𝐩 )
𝐩 =𝐩
𝑎⃗ ⟵ 𝑓⃗/𝑚 𝐩 =𝐩
one
step 𝐩 ⟵ 2𝐩 −𝐩 + 𝑎⃗ ⋅ 𝑑𝑡

Enforce constraints on (𝐩 ) 💡

100

Marco Tarini
Università degli studi di Milano 5
3D Video Games 2020-04-23
05: Game Physics - part3

Position Based Dynamics


a formula
 A positional constraint is with ‘=‘ ‘>’ ‘<‘ etc.

an equation/inequality
involving the positions of particles.
 Useful, for example, to model consistency conditions
 Like “solid objects don’t compenetrate each other”,
or “steel bars won’t bend”
 We will see specific examples soon
 We enforce (impose) positional constraint directly
💡
by displacing the positions of particles
 Thanks to Verlet: this displacement automatically cause
some appropriate update of the velocity!
 not necessarily the correct one, but a plausible one

101

Verlet + Position Based Dynamics.


Advantages
 flexibility: different constraints can be used to model
many different phenomena
 Useful constraints are straightforward to define
 They are easy to impose (they involve only few particles)
 They can be used to model many possible phenomena
 Esamples: see following slides
 robustness : plausibility is ensured by explicitly
enforced the conditions we want to see
 For exampe: a ball won’t ever be seen outside the box
containing it (at lest, not for long)
 Bypasses the need to using forces / impulses to enforce
the same consistency condition
 Which is much more difficult to enforce

102

Marco Tarini
Università degli studi di Milano 6
3D Video Games 2020-04-23
05: Game Physics - part3

Example of positional constraint

«Particles must stay


within [0 – 100] x [0 – 100] »
100 Imposing constraint: simple clamp !
a
ex:
for(int i=0; i<NUM_PARTICLES; i++)
b
{
p[i].x = clamp( p[i].x, 0, 100 );
p[i].y = clamp( p[i].y, 0, 100 );
}
0 100

Imposing constraints like this is a first part of collision response.


For re-bounces, impulses must still be added (see collisions).
103

Example of positional constraint:


equidistance constraint
«Particles a and b must stay at distance d »

104

Marco Tarini
Università degli studi di Milano 7
3D Video Games 2020-04-23
05: Game Physics - part3

Enforce equidistance constraints

if 𝐩 − 𝐩 >𝑑

if 𝐩 − 𝐩 <𝑑

105

Enforce equidistance constraints:


pseudo code
Vector3 pa, pb; // curr positions of a,b
float d; // distance (to enforce)

Vector3 d = pa – pb;
float currDist = v.length;

d /= currDist; // normalization of d

float delta = currDist – d ;

pa += ( 0.5 * delta) * d;
pb -= ( 0.5 * delta) * d;
assuming equal mass, each particle moves half the way
(see later for the general case)

106

Marco Tarini
Università degli studi di Milano 8
3D Video Games 2020-04-23
05: Game Physics - part3

Enforcing sets of constraints

 Many constraints to impose:


when you solve one  you break another one!
 Simultaneous enforcement: computationally expensive

 Practical solution: enforce them in cascade


(Gauss-Seidel fashon):

Constr. Constr. ... Constr.


1 2 N

Repeat until convergence (= max error below threshold)


…but at most for N times! even 1 (remember: soft real time)

107

Enforcing sets of constraints

 Note:
 The whole loop for imposing the constraints happen in
the constraint enforcement phase on one physics step!
 Convergence:
 if constraints are not contradictory
 if convergence not reached (or solution doesn’t exist):
never mind, next frames will fix it (it’s fairly robust)
 needed iterations (typically): 1 ~ 10 (efficient!).
 Optimization (to decrease number of needed iterations):
solve the most unsatisfied constraints first
 Problem: it’s a sequential approach! 
 parallelized versions (similar to Jacobi) are possible
 they have a worse convergence in practice
(they require more iterations)
108

Marco Tarini
Università degli studi di Milano 9
3D Video Games 2020-04-23
05: Game Physics - part3

Equidistance constraints
VS springs
 They are similar
 they both mean:
these 2 particles “want to be” at this distance (not more, not less)
 Differences:
 equidistance constraint:  spring:
 applied during  applied during
constraint enforcement force evaluation step
 directly affecting  affecting forces,
positions therefore accelerations
 models a rigid rod  models a deformable spring
between the two particles between the two particles
 of a given length  of a given length
 sometimes called  sometimes called
an “HARD” constraint a “SOFT” constraint
 A physic engine can combine them in one object!
109

We can combine equidistance


constraints to obtain…
 Rigid bodies

 Articulated bodies

 Ragdolls

 Cloth

 Non-elastic ropes

 And more
110

Marco Tarini
Università degli studi di Milano 10
3D Video Games 2020-04-23
05: Game Physics - part3

Compounds of particles
disguised as rigid bodies

111

Combining equidistance constraints


we obtain rigid objects
 Rigid body dynamics
as emerging behavior
 without explicitly updating
their orientation, angular vel,
angular acc., etc.
A box?
(rigid object)
A configuration of:
• 4 particles
• 6 equidistance constraints

112

Marco Tarini
Università degli studi di Milano 11
3D Video Games 2020-04-23
05: Game Physics - part3

Example

NO NO NO

STEP 0 STEP 1 STEP 1


before constraints after 1st constraint

113

Example

In total: the “box”,


under gravity + impact
• had rotated
• gained angular velocity
(will keep rotating by
inertia)
even the system does not
(explicitly) handle rotations
or
NO NO angular velocities

(works in 3D as well!)

STEP 1 STEP 1
after all constraints (implicit) velocities
multiple times
114

Marco Tarini
Università degli studi di Milano 12
3D Video Games 2020-04-23
05: Game Physics - part3

More examples of
positional constraints
 Preserve volume of some object: «Volume is 𝑣 »
 How to impose it:
1. Estimate current total volume 𝑣
2. uniform scaling of the entire object of 𝑣 /𝑣
 Fixed positions: «particle 𝑎 stays in 𝐩 »
 particles «pinned in position»
 trivial to impose, but useful!
𝐩
 Angle constraints, e.g. 𝛂 < 𝛂 𝛂
 e.g. on joints: «elbows cannot bend backward» 𝐩
 Coplanarity / collinearity 𝐩

 Non interpenetration
 this is part of collision handling – see collisions later

115

Enforcing a positional constraint:


the general case.
 Check: does the equation/inequality hold?
 If so, nothing to do!
 Else:
 All positions must be displaced a bit so that it does
 Infinite ways to achieve this. Which one to pick?
 Answer:
minimize the sum of squared displacements
(with respect to current position)
weighted by particle masses
 Find minimizer by analytically solving simple problems
(in closed form, “analytically” = “on paper”)
116

Marco Tarini
Università degli studi di Milano 13
3D Video Games 2020-04-23
05: Game Physics - part3

Enforcing a positional constraint


the general case: formally problem
 We want to enforce a constraint 𝒞 on particles a , b , c,…
with have mass ma, mb, mc …
 𝒞 defined as an equation/inequality of their positions pa , pb , pc , …

 We must apply the displacements 𝑑 , 𝑑 , 𝑑


which minimize:

argmin ma 𝑑 + mb 𝑑 + mc 𝑑 +⋯
, , ,…

such that 𝒞 pa + 𝑑 , pb + 𝑑 , pc + 𝑑 ,…

among all the choices that satisfy this,


we want the one which minimizes this

117

Enforcing positional constraint


Example: equidistance constraint
 To enforce the constraint
“particles a and b must stay at distance k ”
 input: current positions pa, pb
 input: masses ma, mb
 We need to the the displacements 𝑑 , 𝑑
found by minimizing:
argmin ma 𝑑 + mb 𝑑
,
such that pa + 𝑑 − pb + 𝑑 =𝑘
 And the solution (in closed form) is…
118

Marco Tarini
Università degli studi di Milano 14
3D Video Games 2020-04-23
05: Game Physics - part3

Equidistance constraints: solution for


non-equal masses
Vector3 pa, pb; // curr positions of a,b
float ma, mb; // masses of a,b
float d; // distance (to enforce)

Vector3 v = pa – pb;
float currDist = v.length;

v /= currDist; // normalization of v

float delta = currDist – d ;

/* solutions of the minimization: */


pa += ( mb/(ma+mb) * delta) * v;
pb -= ( ma/(ma+mb) * delta) * v;
119

Enforcing positional constraint


Example: “don’t sink into a plane”
 We want to enforce the constraint
“particle a must be above a constant plane ”
 Given: position of the particle pa and its mass ma
 Point on a plane pq and its normal (unit vec) 𝑛
 We need to apply the displacement 𝑑
found by minimizing:
argmin ma 𝑑
,
such that pa − pq 𝑛 >0
 And the solution (in closed form) is, trivially…
120

Marco Tarini
Università degli studi di Milano 15
3D Video Games 2020-04-23
05: Game Physics - part3

In pseudocode

Vector3 pa; // curr positions of a


float ma; // mass (no effect here)
Vector3 pq; // point on the plane
Vector3 nq; // normal of the plane (unit)

Vector3 v = pa – pq;
float currDist = Vector3.dot( v , n );

if (currDist < 0.0)


pa -= currDist * n; // just project!
else {} // constrain holds, do nothing

121

Rigid objects as compounds of


constrained particles: advantages
 Interesting/rich/useful set of “emerging behaviors”
(i.e. effects with “just automatically happens”) :
 rigid, deformable, jointed objects
 made of particles + hard constraints consequence
 their angular velocities you don’t of
 rotation around proper axis need to constraints
compute disallowing
 their barycenter
or store compene-
 their momentum of inertia these tration
 angular velocity is maintained

 somewhat believable bounces on “impacts”


 for more control: impact impulses can be added (see collisions)
122

Marco Tarini
Università degli studi di Milano 16
3D Video Games 2020-04-23
05: Game Physics - part3

Particles + constraint,
or rigid bodies?
 Rigid-body based systems:
 explicitly compute dynamics for rigid bodies
 updating their rotation, angular speed,…
 Particles-based systems:
 only compute dynamics for particles
 rigid (or deformable, or jointed) bodies
as an emerging behavior
 Mixed systems:
 use both
 may even dynamically swap between the
two representations for rigid bodies

124

Rigid body as particles + constraints:


Challenges
 Approximations are introduced
 e.g.: mass is concentrated in a few locations
 Scalability issues
 many constraints to enforce, many particles to track
 Some of the info which is kept implicit
is needed by the rest of the game engine
 and must therefore be extracted 
 example: the transform (position + orientation) of the
“rigid body” is needed to render the associated mesh
 similarly: angular speed, barycenter pos, velocity…

125

Marco Tarini
Università degli studi di Milano 17

You might also like