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.

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

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:

ExplosionParticleSystem

You can use the following image as a starting point for your particle system:

CreateExplosionParticles0

Note: If you get stuck or don’t want to try the challenge, download the solution particle system and add it to your project.

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:

  1. createExplosion(_: position: rotation:) takes three parameters: geometry defines the shape of the particle effect, while position and rotation help place the explosion into the scene.
  2. This loads Explode.scnp and uses it to create an emitter. The emitter uses geometry as emitterShape so that particles will emit from the surface of the shape.
  3. 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:).
  4. Finally you call addParticleSystem(_: wtihTransform) on scnScene 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.

Note: You’re using presentationNode because the physics simulation is currently moving the node.

Build and run; tap away and make those nodes explode!

BuildAndRun2

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!

Contributors

Over 300 content creators. Join our team.