Game Development Reference
In-Depth Information
dynamic body collides with a static body, the dynamic body is affected by the collision,
and the static body remains constant.
An edge is a static body without volume. The simulation never moves an edge, and an
edge's mass will not affect the simulation of other nodes. Edges represent negative space
within a scene.
Adding Physics to Your Game World
The easiest way to see how all this works is to just do it. Let's begin by creating and asso-
ciating an SKPhysicsBody with the playerNode . Add the following lines of code to
the GameScene 's .init(size: CGSize) method immediately following the con-
struction of the playerNode :
playerNode!.physicsBody =
SKPhysicsBody(circleOfRadius: playerNode!.size.width
/ 2)
playerNode!.physicsBody!.dynamic = true
Take a look at these two lines of code. The first line creates an SKPhysicsBody passing
the initializer a parameter named circleOfRadius with a CGFloat for the value.
Sprite Kit provides a handful of standard shapes for physics bodies, including circles
( circleOfRadius ), rectangles ( rectangleOfSize ), and a path-based polygon
( polygonFromPath ). The most efficient of these shapes is the circle, and the most in-
efficient is the path-based polygon.
Because the circle is the most efficient node shape, it will be the shape used for all of
physics bodies in this game. The value passed to the constructor, in this instance, is the
width of the playerNode divided by 2. I am using this value because I want to create a
circle around the playerNode starting from its center with a radius of half the width of
the node. This will result in a circle that surrounds the playerNode completely. If there
were an SKPhysicsBody(circleOfDiameter:) constructor, then I would not
need to divide the width by 2, but there is no such constructor.
Let's get back to the second line of this snippet. The second line turns the playerNode
into a physics body with a dynamic volume. It will now respond to gravity and other
physical bodies in the scene. Once you have made these changes, your new player setup
code should look like the following.
Search WWH ::




Custom Search