SceneKit Tutorial With Swift Part 3: Physics

In the third installment of our SceneKit With Swift tutorial, you’ll animate your Geometry Fighter game while learning about SceneKit physics. 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 some 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: 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 this series! :]

Where to Go From Here?

You’re making excellent progress, and you’re more than halfway through your first SceneKit game! If you’re flexible (unlike me), give yourself a pat on the back.

Here is the finished project from this part of the tutorial.

At this point, you should keep reading on 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!