## 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:

at frame 3:

at frame 4:

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