stop my constraints from blowing up!...8 let’s iterate step distance relative velocity distance...

51
1 Stop my Constraints from Blowing Up! Oliver Strunk (Havok)

Upload: others

Post on 21-Jun-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

1

Stop my Constraints from Blowing Up!

Oliver Strunk

(Havok)

Page 2: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

2

OverviewJust understanding a rigid-body solver algorithm is not enough.

(google for ‘ragdoll glitch’ and enjoy)

SolverAlgorithm

Page 3: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

3

Chapter I: How does a solver work?

2 meter

198 kg198 kg

2 kg

Page 4: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

4

‘Position’ only Solver

Each Bavarian moves the mug to his boat every other second.

The center of mass of ‘Bavarian + Beer’ must stay constant:

the mass ratio mug : Bavarian = 1:99

-> the mug will move 198cm and the Bavarian 2cm.In the end the mug and the Bavarian moved and all velocities are 0.

In the next second the other Bavarian will do the same.

Repeat

Page 5: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

5

Lets iterateStep Distance

1 2.00

2 1.98

3 1.96

4 1.94

5 1.92

10 1.83

100 .74 ~2/e0.00

0.50

1.00

1.50

2.00

2.50

1 5 9

13

17

21

25

29

33

37

41

45

49

53

57

61

65

69

73

77

81

85

89

93

97

Page 6: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

6

Now use velocities

2 meter

198 kg198 kg

2 kg

Page 7: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

7

‘Position and Velocity’ Solver

Each Bavarian now pulls the rope every other second such that the mug arrives at his boat after one second.

Mass * velocity (moment) must stay constant:

-> If the beer is accelerated to 1.98 meters/second, the Bavarian will be accelerated to 2.0cm/second.After one second: the mug reaches the boat and both mug and Bavarian have some velocity.

In the next second the other Bavarian will pull the rope and reverse the velocity of the mug.

Page 8: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

8

Let’s iterate

Step DistanceRelativeVelocity

DistancePrev. Solver

1 2 0.020 2.00

2 1.98 0.060 1.98

3 1.92 0.098 1.96

4 1.82 0.134 1.94

5 1.69 0.168 1.92

6 1.52 0.199 1.90

7 1.32 0.225 1.88

8 1.10 0.247 1.86

9 0.85 0.264 1.85

10 0.59 0.276 1.83

11 0.31 0.282 1.81

12 0.03 0.282 1.79

0.00

0.50

1.00

1.50

2.00

2.50

1 2 3 4 5 6 7 8 9 10 11 12

Page 9: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

9

Lessons Learned

2 constraints fighting each other iteratively solves the

global problem (=bringing the boats together).

We saw 2 types of iterative solvers:

– Strength ~ num iterations (convergence)

– Strength ~ num iterations2, but

• this adds energy

Page 10: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

10

2 meter

100 000 000 kg100 000 000 kg

2 kg

Can We Pull an Ocean-Liner Using a Mug?

Page 11: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

7000 iterations of our beer-mug solver will move this ship by 1 meter.

Page 12: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

12

Lets look into details.

Solver ‘strength’ depends on:

– Solver type (e.g. linear/quadratic)

• We want fast convergence without the risk of instability.

– Number of solver iterations (per second)

• We want as few as possible to save CPU.

– Mass ratio of the objects involved

• Needs to be as small as possible to allow for small number of iterations.

Page 13: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

13

Solver TypeStatement:

• All quadratic/linear convergence solvers have similar ‘strength’.

• Solver variants found in the literature:

• Position/velocity based solver.

• Error correction by post-projection,

split impulse or Baumgarte-stabilization.

Page 14: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

14

Number of Solver Iterations

Say n=iterations/sec, d=distance

– > we accelerate the body n-times per second

to d*n velocity -> acceleration = n2 *d

n [Hz] d [meter] acceleration acc/gravity

30 0.05 45 4.5

