Hello everyone at gamedev.net,
for a 2d game, I want the user's to be able to change the framerate in their settings (framerate won't change during gameplay tough). However I'm having trouble getting the physics consistent at different framerates. Here is what my main loop looks like:
update()
draw()
remaining = dpf - elapsed
if (remaining > 0)
Sleep(remaining)
The value called "dpf" is the game time that should elapse in one frame. So for 60 fps, it is 1000 / 60 = 16.
Now I drew this out on paper and this is what I think should do the job for physics:
dpfmult = dpf / 16
hacc = hforce / mass
vacc = vforce / mass
hspeed = hforce * dpfmult
vspeed = vforce * dpfmult
xpos += siqnsqrt(hspeed * dpfmult)
ypos += siqnsqrt(vspeed * dpfmult)
The reasoning being that I apply the multiplier twice, hence I take the (signed) square root at the end. Yet this gives me completely different behaviour for 120, 60 or 30 fps.
I also have animations in my game dependent on the dpf valule and those look the same at every fps. So If anyone could take a look at it and check what's wrong that would be great. Thanks!