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.
for frame 2, why v = a * 0.89 + a
If initial vel. is ‘a’ and accelrn. is ‘a’
then after time t,
v = a + a * t
After time t1 after t,
v2 = v + a*t1
i.e. v2 = a + a*t + a*t1 = a (1 + t + t1)
v3 = v2 + a*t2 = a(1 + t + t1 + t2)
How come the factor of 0.89 and why v2, v3 are calculated differently? What have I missed?
Thanks.