GAME PHYSICS ENGINE DEVELOPMENT - GBV

10
GAME PHYSICS ENGINE DEVELOPMENT IAN MILLINGTON i > AMSTERDAM • BOSTON • HEIDELBERG fPf|||l|| LONDON. NEW YORK. OXFORD __ ^ _ _ . PARIS SAN DIEGO • SAN FRANCISCO вТС^Н Г^ 4 .«Mt-fSSKHbe. SINGAPORE . SYDNEY . TOKYO ELSEVIER Morgan Kaufmann is an imprint of Elsevier MORGAN KAUFMANN PUBLISHERS

Transcript of GAME PHYSICS ENGINE DEVELOPMENT - GBV

Page 1: GAME PHYSICS ENGINE DEVELOPMENT - GBV

GAME PHYSICS ENGINE

DEVELOPMENT

IAN MILLINGTON

i > AMSTERDAM • BOSTON • HEIDELBERG f P f | | | l | | LONDON. NEW YORK. OXFORD __ ^ _ _ .

PARIS • SAN DIEGO • SAN FRANCISCO вТС^Н Г̂ 4 .«Mt-fSSKHbe. SINGAPORE . SYDNEY . TOKYO ELSEVIER Morgan Kaufmann is an imprint of Elsevier M O R G A N K A U F M A N N P U B L I S H E R S

Page 2: GAME PHYSICS ENGINE DEVELOPMENT - GBV

CONTENTS

L I S T O F F I G U R E S xvi

P R E F A C E xix

CHAPTER

A I N T R O D U C T I O N I

1.1 W H A T I S G A M E P H Y S I C S ? 2

1.2 W H A T I S A P H Y S I C S E N G I N E ? 2

1.2.1 Advantages of a Physics Engine 3 1.2.2 Weaknesses of a Physics Engine 4

1.3 A P P R O A C H E S TO P H Y S I C S E N G I N E S 5

1.3.1 Types of Object 5 1.3.2 Contact Resolution 5 1.3.3 Impulses and Forces 6 1.3.4 What We're Building 7

1.4 T H E M A T H E M A T I C S OF P H Y S I C S E N G I N E S 7

1.4.1 The Math You Need to Know 8 1.4.2 The Math We'll Review 9 1.4.3 The Math We'll Introduce 10

1.5 T H E S O U R C E C O D E I N T H I S B O O K 10

1.6 H o w T H I S B O O K I S S T R U C T U R E D 11

P A R T I P A R T I C L E P H Y S I C S 1 3

C H A P T E R

Zä T H E M A T H E M A T I C S O F P A R T I C L E S 15

2.1 V E C T O R S 15

2.1.1 The Handedness of Space 19

Vll

Page 3: GAME PHYSICS ENGINE DEVELOPMENT - GBV

viii Contents

2.1.2 Vectors and Directions 20 2.1.3 Scalar and Vector Multiplication 23 2.1.4 Vector Addition and Subtraction 24 2.1.5 Multiplying Vectors 27 2.1.6 The Component Product 28 2.1.7 The Scalar Product 29 2.1.8 The Vector Product 31 2.1.9 The Orthonormal Basis 35

2.2 C A L C U L U S 35

2.2.1 Differential Calculus 36 2.2.2 Integral Calculus 40

2.3 S U M M A R Y 42

CHAPTER

T H E L A W S O F M O T I O N 43

3.1 A P A R T I C L E 43

3.2 T H E F I R S T T W O L A W S 44

3.2.1 The First Law 45 3.2.2 The Second Law 46 3.2.3 The Force Equations 46 3.2.4 Adding Mass to Particles 47 3.2.5 Momentum and Velocity 48 3.2.6 The Force of Gravity 48

3.3 T H E I N T E G R A T O R 50

3.3.1 The Update Equations 51 3.3.2 The Complete Integrator 52

3.4 S U M M A R Y 54

CHAPTER

T H E P A R T I C L E P H Y S I C S E N G I N E 55

4.1 B A L L I S T I C S 55

4.1.1 Setting Projectile Properties 56 4.1.2 Implementation 57

4.2 F I R E W O R K S 60

4.2.1 The Fireworks Data 60 4.2.2 The Fireworks Rules 61 4.2.3 The Implementation 63

