Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a CurrentValue and a TargetValue. Every frame, i want the CurrentValue to "lerp" towards the TargetValue.

I always seem to get jitter, because the currentvalue never perfectly hits the targetvalue. Lets say i have a targetvalue of 5 and a currentvalue of 4, and the speed is 2. Then the value will go from 4 -> 6 -> 4 -> 6

I have been looking at the Lerp function, but i just don't see how exactly i can use it.

The practical applications im thinking of is accelleration of a car, turning a turret on a tank etc. What are some best practices on doing the above?

Thanks in advance :)

share|improve this question
    
It just hit me that i should be able to use lerp, since i know the time between frames, i know the targetvalue and the speed. So i should know how much time it will take from one value to another. I will see if i can make this work the way i have it stacked in my head atm :) –  Moulde Oct 27 '11 at 21:58
add comment

3 Answers

up vote 5 down vote accepted

In general, lerp functions don't take a speed, they take a parametric representation of how much they should be in between your start (A) and end (B) parameters.

Of course, if you have a constant speed, you can figure out how long it should take you to go from A to B doing some simple math. If you're moving at X m/s, and you need to travel Y m, then you can get time by doing Y / X (if you can't remember this, just remember your units and solve for seconds).

Now in your code you have a time amount, a start position, and an end position. You're also going to have to keep track of elapsed time to give you your parametric (0..1) value to pass into your lerp function.

So if this is in a Tick function somewhere:

currentMovementTime += deltaTime;
var t = currentMovementTime / totalCalculatedTime;
var currentPosition = Lerp( startPosition, endPosition, t );

One common thing to do is that if your current elapsed time is greater than your total calculated time, just clamp it (so your t is 1). That way you'll calculate your end position to be the actual end position you want to be at.

All that being said, your particular examples aren't terribly suited for lerping. For rotating a turret you probably just want to apply a constant rotational velocity while the user has a button held down. Likewise for acceleration. Generally speaking lerping is only really useful for things that you know take a fixed amount of time and aren't dependent on continuous user input.

share|improve this answer
add comment

A lerp function is something like this:

 NewCurrentValue = SourceValue + (TargetValue - SourceValue) * Ratio;

Many times I choose a constant ratio, and work over current value, this way I have not to consider time duration and time elapsed, and results are fine.

 CurrentValue = CurrentValue + (TargetValue - CurrentValue) * 0.01f;
share|improve this answer
add comment

Compute two separate values:

  • delta, the difference between your current value and your expected value; in your case, 4 - 5, ie. -1.
  • step, the amount to change wrt. your speed, in your case 2 * Δt where Δt is your timestep (you appear to have a constant timestep Δt = 1, but if you want to cope with framerate jitter, you should consider adapting)

If abs(delta) < step it means you reached the target; set currentvalue = targetvalue.

Otherwise, set currentvalue += step * sign(delta).

share|improve this answer
    
Uhh, soo simple yet so awesome! Thanks :) –  Moulde Oct 27 '11 at 22:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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