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 = 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...