4.3 S U M M A R Y 66

Page 4: GAME PHYSICS ENGINE DEVELOPMENT - GBV

Physics Engine Design ix

PART II MASS-AGGREGATE PHYSICS 67

C H A P T E R

Э ADDING GENERAL FORCES 69

5.1 D ' A L E M B E R T ' S P R I N C I P L E 69

5.2 F O R C E G E N E R A T O R S 72

5.2.1 Interfaces and Polymorphism 73 5.2.2 Implementation 73 5.2.3 A Gravity Force Generator 76 5.2.4 A Drag Force Generator 77

5.3 B U I L T - I N G R A V I T Y A N D D A M P I N G 79

5.4 S U M M A R Y 79

C H A P T E R

О SPRINGS AND SPRINGLIKE THINGS 81

6.1 H O O K ' S L A W 81 6.1.1 The Limit of Elasticity 83 6.1.2 Springlike Things 83

6.2 S P R I N G L I K E F O R C E G E N E R A T O R S 83

6.2.1 A Basic Spring Generator 84 6.2.2 An Anchored Spring Generator 86 6.2.3 An Elastic Bungee Generator 87 6.2.4 A Buoyancy Force Generator 89

6.3 S T I F F S P R I N G S 93

6.3.1 The Problem of Stiff Springs 93 6.3.2 Faking Stiff Springs 95

6.4 S U M M A R Y 101

C H A P T E R

H A R D C O N S T R A I N T S ЮЗ

7.1 S I M P L E C O L L I S I O N R E S O L U T I O N 103

7.1.1 The Closing Velocity 104 7.1.2 The Coefficient of Restitution 105 7.1.3 The Collision Direction and the Contact Normal 105 7.1.4 Impulses 107

Page 5: GAME PHYSICS ENGINE DEVELOPMENT - GBV

x Contents

7.2 C O L L I S I O N P R O C E S S I N G 108 7.2.1 Collision Detection 111 7.2.2 Resolving Interpenetration 112 7.2.3 Resting Contacts 116

7.3 T H E C O N T A C T R E S O L V E R A L G O R I T H M 119 7.3.1 Resolution Order 120 7.3.2 Time-Division Engines 124

7.4 C O L L I S I O N L I K E T H I N G S 125 7.4.1 Cables 126 7.4.2 Rods 128

7.5 S U M M A R Y 131

C H A P T E R

8 T H E M A S S - A G G R E G A T E P H Y S I C S E N G I N E Ш

8.1 O V E R V I E W O F T H E E N G I N E 133

8.2 U S I N G T H E P H Y S I C S E N G I N E 139 8.2.1 Rope-Bridges and Cables 139 8.2.2 Friction 140 8.2.3 Blob Games 141

8.3 S U M M A R Y 142

P A R T III R I G I D - B O D Y P H Y S I C S 1 4 3 C H A P T E R

T H E M A T H E M A T I C S O F R O T A T I O N S 145

9.1 R O T A T I N G O B J E C T S IN T W O D I M E N S I O N S 145 9.1.1 The Mathematics of Angles 146 9.1.2 Angular Speed 148 9.1.3 The Origin and the Center of Mass 148

9.2 O R I E N T A T I O N IN T H R E E D I M E N S I O N S 152 9.2.1 Euler Angles 153 9.2.2 Axis-Angle 155 9.2.3 Rotation Matrices 156 9.2.4 Quaternions 157

9.3 A N G U L A R V E L O C I T Y A N D A C C E L E R A T I O N 159 9.3.1 The Velocity of a Point 160 9.3.2 Angular Acceleration 160

Page 6: GAME PHYSICS ENGINE DEVELOPMENT - GBV

Physics Engine Design xi

9.4 I M P L E M E N T I N G T H E M A T H E M A T I C S 161

9.4.1 The Matrix Classes 161 9.4.2 Matrix Multiplication 162 9.4.3 The Matrix Inverse and Transpose 171 9.4.4 Converting a Quaternion to a Matrix 178 9.4.5 Transforming Vectors 180 9.4.6 Changing the Basis of a Matrix 184 9.4.7 The Quaternion Class 186 9.4.8 Normalizing Quaternions 187 9.4.9 Combining Quaternions 188 9.4.10 Rotating 189 9.4.11 Updating by the Angular Velocity 190

