Run a Playground Physics Simulation

Before jumping back into the Gloop Drop game project, open the PhysicsSimulation.playground file in the projects/playgrounds folder. Playgrounds offer an interactive development environment where you can prototype and test your code in real time. The PhysicsSimulation playground is a small, custom playground that you can use to play around with the SpriteKit physics engine.

The image shows the PhysicsSimulation playground in its running state.

images/WorkingWithPhysicsAndCollisionDetection/playground-callouts.png

To run the playground, click the Execute button in the bottom-left corner. This action runs the code and loads the SpriteKit scene in the Live View. You may need to start and stop the playground more than once to get it to respond to clicks.

If you don’t see the Live View, click the Adjust Editor Options button in the top-right corner and turn on the Live View option. The menu looks like this:

images/WorkingWithPhysicsAndCollisionDetection/live-view-option.png

You won’t need to write any code in this section, except for a handful of lines. Instead, you’ll use the existing code in the PhysicsSimulation playground to learn more about the SpriteKit physics engine. Later in this chapter, you’ll apply what you’ve learned to the gloopdrop project.

With the PhysicsSimulation playground running, click a few random places on the Live View scene, and you’ll see something similar to the image.

images/WorkingWithPhysicsAndCollisionDetection/playground-build-00.png

To understand what’s happening, look through the code in the playground. You’ll find that the touchDown(atPoint:) method contains the following code:

 // Check if toggle button was clicked
 if​ physicsToggle.​contains​(pos) {
  withBody.​toggle​()
 return
 }
 
 // Set up a circle shape node to use for the ball
 let​ ball = ​SKShapeNode​(circleOfRadius: 50)
 ball.name = ​"ball"
 ball.position = pos
 
 // Check value of `withBody` to determine if physics is enabled
 if​ withBody == ​true​ {
  ball.fillColor = ​SKColor​.white
  ball.physicsBody = ​SKPhysicsBody​(circleOfRadius: ball.frame.width/2)
 
 // Set up physics properties
  ball.physicsBody?.restitution = 0.2 ​// Bounce: 0.0-1.0 (default: 0.2)
 }
 
 // Add the ball node to the scene
 addChild​(ball)

This code, among other things, places a shape node named ball at each touch location (pos). The withBody property is currently set to its default value of false, so the code skips over coloring the shape white and adding a physics body to the node—more on physics bodies in a moment.

Click the “Physics Bodies: OFF” button at the top of the scene to toggle the withBody property to true. Once again, click anywhere on the Live View scene and notice that this time, the balls react to gravity and to each other.

images/WorkingWithPhysicsAndCollisionDetection/playground-build-01.png

In this second example, where withBody = true, the three lines that were skipped earlier now run:

 ball.fillColor = ​SKColor​.white
 ball.physicsBody = ​SKPhysicsBody​(circleOfRadius: ball.frame.width/2)
 
 // Set up physics properties
 ball.physicsBody?.restitution = 0.2 ​// Bounce: 0.0-1.0 (default: 0.2)

The fillColor property—which is not part of the physics engine and is only being used as a visual cue for this example—sets the shape node’s color to white. After that, the physicsBody property of that same node gets set using one of the SKPhysicsBody class initializers. The type of initializer you use depends largely on the shape of the physics body you need. In this case, your node is shaped like a circle, so it makes sense to use init(circleOfRadius:).

Finally, the restitution property of the physics body attached to the ball node is set to 0.2. (The restitution property determines the node’s bounciness, in other words, how much kinetic energy the body loses or gains from collisions.) It’s this physics body that makes everything come alive in the SpriteKit physics engine. In fact, change the restitution to 1.0, restart the playground, and you’ll see a big difference in how the balls react when hitting the floor—the higher the number, the more bouncy things get. The restitution property is but one of many properties available for physics bodies.

To better understand the SKPhysicsBody class and how physics bodies work, you’ll continue to use the PhysicsSimulation playground in the next section before returning to the gloopdrop project.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset