Chapter 7. Physics

If, like us, you’ve visited a planet that has gravity, you’ll be familiar with the fact that objects react to forces and collide with other objects. When you pick up an object and let go, it falls down until it hits something. When it hits something, it bounces (or shatters, depending on what you dropped). In games, we can make objects have this kind of behavior through physics simulation.

Physics simulation lets you do things like:

  • Make objects have gravity and fall to the ground

  • Give objects properties like weight, density, friction, and bounciness

  • Apply forces to objects, and make them move around realistically

  • Attach objects together in a variety of configurations

In short, adding physics simulation to your game often gives you a lot of realism for free.

SpriteKit has built-in support for simulating physics in two dimensions, and we’ll mostly be talking about physics in SpriteKit in this chapter. (If you’re not familiar with SpriteKit yet, go check out Chapter 6.) Before we get to the recipes, though, let’s go over some terminology.

7.1 Reviewing Physics Terms and Definitions

Physics simulation has its basis in math, and math people tend to like giving everything its own name. These terms are used by the physics simulation system built into iOS, and it’s important to know what’s being referred to when you encounter, say, a polygon collision body.

In this section, before we get into the recipes themselves, we’re going to present a list of definitions ...

Get iOS Swift Game Development Cookbook, 3rd 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.