So, I have been working on a project for a while now and recently stumbled across a problem:
The common approach to frame-rate independent physics is to either use a fixed update interval (ie. Unity) or just multiply each physical change with delta. So it would look something like this:
update(int delta)
{
...
positionX += velocityX * delta;
...
}
And when acceleration is added, the velocity change is treated similar:
update(int delta)
{
...
velocityX += accelerationX * delta;
...
}
This works fine for most use cases. In fact so fine, that I haven't noticed anything was off for months. But there is a problem with this logic, namely that the position will be slightly off for entirely different frame-rates. This problem is negligible when working with small frame-rate changes, but is entirely noticeable once the delta time is changing by bigger amounts. Say, FPS rate from 30 to 600. Now, in a normal game the FPS are just locked and even if the positions are off for a few frames by like 5% it doesn't matter because one doesn't notice it. But I manually multiply the delta with a factor to accomplish a certain effect, and the error is somewhat noticeable and game breaking.
A small example to clarify what I mean:
There is a game object with start position posX = 0 and a x-Velocity of 10 and an horizontal acceleration of 0.1.
After two frames, for 50FPS this would lead to (with an average delta)
velocityX += acceleration * delta;
posX += velocityX * delta;
velocityX += 0.1 * 20; //velocityX = 12
posX += 12 * 20 //posX = 240
//next frame
velocityX += 0.1 * 20; //velocityX = 14
posX += 14 * 20 //posX = 520 (240 + 280)
After one frame with 25FPS, which equals the same time interval like two frames with 50FPS, this would get us:
velocityX += 0.1 * 40; //velocityX = 14
posX += 14 * 40; //posX = 560
So two situations with the same time interval elapsed we get different results (520 for 50FPS and 560 for 25FPS).
Now, the problem obviously is that there is one extra frame (in this example, it could be any number of frames but the results still differ for the same interval) in which the acceleration is applied and therefore you get further with less FPS.
Stabilizing the delta is not an option as I need different deltas for said effect, so I need to physics to be entirely independent there. Has anyone ever come across a similar situation and are there any solutions for it?