Game Development Reference
In-Depth Information
Listing 14-7
.
SharedConstants.swift Contents for the Bit Mask
let CollisionCategorySpaceMan = 2
let CollisionCategoryObstecles = 4
let CollisionCategoryWalls = 6
let CollisionCategoryEnemy = 8
let CollisionCategoryFloor = 10
If you completed the first section of the topic for Sprite Kit, this code should look famili-
ar. However, while Sprite Kit's collision bit masks use UInt32 variables, Scene Kit uses
Int
variables. This is just one of the subtle differences between the two frameworks.
Now that you have the variables that you will use for the bit mask, you will need to again
update all of your nodes. As mentioned, these variables will be used to create a table to
determine whether an object should collide with another.
SCNPhysicsBody
has a couple of parameters that you will set in order for the collision
detection to work. The first of the two parameters is the category bitmask.
spaceManNode!.physicsBody!.categoryBitMask
= CollisionCategorySpaceMan
The second parameter to set is the
collisionBitMask
.
spaceManNode!.physicsBody!.collisionBitMask
= CollisionCategoryObstacles | CollisionCategoryWalls
| CollisionCategoryEnemy
So, basically what you are doing is as follows: this node is a
SpaceMan
, and it will inter-
act with the
Obstacle
,
Wall
, and
Enemy
nodes. Then, for the other nodes, it will be an
Obstacle
or
Enemy
, and it will interact with the spaceman.
that you have to set on each of the nodes. Make sure you do this after the
SCNPhysic-
sBody
has been initialized and set on the node. Otherwise, setting this on an optional
parameter will have no effect on the node. It is also important that you initialize the
physicsBody
of the
SCNode
before it is added as a child to your scene.
Listing 14-8
.
SCNPhysicsBody categoryBitMask and collisionBitMask