You are on page 1of 10

Copyright © 2013. O'Reilly Media. All rights reserved.

May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or
applicable copyright law.

EBSCO Publishing : eBook Collection (EBSCOhost) - printed on 3/11/2021 5:38 AM via INDEPENDENT INSTITUTE OF EDUCATION
AN: 565704 ; Bourg, David M., Bywalec, Bryan.; Physics for Game Developers : Science, Math, and Code for Realistic Effects
Account: s6311349
Copyright © 2013. O'Reilly Media. All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or
applicable copyright law.

EBSCO Publishing : eBook Collection (EBSCOhost) - printed on 3/11/2021 5:38 AM via INDEPENDENT INSTITUTE OF EDUCATION
AN: 565704 ; Bourg, David M., Bywalec, Bryan.; Physics for Game Developers : Science, Math, and Code for Realistic Effects
Account: s6311349
Copyright © 2013. O'Reilly Media. All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or

SECOND EDITION

Physics for Game Developers

David M. Bourg and Bryan Bywalec


applicable copyright law.

EBSCO Publishing : eBook Collection (EBSCOhost) - printed on 3/11/2021 5:38 AM via INDEPENDENT INSTITUTE OF EDUCATION
AN: 565704 ; Bourg, David M., Bywalec, Bryan.; Physics for Game Developers : Science, Math, and Code for Realistic Effects
Account: s6311349
Physics for Game Developers, Second Edition
Copyright © 2013. O'Reilly Media. All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or

by David M. Bourg and Bryan Bywalec


Copyright © 2013 David M. Bourg and Bryan Bywalec. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/
institutional sales department: 800-998-9938 or corporate@oreilly.com.
Editors: Andy Oram and Rachel Roumeliotis Indexer: Lucie Haskins
Production Editor: Christopher Hearse Cover Designer: Randy Comer
Copyeditor: Rachel Monaghan Interior Designer: David Futato
Proofreader: Amanda Kersey Illustrator: Rebecca Demarest

April 2013: Second Edition

Revision History for the Second Edition:


2013-04-09: First release

See http://oreilly.com/catalog/errata.csp?isbn=9781449392512 for release details.

Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly
Media, Inc. Physics for Game Developers, 2nd Edition, the image of a cat and mouse, and related trade dress
are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐
mark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information contained
herein.
applicable copyright law.

ISBN: 978-1-449-39251-2
[LSI]

EBSCO Publishing : eBook Collection (EBSCOhost) - printed on 3/11/2021 5:38 AM via INDEPENDENT INSTITUTE OF EDUCATION
AN: 565704 ; Bourg, David M., Bywalec, Bryan.; Physics for Game Developers : Science, Math, and Code for Realistic Effects
Account: s6311349
Copyright © 2013. O'Reilly Media. All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or

Table of Contents

Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi

Part I. Fundamentals
1. Basic Concepts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Newton’s Laws of Motion 3
Units and Measures 4
Coordinate System 6
Vectors 7
Derivatives and Integrals 8
Mass, Center of Mass, and Moment of Inertia 9
Newton’s Second Law of Motion 20
Inertia Tensor 24
Relativistic Time 29

2. Kinematics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Velocity and Acceleration 36
Constant Acceleration 39
Nonconstant Acceleration 41
2D Particle Kinematics 42
3D Particle Kinematics 45
X Components 46
Y Components 47
Z Components 48
The Vectors 48
applicable copyright law.

Hitting the Target 49


Kinematic Particle Explosion 54
Rigid-Body Kinematics 61
Local Coordinate Axes 62

iii

EBSCO Publishing : eBook Collection (EBSCOhost) - printed on 3/11/2021 5:38 AM via INDEPENDENT INSTITUTE OF EDUCATION
AN: 565704 ; Bourg, David M., Bywalec, Bryan.; Physics for Game Developers : Science, Math, and Code for Realistic Effects
Account: s6311349
Angular Velocity and Acceleration 62
Copyright © 2013. O'Reilly Media. All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or

3. Force. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Forces 71
Force Fields 72
Friction 73
Fluid Dynamic Drag 75
Pressure 76
Buoyancy 77
Springs and Dampers 79
Force and Torque 80
Summary 83

4. Kinetics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Particle Kinetics in 2D 87
Particle Kinetics in 3D 91
X Components 94
Y Components 95
Z Components 95
Cannon Revised 95
Rigid-Body Kinetics 99

5. Collisions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Impulse-Momentum Principle 104
Impact 105
Linear and Angular Impulse 112
Friction 115

6. Projectiles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Simple Trajectories 120
Drag 124
Magnus Effect 132
Variable Mass 138

Part II. Rigid-Body Dynamics


7. Real-Time Simulations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Integrating the Equations of Motion 144
applicable copyright law.

Euler’s Method 146


Better Methods 153

iv | Table of Contents

EBSCO Publishing : eBook Collection (EBSCOhost) - printed on 3/11/2021 5:38 AM via INDEPENDENT INSTITUTE OF EDUCATION
AN: 565704 ; Bourg, David M., Bywalec, Bryan.; Physics for Game Developers : Science, Math, and Code for Realistic Effects
Account: s6311349
Summary 159
Copyright © 2013. O'Reilly Media. All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or

8. Particles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
Simple Particle Model 166
Integrator 169
Rendering 170
The Basic Simulator 170
Implementing External Forces 172
Implementing Collisions 175
Particle-to-Ground Collisions 175
Particle-to-Obstacle Collisions 181
Tuning 186

