When it comes to calculating movement in a game engine, each frame I update the position of the player, based on his speed. If I try to be realistic in my calculations, I end up with a displacement per frame that is so small that movement overall doesn't occur or, when accelerating, occurs only discontinuously.
Here's an example:
frame rate = 60 fps
window height = 600 px
scale = 10px = 1 m
speed = 2 m/s = 0.03... m/frame (Let's assume the player is free falling and accelerating under gravity of 9.8 m/s)
With this data, the minimum displacement is 1 pixel = 0.1m, so the end result is no movement for a lot of frames. If the speed is constant, no movement occurs at all.
For now, I used a multiplicative factor to scale my calculations up, but is there a better approach to deal with this?
Basically, my question is: how should I handle (small) fractions of movement in a consistent and realistic way?
EDIT: The solution should be to always use float
s(double
s) for every physical variable including object position in space and convert to integer pixel coordinate only as a last step when it's time to render. This way in the physical model everything works, even fine movement.
Mauro
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…