Christer Ericson — «Real Time Collision Detection
Total Page:16
File Type:pdf, Size:1020Kb
Accurate and efficient collision detection in complex environments is one of the foundations of today’s cutting-edge computer games. Yet collision detection is notoriously difficult to implement robustly and takes up an increasingly large fraction of compute cycles in current game engines as increasingly detailed environments are becoming the norm. Real-time Collision Detection is a comprehensive reference on this topic,covering it with both breadth and depth. Not only are the fundamental algorithms explained clearly and in detail, but Ericson’s book covers crucial implementation issues, including geometric and numeric robustness and cache-efficient implementations of the algorithms. Together, these make this book a“must have”practical reference for anyone interested in developing interactive applications with complex environments. –Matt Pharr, Senior Software Developer, NVIDIA Christer Ericson’s Real-time Collision Detection is an excellent resource that covers the fundamentals as well as a broad array of techniques applicable to game development. –Jay Stelly, Senior Engineer,Valve Christer Ericson provides a practical and very accessible treatment of real-time collision detection.This includes a comprehensive set of C++ implementations of a very large number of routines necessary to build such applications in a context which is much broader than just game programming. The programs are well-thought out and the accompanying discussion reveals a deep understanding of the graphics, algorithms, and ease of implementation issues. It will find a welcome home on any graphics programmer’s bookshelf although it will most likely not stay there long as others will be constantly borrowing it. –Hanan Samet, Professor of Computer Science, University of Maryland Real-Time Collision Detection is an excellent resource that every serious engine programmer should have on his bookshelf. Christer Ericson covers an impressive range of techniques and presents them using concise mathematics, insightful figures, and practical code. –Eric Lengyel, Senior Programmer, Naughty Dog If you think you already know everything about collision detection, you’re in for a surprise! This book not only does an excellent job at presenting all the collision detection methods known to date, it also goes way beyond the standard material thanks to a plethora of juicy, down-to-earth, hard-learned implementation tips and tricks. This produces a perfect blend between theory and practice, illustrated by the right amount of source code in appropriate places. Basically the book just oozes with experience. Christer doesn’t forget all the alternative topics that, despite not directly related to collision detection, can ruin your implementation if you don’t include them in your design. The chapters on robustness and optimization are priceless in this respect. Its carefully crafted compact kd-tree implementation beautifully concludes a unique book full of luminous gems. –Pierre Terdiman, Principal Software Engineer, NovodeX AG (author of the OPCODE collision detection library) ThisPageIntentionallyLeftBlank Real-Time Collision Detection The Morgan Kaufmann Series in Interactive 3D Technology Series Editor: David H. Eberly, Magic Software, Inc. 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 other applications. Real-Time Collision Detection Christer Ericson 3D Game Engine Architecture: Engineering Real-Time Applications with Wild Magic David H. Eberly Physically Based Rendering: From Theory to Implementation Matt Pharr and Greg Humphreys Essential Mathematics for Game and Interactive Applications: A Programmer’s Guide James M.VanVerth and Lars M. Bishop Game Physics David H. Eberly Collision Detection in Interactive 3D Environments Gino van den Bergen 3D Game Engine Design: A Practical Approach to Real-Time Computer Graphics David H. Eberly Forthcoming Artificial Intelligence for Computer Games Ian Millington Visualizing Quaternions Andrew J. Hanson Better Video Game Characters by Design: Using Psychology to Make Better Games Katherine Isbister Real-Time Collision Detection Christer Ericson Sony Computer Entertainment America AMSTERDAM • BOSTON • HEIDELBERG • LONDON NEW YORK • OXFORD • PARIS • SAN DIEGO SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO Morgan Kaufmann Publishers is an imprint of Elsevier Senior Editor Tim Cox Publishing Services Manager Simon Crump Senior Project Manager Angela Dooley Assistant Editor Richard Camp Cover Design Chen Design Associates Cover Image Text Design Composition CEPHA Technical Illustration Dartmouth Publishing, Inc. Copyeditor Betty Pessagno Proofreader Phyllis Coyne et al. Indexer Northwind Editorial Interior Printer The Maple-Vail Book Manufacturing Group Cover Printer Phoenix Color, Inc. 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. © 2005 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.uk.You may also complete your request on-line via the Elsevier homepage (http://elsevier.com) by selecting “Customer Support”and then “Obtaining Permissions.” Library of Congress Cataloging-in-Publication Data Application submitted ISBN: 1-55860-732-3 For information on all Morgan Kaufmann publications, visit our Web site at www.mkp.com Printed in the United States of America 0807060504 54321 To mom About the Author Christer Ericson is a senior principal programmer and the tools and technology lead at Sony Computer Entertainment America in Santa Monica. Before joining Sony in 1999, he was a senior programmer at Neversoft Entertainment. Christer received his Masters degree in computer science from Umeå University, Sweden, where he also lectured for several years before moving to the US in 1996. Christer has served on the advisory board for Full Sail’s Game Design and Development degree program since 2002. His interests are varied, but he takes a particular interest in program optimization, a topic he has spoken on at the Game Developers Conference. Contents List of Figures xxi Preface xxxvii Chapter 1 Introduction 1 1.1 Content Overview 2 1.1.1 Chapter 2: Collision Detection Design Issues 2 1.1.2 Chapter 3: A Math and Geometry Primer 2 1.1.3 Chapter 4: BoundingVolumes 3 1.1.4 Chapter 5: Basic Primitive Tests 3 1.1.5 Chapter 6: BoundingVolume Hierarchies 3 1.1.6 Chapter 7: Spatial Partitioning 3 1.1.7 Chapter 8: BSP Tree Hierarchies 4 1.1.8 Chapter 9: Convexity-based Methods 4 1.1.9 Chapter 10: GPU-assisted Collision Detection 4 1.1.10 Chapter 11: Numerical Robustness 4 1.1.11 Chapter 12: Geometrical Robustness 4 1.1.12 Chapter 13: Optimization 5 1.2 About the Code 5 Chapter 2 Collision Detection Design Issues 7 2.1 Collision Algorithm Design Factors 7 2.2 Application Domain Representation 8 2.2.1 Object Representations 8 2.2.2 CollisionVersus Rendering Geometry 11 2.2.3 Collision Algorithm Specialization 12 2.3 Types of Queries 13 2.4 Environment Simulation Parameters 14 ix x Contents 2.4.1 Number of Objects 14 2.4.2 SequentialVersus Simultaneous Motion 15 2.4.3 DiscreteVersus Continuous Motion 16 2.5 Performance 17 2.5.1 Optimization Overview 18 2.6 Robustness 19 2.7 Ease of Implementation and Use 19 2.7.1 Debugging a Collision Detection System 20 2.8 Summary 21 Chapter 3 A Math and Geometry Primer 23 3.1 Matrices 23 3.1.1 Matrix Arithmetic 25 3.1.2 Algebraic Identities Involving Matrices 26 3.1.3 Determinants 27 3.1.4 Solving Small Systems of Linear Equation Using Cramer’s Rule 29 3.1.5 Matrix Inverses for 2 × 2 and 3 × 3 Matrices 31 3.1.6 Determinant Predicates 32 3.1.6.1 ORIENT2D(A, B, C )32 3.1.6.2 ORIENT3D(A, B, C, D)33 3.1.6.3 INCIRCLE2D(A, B, C, D)34 3.1.6.4 INSPHERE(A, B, C, D, E )34 3.2 Coordinate Systems and Points 35 3.3 Vectors 35 3.3.1 Vector Arithmetic 37 3.3.2 Algebraic Identities InvolvingVectors 38 3.3.3 The Dot Product 39 3.3.4 Algebraic Identities Involving Dot Products 40 3.3.5 The Cross Product 41 3.3.6 Algebraic Identities Involving Cross Products 44 3.3.7