Dynamic derring-do

Some conditionals are opaque unless I look at them in a different way.

if (hbDist < radius && hbDist > distIn) { ...

... && hbDist < distIn;

if (ball.sunk && hbDist > distIn) { ...

“hbDist” refers to hole-to-ball distance, or the distance from the center of a golf hole to the center of a golf ball – both modeled as circles. “radius” refers to the golf hole.

“distIn” is the difference between the golf hole radius and the golf ball’s radius. Neither change values, so this is a constant. Now we go to interpretation.

hbDist < radius

hbDist - radius < 0

I think of this as “hbDist gets smaller as the ball approaches the hole, until part of the ball is at the lip of the hole.” (At this point in the code, a vector toward the hole is applied to simulate the golf ball rolling in.) Without this inequality, the golf hole’s gravitational influence would not be limited to its lip; it would apply to the entire screen.

hbDist > distIn

hbDist - distIn > 0

Here I imagine “distIn” as a ruler of constant length extending from the center of the hole, but a little shorter than the actual radius of the hole; i.e., a hole with radius 10 and a ball with radius 3, conceptually, gives us a ruler of length 7. So hbDist gets smaller and smaller but the left side remains positive: the golf ball skirting the lip of the hole, but not quite sunk.

The last conditional describes the ball being in a “sunk” state, but also past the golf hole. How can the golf ball move past its cup? We are processing “a posteriori” collisions, after-the-fact collisions: to calculate how the ball should bounce, we allow it to “pass” the golf hole. (Visually, the graphics update fast enough that the golf ball does not actually appear to ignore the hole. Hopefully.)