The first thing we must implement to make this a real game is collision detection. Then, we'll add an end to the game; otherwise, it will be really boring. Finally, a few extra touches will make the game more appealing.
Collision detection in SceneKit is implemented as it is in SpriteKit. For every node that can collide, we must create a physics body and attach it to the node, setting a unique identifier for that body. Finally, a contact
delegate will receive a call when a collision is detected.
First of all, we define an enumeration to list all the possible types of bodies, of which there are only two in our case:
enum BodyType : Int { case jetfighter = 1 case cube = 2 }
In createJetfighter()
, we create a parallelepiped to act as a physics body for the jet fighter because employing the actual model we used for rendering is a waste of calculation resources. For the purpose of detecting a collision, a rough shape is enough:
func createJetfighter...