O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Game Physics Cookbook

Book Description

Discover over 100 easy-to-follow recipes to help you implement efficient game physics and collision detection in your games

About This Book

  • Get a comprehensive coverage of techniques to create high performance collision detection in games
  • Learn the core mathematics concepts and physics involved in depicting collision detection for your games
  • Get a hands-on experience of building a rigid body physics engine

Who This Book Is For

This book is for beginner to intermediate game developers. You don’t need to have a formal education in games—you can be a hobbyist or indie developer who started making games with Unity 3D.

What You Will Learn

  • Implement fundamental maths so you can develop solid game physics
  • Use matrices to encode linear transformations
  • Know how to check geometric primitives for collisions
  • Build a Physics engine that can create realistic rigid body behavior
  • Understand advanced techniques, including the Separating Axis Theorem
  • Create physically accurate collision reactions
  • Explore spatial partitioning as an acceleration structure for collisions
  • Resolve rigid body collisions between primitive shapes

In Detail

Physics is really important for game programmers who want to add realism and functionality to their games. Collision detection in particular is a problem that affects all game developers, regardless of the platform, engine, or toolkit they use.

This book will teach you the concepts and formulas behind collision detection. You will also be taught how to build a simple physics engine, where Rigid Body physics is the main focus, and learn about intersection algorithms for primitive shapes.

You’ll begin by building a strong foundation in mathematics that will be used throughout the book. We’ll guide you through implementing 2D and 3D primitives and show you how to perform effective collision tests for them. We then pivot to one of the harder areas of game development—collision detection and resolution.

Further on, you will learn what a Physics engine is, how to set up a game window, and how to implement rendering. We’ll explore advanced physics topics such as constraint solving. You’ll also find out how to implement a rudimentary physics engine, which you can use to build an Angry Birds type of game or a more advanced game.

By the end of the book, you will have implemented all primitive and some advanced collision tests, and you will be able to read on geometry and linear Algebra formulas to take forward to your own games!

Style and approach