9. 2D Rigid-Body Simulator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189


Model 190
Transforming Coordinates 197
Integrator 198
Rendering 200
The Basic Simulator 201
Tuning 204

10. Implementing Collision Response. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205


Linear Collision Response 206
Angular Effects 213

11. Rotation in 3D Rigid-Body Simulators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227


Rotation Matrices 228
Quaternions 232
Quaternion Operations 234
Quaternions in 3D Simulators 239

12. 3D Rigid-Body Simulator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243


Model 243
Integration 247
Flight Controls 250

13. Connecting Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255


Springs and Dampers 257
applicable copyright law.

Connecting Particles 258


Rope 258
Connecting Rigid Bodies 265
Links 265

Table of Contents | v

EBSCO Publishing : eBook Collection (EBSCOhost) - printed on 3/11/2021 5:38 AM via INDEPENDENT INSTITUTE OF EDUCATION
AN: 565704 ; Bourg, David M., Bywalec, Bryan.; Physics for Game Developers : Science, Math, and Code for Realistic Effects
Account: s6311349
Rotational Restraint 275
Copyright © 2013. O'Reilly Media. All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or

14. Physics Engines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281


Building Your Own Physics Engine 281
Physics Models 283
Simulated Objects Manager 284
Collision Detection 285
Collision Response 286
Force Effectors 287
Numerical Integrator 288

Part III. Physical Modeling


15. Aircraft. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Geometry 294
Lift and Drag 297
Other Forces 302
Control 303
Modeling 305

16. Ships and Boats. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321


Stability and Sinking 323
Stability 323
Sinking 325
Ship Motions 326
Heave 327
Roll 327
Pitch 328
Coupled Motions 328
Resistance and Propulsion 328
General Resistance 328
Propulsion 334
Maneuverability 335
Rudders and Thrust Vectoring 336

17. Cars and Hovercraft. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339


Cars 339
Resistance 339
applicable copyright law.

Power 340
Stopping Distance 341
Steering 342

vi | Table of Contents

EBSCO Publishing : eBook Collection (EBSCOhost) - printed on 3/11/2021 5:38 AM via INDEPENDENT INSTITUTE OF EDUCATION
AN: 565704 ; Bourg, David M., Bywalec, Bryan.; Physics for Game Developers : Science, Math, and Code for Realistic Effects
Account: s6311349
Hovercraft 345
Copyright © 2013. O'Reilly Media. All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or

How Hovercraft Work 345


Resistance 347
Steering 350

18. Guns and Explosions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353


Projectile Motion 353
Taking Aim 355
Zeroing the Sights 357
Breathing and Body Position 360
Recoil and Impact 361
Explosions 362
Particle Explosions 363
Polygon Explosions 366

19. Sports. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369


Modeling a Golf Swing 370
Solving the Golf Swing Equations 373
Billiards 378
Implementation 380
Initialization 383
Stepping the Simulation 386
Calculating Forces 388
Handling Collisions 393

Part IV. Digital Physics


20. Touch Screens. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Types of Touch Screens 403
Resistive 403
Capacitive 404
Infrared and Optical Imaging 404
Exotic: Dispersive Signal and Surface Acoustic Wave 404
Step-by-Step Physics 404
Resistive Touch Screens 404
Capacitive Touch Screens 408
Example Program 410
Multitouch 410
applicable copyright law.

Other Considerations 411


Haptic Feedback 411
Modeling Touch Screens in Games 411

Table of Contents | vii

EBSCO Publishing : eBook Collection (EBSCOhost) - printed on 3/11/2021 5:38 AM via INDEPENDENT INSTITUTE OF EDUCATION
AN: 565704 ; Bourg, David M., Bywalec, Bryan.; Physics for Game Developers : Science, Math, and Code for Realistic Effects
Account: s6311349
Difference from Mouse-Based Input 412
Copyright © 2013. O'Reilly Media. All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or

Custom Gestures 412

21. Accelerometers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413


Accelerometer Theory 414
MEMS Accelerometers 416
Common Accelerometer Specifications 417
Data Clipping 417
Sensing Orientation 418
Sensing Tilt 420
Using Tilt to Control a Sprite 420
Two Degrees of Freedom 421

22. Gaming from One Place to Another. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427


Location-Based Gaming 427
Geocaching and Reverse Geocaching 428
Mixed Reality 428
Street Games 428
What Time Is It? 429
Two-Dimensional Mathematical Treatment 429
Location, Location, Location 433
Distance 433
Great-Circle Heading 435
Rhumb Line 436

23. Pressure Sensors and Load Cells. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439


Under Pressure 440
Example Effects of High Pressure 440
Button Mashing 442
Load Cells 444
Barometers 448

24. 3D Display. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451


Binocular Vision 451
Stereoscopic Basics 454
The Left and Right Frustums 454
Types of Display 458
Complementary-Color Anaglyphs 458
applicable copyright law.

Linear and Circular Polarization 459


Liquid-Crystal Plasma 462
Autostereoscopy 463
Advanced Technologies 465

viii | Table of Contents

EBSCO Publishing : eBook Collection (EBSCOhost) - printed on 3/11/2021 5:38 AM via INDEPENDENT INSTITUTE OF EDUCATION
AN: 565704 ; Bourg, David M., Bywalec, Bryan.; Physics for Game Developers : Science, Math, and Code for Realistic Effects
Account: s6311349

You might also like