mario math – part 2
To calculate Mario’s position with KEY_RIGHT pressed down continuously for n frames:
let 
at frame 1:

at frame 2:
![\begin{align} & v_2 = a(.89) + a \\ & p_2 = a + [a(.89) + a] \end{align}](http://twoguysarguing.files.wordpress.com/2009/08/pcalc_eqn3.png?w=720)
at frame 3:
![\begin{align} & v_3 = (a(.89) + a).89 + a \\ & p_3 = a + [a(.89) + a] + [(a(.89) + a).89 + a] \end{align}](http://twoguysarguing.files.wordpress.com/2009/08/pcalc_eqn4.png?w=720)
at frame 4:
![\begin{align} & v_4 = ((a(.89) + a).89 + a).89 + a \\ & p_4 = a + [a(.89) + a] + [(a(.89) + a).89 + a] + [((a(.89) + a).89 + a).89 + a] \end{align}](http://twoguysarguing.files.wordpress.com/2009/08/pcalc_eqn5.png?w=720)
If you multiply everything through, a pattern starts to emerge:

So we can say that in general, Mario’s position after n frames will be:

But we’d like a formula we can evaluate in O(1), so massaging the equation a bit we get:

Both of sums in the above equation, we have identities for:
and 
Of which the second one is kind of convoluted, but allows us to come up with:

Which can then write in Java as:
public float positionAfter(float accel, int nFrames) {
float term1 = n * (1 - Math.pow(.89, nFrames + 1)) / .11;
float term2 = (Math.pow(.89, nFrames + 1) * (-.11 - 1) + .89) / .2079;
return a * (term1 - term2);
}
The (.89) term is friction (I think?) and on the ground the code uses that value. In the air, the dynamics and equations are the same, but use a different coefficient of friction: (.85). The initial acceleration of a jump is vastly different, though, so I’ll tackle that in the next Mario Math installment.

leave a comment