0
\$\begingroup\$

How to lerp velocity to 0 in specific time? For example in two seconds? I need this for jumping.

velocity.Y = MathHelper.Lerp(velocity.Y, 0, [time]);
\$\endgroup\$
  • \$\begingroup\$ This looks like XY problem - are you sure you did not wanted to ask how to implement simple gravity? \$\endgroup\$ – wondra May 28 '16 at 17:57
  • \$\begingroup\$ Your problem translates into traversing the (0:1) range in a given amount of time. This should be your starting point. Have you considered doing this? The comments and answers seem to point out specific details you might have not given or overlooked. \$\endgroup\$ – teodron May 28 '16 at 18:55
  • \$\begingroup\$ Technically speaking, it's impossible to lerp to 0 \$\endgroup\$ – Steve H May 29 '16 at 13:17
1
\$\begingroup\$
float Timer = 2f;
float TIMER = 2f;
float StartVelocity = 10;
float EndVelocity = 0;

void Update()
{
     float elapsed = (float)gameTime.elapsedSecondsOrWhatever;

     Timer -= elapsed;
     if (Timer < 0) 
         Timer = 0;

     velocity.Y = MathHelper.Lerp(EndVelocity, StartVelocity, Timer/TIMER);     
}
\$\endgroup\$
0
\$\begingroup\$

Your question is vague (could you please elaborate?), so I'll assume that you don't know how lerping works and that you don't have access to the math function, then you can write your own like this:

float lerp( float value1, float value2, float alpha, float range ){

  alpha = alpha/range;

  if( alpha < 0 )
    alpha = 0;
  if( alpha > 1 )
    alpha = 1;


  return value1 + (alpha) * (value2-value1);
}

Optionally, using alpha blending:

return (1-alpha) * value1 + alpha * value2;

Then, you can call

velocity.y = lerp( velocity.y, 0, delta_time, 2.0);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.