120 0.05 720 72

240 0.01 576 57.6

1000 0.10 100000 10000

Page 15: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

15

Number of Solver Iterations

Observations:

Running one solver iteration per frame (30Hz) is not good enough.

Running a pure quadratic convergent solver higher than frame frequency can lead to instability.

Most game physics engine solvers use quadratic convergence using frame frequency (30Hz) and use linear convergence using sub-iterations (4-10).

Page 16: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

16

Mass Ratio:

High mass ratios require lots of solver iterations, so keep mass ratio low!

Avoid calculating the mass from density automatically:

Guessing density is tricky:

– What is the density of a car / a 747 ?

– Problem: solid vs. hollow objects

– -> So let your artist set the mass not the density.

A tank driving either over a 10cm metal or a wooden box makes no difference:

– Increase your masses on small debris objects

– Exceptions: bullets and rockets

Advice: Don’t tweak mass if you don’t have a problem yet.

Page 17: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

17

Mass-Ratio in a 3d-World

In a 1d-universe, the mass ratio just depends on

the mass of the objects.

But games are not 1d �, so 2d/3d rigid bodies

can rotate. As a result the mass ratio depends

on mass and inertia (=‘angular mass’).

Page 18: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

18

Typical ‘Bad’ Example

arm

Clavicle bone

torso

Lets assume:

– All joints are limited.

– All joints have reached their limit.

– Shoulder bone is 4x smaller than arm.

– Mass is calculated from density.

Page 19: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

19

Let’s simplify Lets move the pivot to the mass center of the combined arm. (in a fixed constraint we can do this without changing behavior).

We see: We virtually apply a force at the shoulder bone way outside its shape.

Is this bad?

Yes, it results in very poor solver strength because the ‘effective’ mass ratio gets extremely high (1 : 3000)

Page 20: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

20

Effective Mass

impulse

Velocity =

impulse/mass

Page 21: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

21

Effective Mass

Impulse

Angular vel.

Linear vel.

velocity at = linearVel + d*angularVel

= impulse/mass + d*impulse/angularMass(d)

angularMass(d) = inertia/d

velocity = impulse / effMass

effMass = 1 / (1/mass + d2/inertia)

Point vel.

Center of Mass

d

Page 22: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

22

Effective Mass ExampleDimensions: 1meter * 20cm * 20cm

Mass: 100kg (= density: 2.5kg/litre)

Distance: = .5 meter.

Inertia: 100/12* (1*1 + .2*.2) = 8.6 kg m2

Effmass at = 1/(1/100 + .52/8.6) = 25kg

Page 23: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

23

Lets make the cube 4x smallerDimensions: .25meter * 5cm * 5cm

Mass: 1.56kg (= density: 2.5kg/litre)

Distance: = .5 meter.

Inertia: 1/12*1.56*(.25*.25 + .05*.05) = 8.46e-5

Total effMass at = 33.13g

cube 4 times smaller -> eff.Mass drops by 750 !!!

Page 24: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

24

Summary Chapter 1

Solver strength mainly depends on:

– Number of solver iterations

– Mass ratio

Rotations are bad!

– Angular movement reduces our effective

mass and therefore our solver strength.

Page 25: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

25

Chapter 2: Angular Effects

Angular movement not only reduces effective mass but also leads to instability!

Demo

Page 26: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

26

Angular Effects

We have 2 distance constraints (distance d)

between a ball and 2 fixed walls.

d

d

Page 27: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

27

Angular Effects

If we move the walls apart, we’ll have to move

the ball to satisfy the constraints:

d

d

d

d

Page 28: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

28

Angular Effects

How will a perfect algorithm solve this?

d

d

Page 29: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

29

Angular Effects

We are forced to linearize the equations of motion.

d

d

Page 30: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

30

Angular Effects

Lets move the walls a little bit further:

d

d

Page 31: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

31

Angular Effects

A solver will gain energy if it “overshoots” too much.

