Configure the Physics Bodies

Apple Game Frameworks and Technologies — by Tammy Coron (116 / 193)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Add a New Collectible Component | TOC | Add a Physics Contact Extension 👉

In Chapter 8, Using the Scene Editor to Add Physics, you used the Scene Editor to add a physics body to the player node. However, as you start to build out Val’s Revenge, keeping track of the physics categories can (and will) get a little tricky. While it’s entirely possible to use only the Scene Editor, setting up the physics bodies like you did for the player node, you have another option: use components and extensions.

Unlike the physics code you wrote in Configure Physics Categories, where you simply set up a single enum to hold the physics categories using a UInt32 type, you’ll instead expand that idea and use a combination of String enums and protocols. You’ll also create and use a new physics component to set up your physics bodies.

To make this solution more robust, you won’t handle the collisions directly in the GameScene class; instead, you’ll build out a separate GameScene extension to further keep your project code organized.

There’s a lot to do. Let’s start with setting up the physics categories.

Mapping and Designing Your Physics Categories

Before you start writing physics-related code — well, any code, really — it’s always nice to have a plan. The plan, here, is to use seven…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.