0
$\begingroup$

Say I have a timeline beginning at 0 and ending at 1. I have several points along the timeline and I want to lerp between them over time. e.g. pt1 to pt2, pt2 to pt3, pt3 to pt1. Once the value reachs 1, loop back to 0. But, the percentage from 1 back to 0 must be maintained.

So think of variable t as the current time on a clock between 0 and 1 and a and b are points on the timeline.

e.g.

a = 0.33
b = 0.5
t = 0.42

Finding what percentage t is between a and b?

"given number x in range between a and b, percentage = x - a  /  b - a."
0.42 is 53% between 0.33 and 0.5

Once t == b, the lerp between the two numbers should be 100%. Once this happens, a becomes 0.5, b becomes 0.33, and t continues as it was.

a = 0.5
b = 0.33
t = 0.72

The formula would not work the same because once it loops back passed 1 and becomes something like 0.3, it breaks. The distance between a and b is 0.83((1 - 0.5) + 0.33), so 0.3 should be somewhere like 80-90%. How would I find this?

$\endgroup$

2 Answers 2

1
$\begingroup$

$$ \frac{t-a}{(1+b)-a}$$ The $1+b$ allows the wrap around.

$\endgroup$
0
$\begingroup$

Your question is confusingly worded, but it seems like what you're saying is that there's some total time T, and then you have a variable t that keeps track of how much time since the beginning of the current loop. So in the first loop, the two variables are the same, but in the second loop, T is ahead of t by 1. If you've looped once, but haven't gotten to a yet your interval is going from b to a, but it's going from b from the first loop to a from the second loop. So the value of T at b is just b, but the value of T at a is a+1, and the value of T at t is t+1. The formula you originally had can be written as:

(current- beginning)/(end-beginning)

Now your current = t+1, beginning = b, end = a+1. So the formula comes out as:

(t+1-b)/(a+1-b)

If you plug in t = 3, a = .33, b = .5, you get

(.3+1-.5)/(.33+1-.5) = .8/.83 ~ .9639

In other word, your remaining time of .03 is ~3.61% of .83.

$\endgroup$
2
  • $\begingroup$ So, is t being reset every loop? i.e. when T == b, t resets to 0? Also, t should never be greater than 1. The formula works if t < a, but if t > b then t becomes larger than 1, which is more than 100%. $\endgroup$
    – slanden
    Sep 3, 2017 at 3:33
  • $\begingroup$ this formula works if the end variable is less than the beginning variable, but not the other way around. So, the same formula can't be used if the end variable is greater than the beginning variable. Is there a formula that works for both cases? $\endgroup$
    – slanden
    Sep 12, 2017 at 21:58

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.