The likelihood of overshoots increases if the solver ‘miss-

predicts’ the angular movement:

– Because of linearization, the force direction (=Jacobian) is not optimally chosen, especially when running ‘building the jacobian’-algorithm at low frequency.

– Angular velocity is high compared to linear velocity.

– Effective angular mass is much less than the body mass and high forces are applied:

inertia/d2 / mass << 1.0

Page 32: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

32

Lessons Learned

Low inertias are the main problem for bad solver behavior!!!

– They increase the effective mass ratios between bodies

– They lead to high angular velocities, which lead to ‘explosions’/jitter

Solution:

– Increase inertias

Page 33: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

33

Increasing Inertia

Demo

Page 34: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

34

Increasing Inertia

Lessons learned:

We can increase inertia of selected single

objects easily by factor of 2 - 4 before users spot

serious artifacts.

Page 35: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

35

Inertia Visualizations

We can visualize the inertia by drawing a box

which would have the same inertia.

Inertia

Object

Inertia

Object with increased

inertia

Page 36: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

36

Combining Inertias

Page 37: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

37

Combining Inertias

Page 38: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

38

Demo Of Ragdoll With Increased Inertia

Demo

Page 39: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

39

Lessons Learned

Increasing inertia matrix is very often quite acceptable in

a game environment and artifacts are hardly noticeable.

– Especially true for small bodies inside a chain of

constraint bodies (like shoulder bone).

Page 40: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

40

How to Increase the Inertia Matrix

Multiply by a factor

(single bodies)

Add a constant to the diagonal(bodies in a constraint chain)

Page 41: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

41

Summary Chapter 2

To improve solver stability and ‘strength’:

Increase inertia

Increase simulation frequency

Decrease mass ratio

Advice: Don’t try to damp rigid bodies or the solver

Page 42: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

42

Chapter 3: Reducing RigidityOur physics engine uses rigid bodies. Rigid means 100% rigid.

– Bodies never deform.

– Bodies never break.

– Impulses and friction forces have no limit:

-> Physics engine can feel ‘cute and bouncy’

Page 43: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

43

Implementing Soft Contact

Reduce the maximum contact impulse to allow

for some penetration.

– E.g. clip the impulse.

Ensure that the penetration recovery happens

slowly to avoid springy behavior.

– E.g. reduce the Baumgarte stabilization for

this contact.

Page 44: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

44

Soft Contact

Demo:

Page 45: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

45

Destruction

Idea: destroy bodies if impulses get too high.

Page 46: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

46

DestructionSolution 1: Estimate the contact impulse and destruct the

object before running the solver:

– Impulse = relativeVelocity * effectiveMass

– This works pretty well, except if the breakable object is blocked by an unbreakable object.

Fast

bullet

Destructible

wallUnbreakable

Box

Page 47: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

47

Destruction

Solution 2: After running the solver:If the contact impulse exceeds a limit, break the object

– If implemented naively, breakable fixed objects will stop any moving object.

Fast

bullet Destructible

wall

1st frame:

Stopped

bullet Destroyed

wall

2nd frame:

Page 48: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

48

Destruction

Solution 2b: Extend solution 2 with the following modifications:

– The solver also clips the impulses to the breaking limit.

– If an object breaks, move the two objects involved to the previous position.

1st frame: 2nd frame

Just after solve:

2nd frame after destruct

and position move:

Page 49: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

49

Destruction

Demo

Page 50: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

50

Summary Chapter 3

Impulse clipping in the solver allows to emulate

soft, deformable or breakable materials.

Page 51: Stop my Constraints from Blowing Up!...8 Let’s iterate Step Distance Relative Velocity Distance Prev. Solver 1 2 0.020 2.00 2 1.98 0.060 1.98 3 1.92 0.098 1.96 4 1.82 0.134 1.94

51

The End

Thanks for listening, questions welcome ☺

SolverAlgorithm