Praise for Game Physics Engine Development “Game Physics Engine Development is the first game physics book to emphasize building an actual engine It focuses on the practical implementation details and addresses trade-offs that must be made Ian’s experience with commercial physics engines definitely shows His book fills a gap by demonstrating how you actually build a physics engine.” — Dave Eberly, President, Geometric Tools “Ian Millington has achieved the remarkable task of creating a self-contained text on game physics programming If you are charged with putting together a game physics engine, this book will carry you through from beginning (with a math and physics primer) through the process (with detailed attention to extendable software design) to the end (collision handling and constrained dynamics) If you are asked to use a game physics engine, this text will help you understand what is going on under the hood, and therefore make you a better user The text is practical enough to serve the industry practitioner (read this before starting your project!), and the writing is solid enough to be used in an undergraduate course on video game physics Game Physics Engine Development comes bundled with working source code closely matched to the text—a valuable service to the reader: the bundled implementation serves as a foundation for experimentation, a reference for comparison, and a guide to modular software design Millington’s writing maintains a steady pace and a friendly tone; the narrative level will appeal to a broad audience of students and practitioners.” — Professor Eitan Grinspun, Department of Computer Science, Columbia University, New York “A competent programmer with sufficient mathematical sophistication could build a physics engine just from the text and equations—even without the accompanying source code You can’t say this about a lot of books!” — Philip J Schneider, Industrial Light + Magic “Thorough content with an engaging style; the essential mathematics is presented clearly with genuine and effective explanation and appropriate scope The C++ code samples are a major strength.” — Dr John Purdy, Department of Computer Science, University of Hull, UK The Morgan Kaufmann Series in Interactive 3D Technology The game industry is a powerful and driving force in the evolution of computer technology As the capabilities of personal computers, peripheral hardware, and game consoles have grown, so has the demand for quality information about the algorithms, tools, and descriptions needed to take advantage of this new technology To satisfy this demand and establish a new level of professional reference for the game developer, we created the Morgan Kaufmann Series in Interactive 3D Technology Books in the series are written for developers by leading industry professionals and academic researchers, and cover the state of the art in real-time 3D The series emphasizes practical, working solutions, and solid software-engineering principles The goal is for the developer to be able to implement real systems from the fundamental ideas, whether it be for games or for other applications Game Physics Engine Development Ian Millington Artificial Intelligence for Games Ian Millington X3D: Extensible 3D Graphics for Web Authors Don Brutzman and Leonard Daly 3D Game Engine Design: A Practical Approach to Real-Time Computer Graphics, Second Edition David H Eberly 3D Game Engine Architecture: Engineering Real-Time Applications with Wild Magic David H Eberly Game Physics David H Eberly Forthcoming Real-Time Cameras Mark Haigh-Hutchinson Better Game Characters by Design: A Psychological Approach Katherine Isbister Real-Time Collision Detection Christer Ericson Visualizing Quaternions Andrew J Hanson Physically Based Rendering: From Theory to Implementation Matt Pharr and Greg Humphreys Collision Detection in Interactive 3D Environments Gino van den Bergen Essential Mathematics for Games and Interactive Applications: A Programmer’s Guide James M Van Verth and Lars M Bishop GAME PHYSICS ENGINE DEVELOPMENT IAN MILLINGTON AMSTERDAM • BOSTON • HEIDELBERG LONDON • NEW YORK • OXFORD PARIS • SAN DIEGO • SAN FRANCISCO SINGAPORE • SYDNEY • TOKYO Morgan Kaufmann is an imprint of Elsevier Publisher Publishing Services Manager Assistant Editor Project Manager Cover Design Composition Illustrations Copyeditor Proofreader Indexer Interior printer Cover printer Denise E M Penrose George Morrison Michelle Ward Marilyn E Rash Chen Design Associates VTeX Integra Carol Leyba Dianne Wood Keith Shostak The Maple Press Company Phoenix Color Corp Morgan Kaufmann Publishers is an imprint of Elsevier 500 Sansome Street, Suite 400, San Francisco, CA 94111 This book is printed on acid-free paper © 2007 by Elsevier Inc All rights reserved Designations used by companies to distinguish their products are often claimed as trademarks or registered trademarks In all instances in which Morgan Kaufmann Publishers is aware of a claim, the product names appear in initial capital or all capital letters Readers, however, should contact the appropriate companies for more complete information regarding trademarks and registration No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means—electronic, mechanical, photocopying, scanning, or otherwise—without prior written permission of the publisher Permissions may be sought directly from Elsevier’s Science & Technology Rights Department in Oxford, UK: phone: (+44) 1865 843830, fax: (+44) 1865 853333, E-mail: permissions@elsevier.com You may also complete your request on-line via the Elsevier homepage (http://elsevier.com), by selecting “Support & Contact” then “Copyright and Permission” and then “Obtaining Permissions.” Library of Congress Cataloging-in-Publication Data Millington, Ian Game physics engine development / Ian Millington p cm Includes bibliographical references and index ISBN-13: 978-0-12-369471-3 (alk paper) ISBN-10: 0-12-369471-X (alk paper) Computer games—Programming Physics—Data processing I Title QA76.76.C672M55 2006 794.8 1526–dc22 2006023852 For information on all Morgan Kaufmann publications, visit our Web site at www.mkp.com or www.books.elsevier.com Printed in the United States of America 07 08 09 10 11 To Melanie A BOUT THE A UTHOR Ian Millington is a partner at IPR Ventures, a consulting company involved in the development of next-generation technologies for entertainment, modeling, and simulation Previously, he founded Mindlathe Ltd, the largest specialist AI middleware company in computer games, which worked on a huge range of game genres and technologies He has an extensive background in artificial intelligence (AI), including PhD research in complexity theory and natural computing Ian has published academic and professional papers and articles on topics ranging from paleontology to hypertext and is the author of Artificial Intelligence for Games (Morgan Kaufmann, 2006) C ONTENTS xvi L IST OF F IGURES P REFACE xix C HAPTER I NTRODUCTION 1.1 W HAT I S G AME P HYSICS ? 1.2 W HAT I S A P HYSICS E NGINE ? 1.2.1 1.2.2 Advantages of a Physics Engine Weaknesses of a Physics Engine 1.3 A PPROACHES TO P HYSICS 1.3.1 Types of Object 1.3.2 Contact Resolution 1.3.3 Impulses and Forces 1.3.4 What We’re Building 1.4 T HE M ATHEMATICS OF P HYSICS 1.4.1 The Math You Need to Know 1.4.2 The Math We’ll Review 1.4.3 The Math We’ll Introduce E NGINES E NGINES 1.5 T HE S OURCE C ODE IN T HIS B OOK 1.6 H OW THIS B OOK I S S TRUCTURED PART I PARTICLE P HYSICS 5 7 10 10 11 13 C HAPTER T HE M ATHEMATICS 2.1 OF PARTICLES V ECTORS 2.1.1 The Handedness of Space 15 15 19 vii viii Contents 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 Vectors and Directions Scalar and Vector Multiplication Vector Addition and Subtraction Multiplying Vectors The Component Product The Scalar Product The Vector Product The Orthonormal Basis 20 23 24 27 28 29 31 35 2.2 C ALCULUS 2.2.1 Differential Calculus 2.2.2 Integral Calculus 35 36 40 2.3 S UMMARY 42 C HAPTER M OTION 43 A PARTICLE 3.2 T HE F IRST T WO L AWS 43 T HE L AWS OF 3.1 The First Law The Second Law The Force Equations Adding Mass to Particles Momentum and Velocity The Force of Gravity 44 45 46 46 47 48 48 3.3 T HE I NTEGRATOR 3.3.1 The Update Equations 3.3.2 The Complete Integrator 50 51 52 3.4 S UMMARY 54 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 C HAPTER T HE PARTICLE P HYSICS E NGINE 55 4.1 B ALLISTICS 4.1.1 Setting Projectile Properties 4.1.2 Implementation 55 56 57 4.2 F IREWORKS 4.2.1 The Fireworks Data 4.2.2 The Fireworks Rules 4.2.3 The Implementation 60 60 61 63 4.3 S UMMARY 66 Physics Engine Design PART II M ASS -A GGREGATE P HYSICS 67 C HAPTER A DDING G ENERAL F ORCES 69 5.1 D’A LEMBERT ’ S P RINCIPLE 69 5.2 F ORCE G ENERATORS 5.2.1 Interfaces and Polymorphism 5.2.2 Implementation 5.2.3 A Gravity Force Generator 5.2.4 A Drag Force Generator 72 73 73 76 77 5.3 B UILT- IN G RAVITY 79 5.4 S UMMARY AND D AMPING 79 C HAPTER S PRINGS 6.1 S PRINGLIKE T HINGS H OOK ’ S L AW 6.1.1 6.1.2 6.2 AND The Limit of Elasticity Springlike Things S PRINGLIKE F ORCE G ENERATORS 6.2.1 6.2.2 6.2.3 6.2.4 A Basic Spring Generator An Anchored Spring Generator An Elastic Bungee Generator A Buoyancy Force Generator 6.3 S TIFF S PRINGS 6.3.1 The Problem of Stiff Springs 6.3.2 Faking Stiff Springs 6.4 S UMMARY 81 81 83 83 83 84 86 87 89 93 93 95 101 C HAPTER H ARD C ONSTRAINTS 103 7.1 103 104 105 105 107 S IMPLE C OLLISION R ESOLUTION 7.1.1 The Closing Velocity 7.1.2 The Coefficient of Restitution 7.1.3 The Collision Direction and the Contact Normal 7.1.4 Impulses ix ...Praise for Game Physics Engine Development Game Physics Engine Development is the first game physics book to emphasize building an actual engine It focuses on the practical... complete physics engine in a game Although Exile was released in 1988, I feel like a relative newcomer to the physics coding party I started writing game physics in 1999 by creating an engine for... that a physics engine is a panacea There are reasons that you might not want to use a full physics engine in your game The most common reason is one of speed A general-purpose physics engine