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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
SceneKit Tutorial With Swift Part 3: Physics
20 mins
Welcome back to our Scene Kit Tutorial With Swift series, where you’ll learn how to make your first 3D iOS game: a game like Fruit Ninja called Geometry Fighter!
This tutorial series will show you how to create your first game with Scene Kit, Apple’s built-in 3D game framework.
In the first part of the series, you learned how to make an empty Scene Kit project as a good starting point.
In the second part of the series, you started making your game, learning about Scene Kit nodes along the way.
In this third part of the series, you’ll learn how to make your geometry move through the power of Scene Kit physics.
Let’s dive back in!
Getting Started
In this tutorial, you’ll use SceneKit’s physics engine to add physics to your game.
SceneKit’s physics engine is powerful, yet easy to use. You simply tell SceneKit on which objects you want to apply physics, and the engine will take over from that point, simulating things such as gravity and collisions.
Before you dive into integrating physics into your game, you’ll first need to add some game utilities to your project.
Introducing the Game Utilities
The game utilities were created especially for you. They include helper methods which handle the complicated bits in your game. This lets you focus on the gameplay rather than those pesky bits.
Download the SceneKit_Part3_resources here.
Adding the Game Utilities
To add the game utilities, simply drag and drop the GameUtils folder into your project under the GeometryFighter group folder:
Leave all the settings at their defaults and click Finish:
This imports the entire GameUtils folder into your project as a group. Expand this group folder and have a quick look at some of the helper methods. Don’t worry too much if some of the code doesn’t make sense to you yet.
Physics
Time for a quick status check of the current state of your game. Build and run the game; a cool random geometric object spawns out of thin air like some kind of dark magic. This might not seem like much right now, but things will definitely start to shape up soon! :]
The freshly spawned object just hangs there in an empty space and doesn’t do much. Sure, you can rotate the camera around it and zoom in and out, but that’s about it. It’s not much of a fun game. To pump up the excitement level, it would be nice if the object at least moved around a little.
Now, you could take the long way around and manipulate the object’s position and rotation over time so that it spins and moves around. You’d soon realize that although it’s possible to animate objects in this manner, it requires a lot of coding effort — especially when you add into the mix other features like realistic collisions and interactions between objects.
Thankfully, the developers at Apple have already thought about this; to this end, they integrated a very powerful 3D physics engine into SceneKit. To make use of this built-in physics engine, you simply need to make the engine aware of your object.
In the same way you attach geometry information to your node, you can attach a physics body to your node. The physics body describes all the physical properties of your node, which includes things such as shape, mass, friction, damping and restitution. The physics engine takes all this information into account when it simulates the real-world physics interactions of your objects. This includes things such as gravity, friction and collisions with other bodies within the physics world.
The next section details some of the important characteristics of a physics body.
Physics Body Types
One of the key properties you must specify when creating a physics body is its type. The physics body type defines how the body interacts with forces and other bodies in the simulation.
There are three types used in SceneKit:
- Static bodies don’t move: while other objects can collide with these bodies, the static bodies themselves are unaffected by any forces and collisions in the simulation. You can use this type for things like walls and massive immobile boulders.
- Dynamic bodies are automatically moved by the physics engine in response to forces and collisions. You can use this type for things such as movable chairs, tables and cups.
- Kinematic bodies are not automatically moved by the physics engine in response to forces and collisions. In other words, if a kinematic body collides with a dynamic body, the dynamic body will move, but the kinematic body won’t. However, you can move kinematic bodies around manually in code. You can use this type for things you want to control manually, such as moving elevators or a door that can open and close.
Physics Shapes
In addition to the type of the body, another import property you must specify when creating a physics body is its shape. The physics shape defines the 3D shape used by the physics engine during collision detections. While the geometry defines the visuals of the node, the physics body defines how the node interacts with other objects in a physics simulation.
You can make the shape exactly the same as the visual geometry, but this is often more computationally expensive than you need (hence causing your game to run slower). Instead, you should usually work with a simplified version of the geometry for your shape, such as a simple bounding box, sphere or one of the provided primitive shapes that roughly matches the node’s visible appearance like so:
Adding Physics
Now that you’ve learned the theory behind the physics, it’s time to start using these concepts to move things around in your game.
In SceneKit, all the physics bodies are SCNPhysicsBody
objects. Once you create the physics body, you can assign it to the physicsBody
property of the SCNNode
instance. Once you assign the physics body to the appropriate node, the physics engine can simulate the physics for you. It’s that simple! :]
Open GameViewController.swift and add the following after the line of code that creates geometryNode
in spawnShape()
:
geometryNode.physicsBody =
SCNPhysicsBody(type: .dynamic, shape: nil)
This line of code creates a new instance of SCNPhysicsBody
and assigns it to the physicsBody
property of geometryNode
. When you create a physics body, you specify the type and shape of the body. If you pass in nil
for the physics shape, SceneKit will automatically generate a shape based on the visual geometry of the node. Neat, huh?
If you want to add more detail to the physics shape, you can create a SCNPhysicsShape
and use that for the shape instead of passing in nil
.
Build and run your game; a random shape spawns into the scene, and then drops out of the air with all the grace of a dead bird, falling out of sight:
You can even pan the camera to watch the object fall into oblivion. What you’re witnessing here is the effect of gravity acting on the object. A scene in SceneKit has gravity turned on by default. Now that the spawned object has a physics body, the physics simulation will apply simulated forces — such as gravity — to the object.