Slowing Down the Ball on Rebound

You’ve surely noticed that as the ball bounces back and forth between the obstacles, it quickly zooms out of control. The high restitution values on the bumpers and targets increase the speed of the ball with every hit. As the ball ricochets around long enough, it picks up speed that even the best human couldn’t handle. We want the restitution to increase the speed of the ball, but we want to cap the ball’s rebound speed to keep the game playable.

More trigonometry to the rescue! Let’s write a method named capPhysicsBody:atSpeed: that we can call to slow down any physics body we want in the scene.

07-MorePhysics/step08/PhysicsBall/RCWMyScene.m
 
- (​void​)capPhysicsBody:(SKPhysicsBody *)body atSpeed:(CGFloat)maxSpeed ...

Get Build iOS Games with Sprite Kit 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.