mario math
To calculate Mario’s velocity, with KEY_RIGHT pressed down continuously for n frames:
let
(1.2 if KEY_SPEED is held down as well)
at frame one, then, your velocity 
frame 2: 
frame 3: 
frame 4: 
which, if you multiply through, looks like this:

Which makes it a bit easier to generalize Mario’s velocity at any frame n:


we know from paying attention in high school wikipedia that:

however, our sequence only goes to n-1 frames. It’s missing the last term:

subtracting xn from both sides


which we could have maybe figured out faster if we had remembered this identity:

replacing the lower m with 0 and upper n with n-1 we get:


which multiplied by
is what we found earlier.
and so mario’s velocity after n frames of acceleration is:

which can be calculated in constant time by this Java function:
public float velocityAfter(float accel, int nFrames) {
return accel * ((1 - Math.pow(0.89, nFrames)) / 0.11);
}
Whew.
Thanks to Heath Borders and my co-blogger Ben for working this out on a whiteboard with me.

1 comment