9.5 S U M M A R Y 191

C H A P T E R

10 L A W S O F M O T I O N F O R R I G I D B O D I E S 193

10.1 T H E R I G I D B O D Y 193

10.2 N E W T O N 2 F O R R O T A T I O N 196

10.2.1 Torque 197 10.2.2 The Moment of Inertia 198 10.2.3 The Inertia Tensor in World Coordinates 202

10.3 D ' A L E M B E R T F O R R O T A T I O N 205

10.3.1 Force Generators 208

10.4 T H E R I G I D - B O D Y I N T E G R A T I O N 210

10.5 S U M M A R Y 212

CHAPTER

11 T H E R I G I D - B O D Y P H Y S I C S E N G I N E 213

11.1 O V E R V I E W O F T H E E N G I N E 213

11.2 U S I N G T H E P H Y S I C S E N G I N E 216

11.2.1 A Flight Simulator 216 11.2.2 A Sailing Simulator 222

11.3 S U M M A R Y 227

Page 7: GAME PHYSICS ENGINE DEVELOPMENT - GBV

xii Contents

P A R T IV COLLISION DETECTION 2 2 9

C H A P T E R

12 C O L L I S I O N D E T E C T I O N 231

12.1 C O L L I S I O N D E T E C T I O N P I P E L I N E 232

12.2 C O A R S E C O L L I S I O N D E T E C T I O N 232

12.3 B O U N D I N G V O L U M E S 233

12.3.1 Hierarchies 235 12.3.2 Building the Hierarchy 241 12.3.3 Sub-Object Hierarchies 250

12.4 S P A T I A L D A T A S T R U C T U R E S 251

12.4.1 Binary Space Partitioning 251 12.4.2 Oct-Trees and Quad-Trees 255 12.4.3 Grids 258 12.4.4 Multi-Resolution Maps 260

12.5 S U M M A R Y 261

C H A P T E R

13 G E N E R A T I N G C O N T A C T S 263

13.1 C O L L I S I O N G E O M E T R Y 264

13.1.1 Primitive Assemblies 264 13.1.2 Generating Collision Geometry 265

13.2 C O N T A C T G E N E R A T I O N 265

13.2.1 Contact Data 267 13.2.2 Point-Face Contacts 269 13.2.3 Edge-Edge Contacts 269 13.2.4 Edge-Face Contacts 271 13.2.5 Face-Face Contacts 271 13.2.6 Early-Outs 272

13.3 P R I M I T I V E C O L L I S I O N A L G O R I T H M S 273

13.3.1 Colliding Two Spheres 274 13.3.2 Colliding a Sphere and a Plane 276 13.3.3 Colliding a Box and a Plane 279 13.3.4 Colliding a Sphere and a Box 282 13.3.5 Colliding Two Boxes 287 13.3.6 Efficiency and General Polyhedra 297

13.4 S U M M A R Y 297

Page 8: GAME PHYSICS ENGINE DEVELOPMENT - GBV

Physics Engine Design xiii

P A R T V C O N T A C T P H Y S I C S 2 9 9

C H A P T E R

14 C O L L I S I O N R E S O L U T I O N 301

14.1 I M P U L S E S A N D I M P U L S I V E T O R Q U E S 301

14.1.1 Impulsive Torque 302 14.1.2 Rotating Collisions 304 14.1.3 Handling Rotating Collisions 305

14.2 C O L L I S I O N I M P U L S E S 306

14.2.1 Change to Contact Coordinates 306 14.2.2 Velocity Change by Impulse 313 14.2.3 Impulse Change by Velocity 317 14.2.4 Calculating the Desired Velocity Change 318 14.2.5 Calculating the Impulse 319 14.2.6 Applying the Impulse 320

14.3 R E S O L V I N G I N T E R P E N E T R A T I O N 321

14.3.1 Choosing a Resolution Method 321 14.3.2 Implementing Nonlinear Projection 325 14.3.3 Avoiding Excessive Rotation 328

14.4 T H E C O L L I S I O N R E S O L U T I O N P R O C E S S 330

