Configure Physics Categories

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

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Use Physics Bodies for Collision Detection | TOC | Handle Contact Between Physics Bodies 👉

Before you can set up physics categories, you first need to understand what SpriteKit does when two physics bodies attempt to occupy the same space, and what types of interactions are possible. With SpriteKit, there are two types of interactions: a contact and a collision. A contact occurs the moment body A touches body B. A collision occurs when two bodies collide. The difference between these two interactions is subtle but important. The first type of interaction is generally used to handle some type of game logic. For example, knowing when the player collects an item. The second type of interaction is used more for simulating real-world physics — for example, a ball bouncing off the side of a wall.

SpriteKit determines how to handle contacts and collisions between bodies through the use of physics categories. Physics categories are set up by you, the developer; you can have up to 32 different categories per scene. With physics categories, you’re able to set up game logic based on these interactions. Furthermore, by precisely restricting the interaction between certain bodies, you limit the number of interactions in each frame, which increases your game’s overall performance.

For Gloop Drop, you’ll use four physics categories: none, player, collectible, and foreground.

--

--

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.