There are two speed values, that are used by moving object. After a while speed switching occurs from V1 to V2. I need the formula that can be used for calculation of current distance (at specified time).

Input variable:

  • T - Current time


  • V1 - first speed
  • V2 - second speed
  • Ts - switching time between V1 and V2
  • P - switching power

Output variable:

  • S = ?

I tried to use next formula: S = Lerp[Clamp(T / Ts, 0, 1) ^ P), V1, V2] * T

But in this case the moving object will be returned back after time and moves forward again.

Probably acceleration should be used in the formula?

  • \$\begingroup\$ Explain switching power. This is more a math question than a coding or gamedev question. I see it this way: you have V1 and V2, and V1 is constant until Ts (an horizon) is reached. Then V2 is constant too from that point. What is P? \$\endgroup\$ Aug 22, 2019 at 16:45
  • \$\begingroup\$ P is how fast switching will be occurred. In my example formula it's interpolation alpha \$\endgroup\$
    – Broly
    Aug 22, 2019 at 17:15

1 Answer 1


Rewriting your speed transition formula as

float blend = clamp((currentTime - startTime)/transitionDuration), 0, 1);

Vector3 currentVelocity = startVelocity + (endVelocity - startVelocity) * blend;

Then during the transition we're applying a constant acceleration of:

Vector3 acceleration = (endVelocity - startVelocity) / transitionDuration;

Now we can integrate this to get the total displacement from the starting position at time T=0 in three parts.

First, for currentTime <= startTime,

Vector3 currentPosition= startPosition + startVelocity * currentTime;

Which at the end of this interval reaches a value of:

Vector3 accelerationStartPosition = startPosition + startVelocity * startTime;

Second, for startTime < currentTime <= startTime + transitionDuration, we can use the formula for position under constant acceleration:

$$\vec p(\Delta t) = \vec p_0 + \vec v_0 \cdot \Delta t + \frac 1 2 \vec a \cdot \Delta t^2$$

in code:

float transitionTime = currentTime - startTime;

currentPosition = accelerationStartPosition
             + startVelocity * transitionTime 
             + 0.5f * acceleration * transitionTime*transitionTime;

Which we can simplify to:

 currentPosition = startPosition + startVelocity * currentTime
            + 0.5f * acceleration * transitionTime*transitionTime;

Which again, at the end of the transition interval, takes a value of:

float endTime = startTime + transitionDuration;

Vector3 transitionEndPosition = startPosition
               + startVelocity * endTime
               + 0.5f * acceleration * transitionDuration*transitionDuration;

And lastly, for currentTime > startTime + transitionDuration:

currentPosition = transitionEndPosition + endVelocity * (currentTime - endTime);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .