Scene Kit Tutorial with Swift Part 5: Particle Systems
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.
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
Scene Kit Tutorial with Swift Part 5: Particle Systems
30 mins
- Getting Started
- SCNParticleSystem
- Particle System Editor
- Configuring the Trail Particle System
- Emitter Attributes
- Simulation Attributes
- Image Attributes
- Image Sequence Attributes
- Rendering Attributes
- Physics Attributes
- Life Cycle Attributes
- Adding the Trail Particles
- Adding a Heads-up Display
- Adding Touch Handling
- Naming Nodes
- Adding a Touch Handler
- Using the Touch Handler
- Challenge
- Shaping Particle Explosions
- Juice
- Where To Go From Here?
Challenge
Time to up the cool factor – and what’s cooler than explosions? I know: nothing, right?
That brings you to the challenge of this chapter: create another particle system and name it Explode.scnp. See if you can figure out what attributes to modify to make those particles explode.
The effect should look something similar to this:
You can use the following image as a starting point for your particle system:
Shaping Particle Explosions
Now that you’ve created the explosion particle system, you need to add some code to make those nodes explode. You’re going to use some special properties to make the explosion take the same shape as whatever node you touch.
Add the following to the bottom of GameViewController
, below touchesBegan(_: withEvent)
:
// 1
func createExplosion(geometry: SCNGeometry, position: SCNVector3,
rotation: SCNVector4) {
// 2
let explosion =
SCNParticleSystem(named: "Explode.scnp", inDirectory:
nil)!
explosion.emitterShape = geometry
explosion.birthLocation = .Surface
// 3
let rotationMatrix =
SCNMatrix4MakeRotation(rotation.w, rotation.x,
rotation.y, rotation.z)
let translationMatrix =
SCNMatrix4MakeTranslation(position.x, position.y,
position.z)
let transformMatrix =
SCNMatrix4Mult(rotationMatrix, translationMatrix)
// 4
scnScene.addParticleSystem(explosion, withTransform:
transformMatrix)
}
Here’s the play-by-play of the above code:
-
createExplosion(_: position: rotation:)
takes three parameters:geometry
defines the shape of the particle effect, whileposition
androtation
help place the explosion into the scene. -
This loads Explode.scnp and uses it to create an emitter. The emitter uses
geometry
asemitterShape
so that particles will emit from the surface of the shape. -
Enter the Matrix! :] Don’t be scared by these three lines; they simply provide a combined rotation and position (or translation) transformation matrix to
addParticleSystem(_: withTransform:)
. -
Finally you call
addParticleSystem(_: wtihTransform)
onscnScene
to add the explosion to the scene.
You’re so close to replicating those great Hollywood explosions! Add the following line twice inside handleTouchFor(_:)
, once to the “good” if
block and once to the “bad” else
block, right before you remove node
from the parent:
createExplosion(node.geometry!, position: node.presentationNode.position,
rotation: node.presentationNode.rotation)
This uses the presentationNode
property to retrieve the position
and rotation
parameters of node
. You then call createExplosion(_: position: rotation:)
with those parameters.
presentationNode
because the physics simulation is currently moving the node.
Build and run; tap away and make those nodes explode!
Juice
Congratulations! At this point you have completed your first Scene Kit game.
However, there’s still plenty room for improvement, right? To push your game to that next level, you absolutely have to add something known as juice. Juice will give your game that little something special, just to make it stand out above the rest.
Here’s a few ideas that will definitely juice things up:
- Game state management. With basic game state management you’ll be able to control certain game machanics based on a game state like TapToPlay, Playing or GameOver.
- Splash screens. Use pretty splash screens. They provide the player with visual clues of the current game state.
- Sound effects. Add cool sound effects to provide the player with crucial audio feedback of good and bad interaction with the game elements.
- Camera shakes. Really bad explosions produce really big shockwaves. Adding a shaking camera effect just that little something extra.
Where To Go From Here?
Here is the example code from this Scene Kit tutorial with Swift (with the juice applied).
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!