14.4.1 The Collision Resolution Pipeline 331 14.4.2 Preparing Contact Data 333 14.4.3 Resolving Penetration 337 14.4.4 Resolving Velocity 344 14.4.5 Alternative Update Algorithms 346

14.5 S U M M A R Y 349

C H A P T E R

15 R E S T I N G C O N T A C T S A N D F R I C T I O N 351

15.1 R E S T I N G F O R C E S 352

15.1.1 Force Calculations 353

15.2 M I C R O - C O L L I S I O N S 354

15.2.1 Removing Accelerated Velocity 356 15.2.2 Lowering the Restitution 357 15.2.3 The New Velocity Calculation 357

15.3 T Y P E S O F F R I C T I O N 358

15.3.1 Static and Dynamic Friction 359 15.3.2 Isotropic and Anisotropic Friction 361

15.4 I M P L E M E N T I N G F R I C T I O N 362

15.4.1 Friction as Impulses 363

Page 9: GAME PHYSICS ENGINE DEVELOPMENT - GBV

xiv Contents

15.4.2 Modifying the Velocity Resolution Algorithm 365

15.4.3 Putting It All Together 371

15.5 F R I C T I O N A N D S E Q U E N T I A L C O N T A C T R E S O L U T I O N 373

15.6 S U M M A R Y 374

C H A P T E R

16 S T A B I L I T Y A N D O P T I M I Z A T I O N 375

16.1 S T A B I L I T Y 375

16.1.1 Quaternion Drift 376 16.1.2 Interpenetration on Slopes 377 16.1.3 Integration Stability 379 16.1.4 The Benefit of Pessimistic Collision Detection 380 16.1.5 Changing Mathematical Accuracy 381

16.2 O P T I M I Z A T I O N S 383

16.2.1 Sleep 383 16.2.2 Margins of Error for Penetration and Velocity 390 16.2.3 Contact Grouping 393 16.2.4 Code Optimizations 394

16.3 S U M M A R Y 397

C H A P T E R

17 P U T T I N G I T A L L T O G E T H E R 399

17.1 O V E R V I E W O F T H E E N G I N E 399

17.2 U S I N G T H E P H Y S I C S E N G I N E 401

17.2.1 Ragdolls 402 17.2.2 Fracture Physics 405 17.2.3 Explosive Physics 411

17.3 L I M I T A T I O N S O F T H E E N G I N E 418

17.3.1 Stacks 418 17.3.2 Reaction Force Friction 419 17.3.3 Joint Assemblies 419 17.3.4 Stiff Springs 419

17.4 S U M M A R Y 419

P A R T V I W H A T C O M E S N E X T ? 4 2 1

C H A P T E R

18 O T H E R T Y P E S O F P H Y S I C S 423

18.1 S I M U L T A N E O U S C O N T A C T R E S O L U T I O N 423

Page 10: GAME PHYSICS ENGINE DEVELOPMENT - GBV

Physics Engine Design xv

18.1.1 Thejacobian 424 18.1.2 The Linear Complementary Problem 425

18.2 R E D U C E D C O O R D I N A T E A P P R O A C H E S 428

18.3 S U M M A R Y 429

A P P E N D I C E S

В

D

C O M M O N I N E R T I A T E N S O R S 431

A.l D I S C R E T E M A S S E S 431

A.2 C O N T I N U O U S M A S S E S 432

A.3 C O M M O N S H A P E S 432 A.3.1 Cuboid 432 A.3.2 Sphere 432 A.3.3 Cylinder 433 A.3.4 Cone 433

U S E F U L F R I C T I O N C O E F F I C I E N T S F O R G A M E S 434

O T H E R P R O G R A M M I N G L A N G U A G E S 435

C.l С 435

C.2 J A V A 436

C.3 C O M M O N L A N G U A G E R U N T I M E ( . N E T ) 436

C.4 L U A 436

M A T H E M A T I C S S U M M A R Y 438

D.l V E C T O R S 438

D.2 Q U A T E R N I O N S 439

D.3 M A T R I C E S 440

D.4 I N T E G R A T I O N 441

D.5 P H Y S I C S 442

D.6 O T H E R F O R M U L A E 443

B I B L I O G R A P H Y 445

I N D E X 447