O'Reilly logo

Swift Game Development - Third Edition by Stephen Haney, Siddharth Shekar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding objects to the scene

Let's next add geometry to the scene. We can create basic geometry such as spheres, boxes, cones, tori, and so on in SceneKit with a lot of ease. Let's create a sphere first and add it to the scene.

Adding a sphere

Create a new function called addGeometryNodes in the GameViewController class, as follows:

func addGeometryNode() {

   let sphereGeometry = SCNSphere(radius: 1.0)
   sphereGeometry.firstMaterial?.diffuse.contents = UIColor.orange
        
   let sphereNode = SCNNode(geometry: sphereGeometry)
   sphereNode.position = SCNVector3Make(0.0, 0.0, 0.0)
   self.rootNode.addChildNode(sphereNode)        
}

We will use the SCNSphere class to create a sphere. We can also call SCNBox, SCNCone, SCNTorus, and so on to create a box, a cone, or a torus.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required