Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How would I incorporate elapsed time into linear interpolation?

Asked by 8 years ago

I'm updating a value 60 percent closer to it's target value every frame using the formula a+(b-a)*alpha. This works, but if someone has low frames it'll take longer to reach it's target.

local target = 4
local value = 0

local last = tick()
while rs:wait() do
local time = tick()
    local elaps = time - last
    value = value + (target - value) * .6
end

How can I modify this formula to use the elapsed time?

0
You could probably just multiply by elaps*30 nicemike40 486 — 8y

1 answer

Log in to vote
2
Answered by 8 years ago

Oh that's simple, but completely silly.

The issue is that in this equation, you're missing a fundamental scale of when the tween is supposed to finish. Are we supposed to go there in 2 seconds? 2 minutes? It's all about that end goal.

local target = 4
local value = 0

local last = tick()
while rs:wait() do
local time = tick()
    local elaps = time - last
    last = time
    value = value + (target - value) * (1-0.6^elaps)
end
0
I probably should've included a note on this; I know it will never finish. Target is constantly changing, I'm making a smooth transition between value changes YellowoTide 1992 — 8y
0
Then the time is irrelevant. If you have no target, your time is completely irrelevant. PlsNoDiscrimination 0 — 8y
0
I have a target, it is dynamic. Currently I'm moving value 60% closer to target every frame. If someone has 30 fps, their value will be far behind someone's who has 60 fps (Assuming they start at the same value and have the same target). YellowoTide 1992 — 8y
0
Right that's what you want. You'll need the delta, which is magic stuff might I add. Check my revised code. PlsNoDiscrimination 0 — 8y
Ad

Answer this question