Search code examples
mathformulaeasing

Moving an object with geometric progression


I'm moving some object by it's X coordinate towards a target like this:

object.x += (target - object.x) * 0.1;  // Distance reduced by 10% each frame

I'm executing this each frame at 60 FPS. How to calculate time in seconds (or number of frames) needed to reach the target (be closer than given radius)?

I think it's called geometric progression or exponential decay but couldn't find how to apply these ideas and formulas to solve my problem.


Solution

  • The clue is given by the comment:

    // Distance reduced by 10% each frame
    

    This can be used to construct the explicit formula for the final position:

    final = abs(initial - target) * pow(1 - 0.1, frames);
    

    The initial displacement is multiplied by 0.9 each frame (i.e. lowered by 10%). The power term accumulates these factors.

    To invert the expression, use a logarithm:

    frames = log(min_dist / abs(initial - target)) / log(1 - 0.1);
    

    (Note that some languages have a variant of log which accepts a base; the above is the equivalent alternative in the case that your language does not.)


    Edit: to calculate the multiplier:

    mult = 1 - pow(min_dist / abs(initial - target)), 1 / frames);