Gain the necessary skills needed to build a Physics engine for your games through practical recipes, in an easy-to-read manner. Every topic explained in the book has clear, easy to understand code accompanying it.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Game Physics Cookbook
    1. Table of Contents
    2. Game Physics Cookbook
    3. Credits
    4. About the Author
    5. Acknowledgements
    6. About the Reviewer
    7. Acknowledgements
    8. www.PacktPub.com
      1. eBooks, discount offers, and more
        1. Why Subscribe?
    9. Customer Feedback
    10. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Sections
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
        5. See also
      5. Conventions
      6. Reader feedback
      7. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    11. 1. Vectors
      1. Introduction
      2. Vector definition
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. The W component
      3. Component-wise operations
        1. Getting ready
        2. How to do it…
        3. How it works…
          1. Addition
          2. Subtraction
          3. Multiplication (Vector and Scalar)
          4. Comparison
        4. There's more…
      4. Dot product
        1. How to do it…
        2. How it works…
        3. There's more…
          1. Geometric definition
      5. Magnitude
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      6. Normalizing
        1. Getting ready
        2. How to do it…
        3. How it works…
      7. Cross product
        1. Getting ready
        2. How to do it…
        3. How it works…
      8. Angles
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Radians and degrees
      9. Projection
        1. Getting ready
        2. How to do it…
        3. How it works…
      10. Reflection
        1. Getting ready
        2. How to do it…
        3. How it works…
    12. 2. Matrices
      1. Introduction
      2. Matrix definition
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. Transpose
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Multiplication
        1. Getting ready
        2. How to do it…
        3. How it works…
      5. Identity matrix
        1. Getting ready
        2. How to do it…
        3. How it works…
      6. Determinant of a 2x2 matrix
        1. How to do it…
        2. How it works…
      7. Matrix of minors
        1. Getting ready
        2. How to do it…
        3. How it works…
          1. Minor of a 2x2 matrix
          2. Minor of a 3x3 matrix
      8. Cofactor
        1. Getting ready
        2. How to do it…
        3. How it works…
      9. Determinant of a 3x3 matrix
        1. Getting ready
        2. How to do it…
        3. How it works…
      10. Operations on a 4x4 matrix
        1. Getting ready
        2. How to do it…
        3. How it works…
      11. Adjugate matrix
        1. Getting ready
        2. How to do it…
        3. How it works…
      12. Matrix inverse
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Expanding the inverse
    13. 3. Matrix Transformations
      1. Introduction
      2. Matrix majors
      3. Translation
        1. Getting Ready
        2. How to do it…
        3. How it works…
      4. Scaling
        1. Getting ready
        2. How to do it…
        3. How it works…
      5. How rotations work
        1. Getting ready
        2. How to do it…
        3. How it works…
      6. Rotation matrices
        1. Getting ready
        2. How to do it…
        3. How it works…
          1. X-Basis vector
          2. Y-Basis vector
          3. Z-Basis vector
        4. There's more…
          1. X and Y rotation
      7. Axis angle rotation
        1. Getting ready
        2. How to do it…
        3. How it works…
      8. Vector matrix multiplication
        1. Getting ready
        2. How to do it…
        3. How it works…
      9. Transform matrix
        1. Getting ready
        2. How to do it…
        3. How it works…
      10. View matrix
        1. Getting ready
        2. How to do it…
        3. How it works…
      11. Projection matrix
        1. Getting ready
        2. How to do it…
        3. How it works…
    14. 4. 2D Primitive Shapes
      1. Introduction
      2. 2D points
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. 2D lines
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Circle
        1. Getting ready
        2. How to do it…
        3. How it works…
      5. Rectangle
        1. Getting ready
        2. How to do it…
        3. How it works…
      6. Oriented rectangle
        1. Getting ready
        2. How to do it…
        3. How it works…
      7. Point containment
        1. Getting ready
        2. How to do it…
        3. How it works…
          1. Point on a line
          2. Point in a circle
          3. Point in a rectangle
          4. Point in an oriented rectangle
      8. Line intersection
        1. Getting ready
        2. How to do it…
        3. How it works…
          1. Line circle
          2. Line rectangle
          3. Line oriented rectangle
    15. 5. 2D Collisions
      1. Introduction
      2. Circle to circle
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. Circle to rectangle
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      4. Circle to oriented rectangle
        1. Getting ready
        2. How to do it…
        3. How it works…
      5. Rectangle to rectangle
        1. Getting ready
        2. How to do it…
        3. How it works…
      6. Separating Axis Theorem
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Determining which axis to test
      7. Rectangle to oriented rectangle
        1. Getting ready
        2. How to do it…
        3. How it works…
      8. Oriented rectangle to oriented rectangle
        1. Getting ready
        2. How to do it…
        3. How it works…
    16. 6. 2D Optimizations
      1. Introduction
      2. Containing circle
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. Containing rectangle
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Simple and complex shapes
        1. Getting ready
        2. How to do it…
        3. How it works…
      5. Quad tree
        1. Getting ready
        2. How to do it…
        3. How it works…
      6. Broad phase collisions
        1. Getting ready
        2. How to do it…
        3. How it works…
    17. 7. 3D Primitive Shapes
      1. Introduction
      2. Point
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. Line segment
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Ray
        1. Getting ready
        2. How to do it…
        3. How it works…
      5. Sphere
        1. Getting ready
        2. How to do it…
        3. How it works…
      6. Axis Aligned Bounding Box
        1. Getting ready
        2. How to do it
        3. How it works
      7. Oriented Bounding Box
        1. Getting ready
        2. How to do it
        3. How it works
      8. Plane
        1. Getting ready
        2. How to do it
        3. How it works
      9. Triangle
        1. Getting ready
        2. How to do it
        3. How it works
    18. 8. 3D Point Tests
      1. Introduction
      2. Point and sphere
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. Point and AABB
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Point and Oriented Bounding Box
        1. Getting ready
        2. How to do it…
        3. How it works…
      5. Point and plane
        1. Getting ready
        2. How to do it…
        3. How it works…
      6. Point and line
        1. Getting ready
        2. How to do it…
        3. How it works…
      7. Point and ray
        1. Getting ready
        2. How to do it…
        3. How it works…
    19. 9. 3D Shape Intersections
      1. Introduction
      2. Sphere-to-sphere
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. Sphere-to-AABB
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Sphere-to-OBB
        1. Getting ready
        2. How to do it…
        3. How it works…
      5. Sphere-to-plane
        1. Getting ready
        2. How to do it…
        3. How it works…
      6. AABB-to-AABB
        1. Getting ready
        2. How to do it…
        3. How it works…
      7. AABB-to-OBB
        1. Getting ready
        2. How to do it…
        3. How it works…
      8. AABB-to-plane
        1. Getting ready
        2. How to do it…
        3. How it works…
      9. OBB-to-OBB
        1. Getting ready
        2. How to do it…
        3. How it works…
      10. OBB-to-plane
        1. Getting ready
        2. How to do it…
        3. How it works…
      11. Plane-to-plane
        1. Getting ready
        2. How to do it…
        3. How it works…
    20. 10. 3D Line Intersections
      1. Introduction
      2. Raycast Sphere
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. Raycast Axis Aligned Bounding Box
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Raycast Oriented Bounding Box
        1. Getting ready
        2. How to do it…
        3. How it works…
      5. Raycast plane
        1. Getting ready
        2. How to do it…
        3. How it works…
      6. Linetest Sphere
        1. Getting ready
        2. How to do it…
        3. How it works…
      7. Linetest Axis Aligned Bounding Box
        1. Getting ready
        2. How to do it…
        3. How it works…
      8. Linetest Oriented Bounding Box
        1. Getting ready
        2. How to do it…
        3. How it works…
      9. Linetest Plane
        1. Getting ready
        2. How to do it…
        3. How it works…
    21. 11. Triangles and Meshes
      1. Introduction
      2. Point in triangle
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. Closest point triangle
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Triangle to sphere
        1. Getting ready
        2. How to do it…
        3. How it works…
      5. Triangle to Axis Aligned Bounding Box
        1. Getting ready
        2. How to do it…
        3. How it works…
      6. Triangle to Oriented Bounding Box
        1. Getting ready
        2. How to do it…
        3. How it works…
      7. Triangle to plane
        1. Getting ready
        2. How to do it…
        3. How it works…
      8. Triangle to triangle
        1. Getting ready
        2. How to do it…
        3. How it works…
      9. Robustness of the Separating Axis Theorem
        1. Getting ready
        2. How to do it…
        3. How it works…
      10. Raycast Triangle
        1. Getting ready
        2. How to do it…
        3. How it works…
      11. Linetest Triangle
        1. Getting ready
        2. How to do it…
        3. How it works…
      12. Mesh object
        1. Getting ready
        2. How to do it…
        3. How it works…
      13. Mesh optimization
        1. Getting ready
        2. How to do it…
        3. How it works…
      14. Mesh operations
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
    22. 12. Models and Scenes
      1. Introduction
      2. The Model object
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. Operations on models
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. The Scene object
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more
      5. Operations on the scene
        1. Getting ready
        2. How to do it…
        3. How it works…
      6. The Octree object
        1. Getting ready
        2. How to do it…
        3. How it works…
      7. Octree contents
        1. Getting ready
        2. How to do it…
        3. How it works…
      8. Operations on the Octree
        1. Getting ready
        2. How to do it…
        3. How it works…
      9. Octree scene integration
        1. Getting ready
        2. How to do it…
        3. How it works…
    23. 13. Camera and Frustum
      1. Introduction
      2. Camera object
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. Camera controls
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Frustum object
        1. Getting ready
        2. How to do it…
        3. How it works…
      5. Frustum from matrix
        1. Getting ready
        2. How to do it…
        3. How it works…
      6. Sphere in frustum
        1. Getting ready
        2. How to do it…
        3. How it works…
      7. Bounding Box in frustum
        1. Getting ready
        2. How to do it…
        3. How it works…
      8. Octree culling
        1. Getting Ready
        2. How to do it…
        3. How it works…
      9. Picking
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
    24. 14. Constraint Solving
      1. Introduction
      2. Framework introduction
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      3. Raycast sphere
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Raycast Bounding Box
        1. Getting ready
        2. How to do it…
        3. How it works…
      5. Raycast plane and triangle
        1. Getting ready
        2. How to do it…
        3. How it works…
      6. Physics system
        1. Getting ready
        2. How to do it…
        3. How it works…
      7. Integrating particles
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
      8. Solving constraints
        1. Getting ready
        2. How to do it…
        3. How it works…
      9. Verlet Integration
        1. Getting ready
        2. How to do it…
        3. How it works…
    25. 15. Manifolds and Impulses
      1. Introduction
      2. Manifold for spheres
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. Manifold for boxes
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
          1. Duplicate points
      4. Rigidbody Modifications
        1. Getting ready
        2. How to do it…
        3. How it works…
      5. Linear Velocity
        1. Getting ready
        2. How to do it...
        3. How it works...
      6. Linear Impulse
        1. Getting ready
        2. How to do it...
        3. How it works...
          1. Linear Impulse
          2. Friction
        4. There's more...
      7. Physics System Update
        1. Getting ready
        2. How to do it...
        3. How it works...
      8. Angular Velocity
        1. Angular Velocity and Acceleration
          1. Tangential Acceleration
        2. Centripetal Acceleration
        3. Torque
        4. Inertia Tensor
        5. Getting ready
        6. How to do it...
        7. How it works...
        8. There's more...
          1. Tensors
      9. Angular Impulse
        1. Getting ready
        2. How to do it...
        3. How it works...
        4. There's more...
          1. Non-linear projection
    26. 16. Springs and Joints
      1. Introduction
      2. Particle Modifications
        1. Getting ready
        2. How to do it…
        3. How it works…
      3. Springs
        1. Getting ready
        2. How to do it…
        3. How it works…
      4. Cloth
        1. Getting ready
        2. How to do it…
        3. How it works…
      5. Physics System Modification
        1. Getting ready
        2. How to do it…
        3. How it works…
      6. Joints
        1. Getting ready
        2. How to do it…
        3. How it works…
        4. There's more…
    27. A. Advanced Topics
      1. Introduction
      2. Generic collisions
        1. Minkowski Sum
        2. Gilbert Johnson Keerthi (GJK)
        3. Expanding Polytope Algorithm (EPA)
      3. Stability improvements
        1. Arbiters
        2. Accumulated impulse
      4. Springs
        1. Collision resolution
        2. Softbody objects
      5. Open source physics engines
        1. Box2D Lite
        2. Box2D
        3. Dyn4j
        4. Bullet
        5. ODE
        6. JigLib
        7. React 3D
        8. Qu3e
        9. Cyclone Physics
      6. Books
      7. Online resources
      8. Summary
    28. Index