Game Development Reference
In-Depth Information
Scene Kit uses the SCNPhysicsBody object to add physics simulations to a node. Dur-
ing the render of a scene, Scene Kit prepares the frame in which it will perform physics
calculations. These calculations include gravity, friction, and collisions with other nodes.
The SCNPhysicsBody class has a couple of properties that you will need to set for each
of your SCNNode s in order to detect the collisions.
spaceManNode!.physicsBody = SCNPhysicsBody(type: .Dynamic,
shape: nil)
As you can see, the first item we are setting is the type. Scene Kit has three different types
of physics bodies.
Static : This type of physic body is unaffected by forces or collisions. You
will use this for the floor, obstacles, and walls since they will collide with
objects, but they themselves will not move.
Dynamic : This type of physic bodies is affected by forces or collisions. You
will use this type for the spaceman and enemy.
Kinematic : This type is unaffected by forces or collisions but can cause colli-
sions to occur within the physic world. You will not use this type, but it
could be used for an invisible node that would represent the user's finger
when touching an object.
Shape is the next parameter in the initializer. The shape defines the body for collision de-
tection. In your calls you will set this to nil , which will allow Scene Kit to automatically
create a physics shape based on the node's geometric property. If you want to have more
control over the actual shape Scene Kit uses for collision detection, then you can set it to a
different shape. For your purpose, allowing Scene Kit to define the shape will be enough
for you. Listing 14-6 gives you the physicsBody you will need to set on all of your
nodes. So, take your time and set each of the nodes at each of their locations. Note that
since you are using the clone method for each of the walls, you will need to add the new
method call only the first time you create the wall node.
Listing 14-6 . SCNPhysicsBody Class Initialization
//Add the following line to the GameViewController
setupSpaceMan():
spaceManNode!.physicsBody = SCNPhysicsBody(type: .Dynamic,
shape: nil)
Search WWH ::




Custom Search