07-05-2024, 11:08 AM
(This post was last modified: 07-05-2024, 11:33 AM by josemendez.)
(07-05-2024, 09:38 AM)kodra Wrote: I still don't get it. LateUpdate() is called every frame. It's frame rate dependent. Then how Obi solvers runs in LateUpdate() while using a fixed timestep at the same time?
What do you mean? all callbacks in Unity are frame rate dependent, since they're called during a specific frame. Framerate independence comes from scaling things by the amount of time passed since last time you did them, regardless of where they are done during the frame.
If you mean FixedUpdate vs LateUpdate, a common misconception is that FixedUpdate() is called every X seconds which is not at all the case. It's just that FixedUpdate is guaranteed to be called once per timestep as opposed to once per frame, which means it may be called zero, one, or multiple times per frame while LateUpdate is guaranteed to be called *exactly* once per frame.
So if 0.2 seconds have passed since the last frame and your fixed timestep is 0.05, Unity will perform 4 (0.2/0.05) back to back calls to FixedUpdate() during that frame. If 0.01 seconds have passed since the last frame, Unity won't call FixedUpdate that frame and will just accumulate the amount of time passed, and check again next frame whether enough time has passed to call FixedUpdate() again. You can easily replicate this behavior anywhere, this is pseudocode for what Obi does:
Code:
void LateUpdate()
{
// accumulate time passed since last frame
accum += Time.deltaTime;
// while more than 1 timestep has passed, advance a timestep and subtract it from
// the accumulator:
while (accum >= Time.fixedDeltaTime)
{
InternalObiUpdate(Time.fixedDeltaTime);
accum -= Time.fixedDeltaTime;
}
}
If you're curious about what fixed timestepping means and how it's actually implemented in physics engines, I can recommend this timeless (no pun intended) classic:
https://gafferongames.com/post/fix_your_timestep/
(07-05-2024, 09:38 AM)kodra Wrote: If I have game logic in LateUpate() (for example, update the bone's position), does Obi solver updates before or after it?
It entirely depends on your script execution order.
kind regards,