Learn how to set up nodes for collision detection.
SpriteKit supports two kinds of interaction between physics bodies that come into contact or attempt to occupy the same space:
A contact is used when you need to know that two bodies are touching each other. In most cases, you use contacts when you need to make gameplay changes when a collision occurs.
A collision is used to prevent two objects from interpenetrating each other. When one body strikes another body, SpriteKit automatically computes the results of the collision and applies impulse to the bodies in the collision.
Your game configures the physics bodies in the scene to determine when collisions should occur and when interactions between physics bodies require additional game logic to be performed. Limiting these interactions is not only important for defining your game’s logic, it is also necessary in order to get good performance from SpriteKit.
SpriteKit uses two mechanisms to limit the number of interactions in each frame:
Edge-based physics bodies never interact with other edge-based physics bodies. This means that even if you move them by repositioning the nodes, edge-based physics bodies never collide or contact each other.
Every physics body is categorized. Categories are defined by your app; each scene can have up to 32 categories. When you configure a physics body, you define which categories it belongs to and which categories of bodies it should interact with. You define contacts and collisions separately.
The following code creates a red and a blue ball, and a ground object constructed from a series of points. By giving the red ball a
collision that matches the ground’s
category, you make the red ball bounce off the ground. However, the blue ball’s
collision is set to a different value and doesn’t interact with the ground.
The following image shows the paths taken by both balls when the code above is executed.