ISBN 978-1-4666-1634-9 Chapter 8

Algorithmic and Architectural Gaming Design: Implementation and Development

Practical Introduction to Rigid Body LCP (Linear Complementary Problem) Constraint Solvers Ben Kenwright Newcastle University b.kenwright@ncl.ac.uk

(a)

Dr. Graham Morgan Newcastle University graham.morgan@ncl.ac.uk

(b)

(c)

Figure 1. Simulations to demonstrate stable stacking (a), articulated joints for characters (b) and chains of objects (c).

Theme: Physics-based Simulations Difficulty: Medium/Hard Implementation: Theory and Implementation in C/C++

Summary The chapter introduces Linear Complementary Problem (LCP) Solvers as a method for writing a robust realtime physics simulator, and will explain principles and algorithms with practical examples and reasoning. When first investigating and writing a solver, a person can easily get overwhelmed by the number of different methods and lack of implementation details, so the chapter will demonstrate the various methods from a no-nonsense point of view rather than a theoretical one, using code snippets and real test cases to help understanding.

Sections

Introduction Collision information (Consistency and Manifolds) Jacobian and Constraints o Constraint Examples Linear Complementary Problem o Simplified Examples o Iterative Methods Implementation snippets, hacks and explanations Simulation Results (source code also available online) Sparse Matrices (introduction and example) Stability and Speed Parallelization

Overview With the ever increasing visual realism in today’s computer generated scenes, it should come as no shock that people also expect the scene to move and react no less realistically. With the computational power available today, the ability to run physically accurate real-time simulations is a must if you want to capture the audience’s attention.

Practical Introduction to Rigid Body Linear Complementary Problem (LCP) Constraint Solvers

Writing a robust, flexible and scalable rigid body physics simulator is a challenging task. Various methods exist to produce realistic simulations, the three main ones being penalty methods (springs), impulse methods (instant velocity changes) and solvers (using either Newtonian or Lagrangian Dynamics). Understanding and writing a good rigid body simulator will allow the reader to customize and design a system to meet any requirements, enabling improved overall performance and scalability. It also provides the ability for cross-platform expansion and an understanding of the underlying algorithms. Realization of the workings lets the reader optimize their simulator to suit their needs (with the ability to trade between speed and accuracy when needed). A solver is used to compute the exact magnitude of the forces necessary to satisfy constraints. These constraints can be from impacts, contacts or joints, having bilateral and unilateral properties. The simulator creates a set of linear equations which represents the various constraints and solves the system with valid forces/velocities. Using a solver requires less tweaking and tuning, and has many advantages over other methods, including the ability to work with large stacks or chains of objects, as well as allowing custom constraints (e.g. hinges, sliders). The chapter will demonstrate how such constraints are formulated in the solver so that custom constraints can be built (e.g. path constraints, angle limiting hinges), while giving basic constraints (ball joints, contacts) as examples to work with. A number of commercial simulators have already taken advantage of solvers, such as ODE and Bullet, and have demonstrated themselves in numerous simulations both in the video game and movie industry. There are a number of books available on the subject of impulse and penalty methods, but there is a scarce supply of information on writing a comparable solid simulator using LCP Solvers. The reader is introduced step by step with examples and reasoning to writing a working solver for simulating rigid body physics, introducing the mathematics and discussing the problems associated with implementing a practical solver. Giving basic code examples and simplified test cases will help give the reader a solid understanding of what is actually happening in a solver, allowing them to build upon this knowledge. A brief overview of optimisation and parallelisation methods will be given; so that once understanding is achieved, the reader can continue to improve upon their work. Simple code examples written in C/C++ will demonstrate the implementation of algorithms, giving the reader a basic foundation to start from, before going on to understanding more complex open source solvers such as those used in Bullet and ODE. Sparse matrices will be introduced as they are used to manage simulation data and can greatly affect the performance and reliability of a system. It also means that the reader is not tied to any specific libraries, making it easier to implement on different platforms with the same results. Comparing the cost of building a solver and managing large sparse systems with positive definiteness (stability) will be explored, while trying to keep the implementation as simple as possible, as complexity can be a big problem. One of the main advantages of an LCP solver over penalty and impulse methods is its ability to work with a wide range of cases with the minimum amount of user tuning. Solvers can provide a fast, stable, practical realtime dynamics simulator that can be used for articulated models, rigid body objects, contacts and collisions and even high precision simulations where accuracy is very important. The reader, after completing this chapter should walk away with an understanding of what a solver is, why they are used and how to write a simple practical implementation. They should be able to explain what a stable solver is and when it can become unstable (over-constrained/closed loop/bad matrices/floating point errors). This will give the ability to determine when and why the simulation has become unstable and how to deal with it.

Practical Introduction to Rigid Body Linear Complementary Problem (LCP) Constraint Solvers