O'Reilly logo

Build iOS Games with Sprite Kit by Josh Smith, Jonathan Penn

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required