Game Development Reference
// "real" gravity falls off by the square over distance
float distanceSquared = distance * distance;
b2Vec2 force = ((1.0f / distanceSquared) * 20.0f) * bodyToFingerDirection;
physicsBody- > ApplyForce(force, physicsBody- > GetWorldCenter());
The multiplication by 20.0f in this case is a magic number. It's just there to make the
gravitational pull noticeable enough. Now the ball will speed up more the closer it gets
to your finger and will barely move if you touch the screen relatively far away from the
Although the applyForceTowardsFinger code serves only as a temporary con-
trol mechanism, you could use the gravity code in Listing 13-11 to create magnetic ob-
jects on your pinball table.
Adding the Bumpers
Now that you have a ball you can move with your finger, let's make things a bit more
interesting by introducing bumpers to the game. What are bumpers? They're the round,
mushroom-shaped objects that force the ball away when it touches them.
Note Sometimes people confuse bumpers with the flippers that the player con-
trols or the usually triangular slingshots just above the flippers. If you want to
refresh your memory about pinball terminology, the Wikipedia article on pin-
ball can help clarify it: en.wikipedia.org/wiki/Pinball .
Listing 13-12 shows the, once again, rather simple header file of the Bumper class.
@interface Bumper : BodySprite