Book Image

Game Development with Swift

By : Stephen Haney
Book Image

Game Development with Swift

By: Stephen Haney

Overview of this book

Table of Contents (18 chapters)
Game Development with Swift
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Bee meets bee


You may have noticed that we positioned bee2 and bee3 at the same height in the game world. We only need to push one of them horizontally to create a collision – perfect crash test dummies! We can use an impulse to create velocity for the outside bee.

Locate the didMoveToView function in GameScene.swift. At the bottom, below all of our spawn code, add this line:

bee2.physicsBody?.applyImpulse(CGVector(dx: -3, dy: 0))

Run the project. You will see the outermost bee fly towards the middle and crash into the inner bee. This pushes the inner bee to the left and slows the first bee from the contact.

Attempt the same experiment with a variable: increased mass. Before the impulse line, add this code to adjust the mass of bee2:

bee2.physicsBody?.mass = 0.2

Run the project. Hmm, our heavier bee does not move very far with the same impulse (it is a 200-gram bee, after all.) It eventually bumps into the inner bee, but it is not a very exciting collision. We will need to crank up the impulse...