Scene Kit Tutorial with Swift Part 3: Physics

In this 5-part Scene Kit tutorial series, you’ll learn how to make your first 3D iOS game: a game like Fruit Ninja called Geometry Fighter! By Chris Language.

Leave a rating/review
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Positioning the Camera

To move the camera position up, replace the line where you set the camera’s position in setupCamera() with the following:

cameraNode.position = SCNVector3(x: 0, y: 5, z: 10)

Build and run, and you’ll see that the object appears to spawn off-screen!

Offscreen

Adding Color

As a final touch, you’ll add a bit of variety to your randomly generated shapes.

Add the following lines inside spawnShape(), after the spot where you randomly create geometry, but just before you create geometryNode:

geometry.materials.first?.diffuse.contents = UIColor.random()

To color your random object, you modify materials on geometry; this line gets the first available material of geometry and sets the diffuse property’s contents to a random UIColor. The random() method on UIColor is another helper defined as an extension inside the game utilities.

Note: You’ll learn more about materials and their properties in Section II of this book. The takeaway here is that you can assign a UIColor to the diffuse property’s content in order to change the object’s color.

Finally, build and run your game to see a beautifully colored object:

BuildAndRun2

Wouldn’t it be neat if you could spawn more than one object? You’ll fix this…in the next part of the series! :]

Where To Go From Here?

Here is the example code from this Scene Kit tutorial with Swift.

At this point, you should keep reading to the fourth part of this tutorial series, where you’ll learn how to make geometry spawn over time using the Scene Kit render loop.

If you’d like to learn more, you should check out our book 3D iOS Games by Tutorials. The book teaches you everything you need to know to make 3D iOS games, by making a series of mini-games like this one, including a games like Breakout, Marble Madness, and even Crossy Road.

In the meantime, if you have any questions or comments about this tutorial, please join the forum discussion below!

Contributors

Over 300 content creators. Join our team.