Game Development Reference
In-Depth Information
Chapter
3
Adding Physics and Collision Detection to
Your Game
In the previous chapter I covered quite a bit of information including what SKScene ob-
jects are and how they are built. I also talked about the SKScene rendering loop and how
the order that the scene's node tree is constructed in can affect the look and interactivity of
the nodes. I closed the chapter with a look at a scene's coordinate system and node anchor
points.
In this chapter, I will cover Sprite Kit's physics engine and collision detection. I will begin
the chapter with a discussion of SKPhysicsBody —the class used to simulate collision
detection. You will then turn on gravity in the game world and see how that affects the
nodes. After that, you will add a touch responder to propel the playerNode up into
space, and I will close this chapter with a discussion of how to handle node collisions.
What Is an SKPhysicsBody?
To simulate physics in a Sprite Kit game, you add a physics body to a scene or node. A
physics body, implemented by the Sprite Kit class SKPhysicsBody , is a simulated ob-
ject attached to a node in the scene's node tree. The SKPhysicsBody class uses the prop-
erties of the node such as the position and velocity, combined with its own properties, to
simulate how physical forces are applied to the simulated game world. It does the calcula-
tions to perform these simulations with each iteration of the render loop.
There are three types of physics bodies. There is a dynamic volume, a static volume, and an
edge. A dynamic volume is a physical body with a volume and a mass that is affected by
collisions and forces in the physics simulation. Nodes with a dynamic volume attached to
them are the active nodes in games.
A physics body with a static volume is just like a dynamic volume, except forces and colli-
sions do not affect them. Static volume bodies are often used as barriers in a game. When a
Search WWH ::




Custom Search