Hello, I have been following a tutorial on how to implement a physics-simulated spring for my game, but was unable to sync movements across multiple framerates. I have researched on how to do this but have had no luck in my attempts, as I am fairly new to using deltatime.
However, this did not work. A higher framerate appeared to be much more harsh. Is there any visible problem in the logic of this statement? The time variable is listed above.
You don’t want to be multiplying the delta time value.
Also, remember, multiplication comes first in calculations, no matter where it is in the function.
Just try wrapping the whole thing in brackets and then multiplying outside.
I attempted this previously by setting the expression in parentheses, then multiplying by delta, but it didn’t make movement sync between framerates. Prior research has shown examples like yours, but they haven’t seemed to work for me.
What object are you changing the properties for? Are you directly setting a new position for the players primary part or setting a velocity for a bodymover like a linear velocity?
If they haven’t worked, you’re doing something very wrong.
For example:
--// Right
local num = ((13 * (4 + 8)) - (72 / 14)) * delta
--// Entire calculation is multiplied by delta
--// Wrong
delta *= desiredFps
local num = (13 * (4 + 8)) - (72 / 14) * delta
--// Only the second half (after subtraction) is multiplied by delta
You should only ever multiply the delta time in few calculations, like lerp functions.
And you should only ever divide it to get the clients current FPS.
DeltaTime is the time between frames, so multiplying it does nothing but throw math off.
Yes I did the first version before but it didn’t solve anything. Not really sure what the problem is as other people have shown solutions similar to that.
local TICK_RATE = 20 -- ticks per second
local TICK_INTERVAL = 1 / TICK_RATE -- seconds per tick
local deltaTime = 0
while true do
while deltaTime >= TICK_INTERVAL then -- in case deltaTime is > 2 * TICK_INTERVAL
signalOut() -- Do something on the tick
deltaTime -= TICK_INTERVAL -- account for error
end
deltaTime += task.wait() -- Using TICK_INTERVAL here is an option, but beware for error
end
This is a simple way to make it so actions runs at roughly the same rate for everyone, although it falls apart if the user is already struggling to make the target tickrate.
But to more accurately address your question in OP exactly, though, we could use a busy wait
-- starting with some external deltaTime
local previousTime = time() - deltaTime
while deltaTime < DESIRED_FRAME_TIME do
local currentTime = time()
deltaTime = time() - previousTime
previousTime = currentTime
end