Bumping bees into bees

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 didMove function in GameScene.swift. At the bottom, after 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 toward 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 ...

Get Swift Game Development - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.