Chapter 9. 2D Rigid-Body Simulator

After reading Chapter 8, you’ve learned the main ingredients that go into a simulator, specifically a particle simulator. In this chapter we’ll look beyond particles at 2D rigid bodies. The main difference here is that rigid bodies rotate, and you must deal with an additional equation of motion—namely, the angular equation of motion relating a rigid body’s angular acceleration and inertia to the sum of all moments (torques) acting on the rigid body. The fundamental elements of the simulator—the model, integrator, renderer, etc.—are the same as before; you just have to deal with rotation. In two dimensions, handling rotation is simple. Things get a bit more involved when handling rotation in three dimensions, and we’ll treat that problem in Chapter 11.

The example we’ll take a close look at in this chapter is simple by design. We want to focus on the differences between the particle simulator and a 2D rigid-body simulator. In Chapter 10, we’ll extend this simple example to deal with multiple rigid bodies and collisions. That’s where things really get interesting. For now, we’ll consider a single rigid body, a virtual hovercraft, that moves around the screen under the influences of thrust forces that you can control with the keyboard. While simple, this example covers the most fundamental aspects of simulating 2D rigid bodies.

Figure 9-1 shows our virtual hovercraft. The pointy end is the front, and the hovercraft will start off moving from the left ...

Get Physics for Game Developers, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.