Frame Rate Help...

Started by
1 comment, last by RegularKid 22 years, 3 months ago
Ok, here''s my problem: I''ve seen all these articles on frame rate independent movement that take a velocity and adjust it based on the frame rate. For example, if my target speed is 50fps and the target velocity is 2, then on a machine that is running at 100fps the velocity would be 1 and on a machine that is running at 25fps the velocity would be 4. Get it? OK, thats all perfect for constant velocities, but what about velocities that aren''t constant. For example, let''s say that I have a mario-style side-scroller and when my guy jumps, his vertical velocity starts at -4 and I increment that every frame so that it goes -4, -3, -2, -1, 0, 1, 2, 3, 4 and then he''s back on the ground again. But if the frame rate drops to half the target speed then the velocities look like this: -8, -6, -4, -2, 0, 2, 4, 6, 8. and if the frame rate is double the target speed then the velocity of the guy looks like this: -2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2. See my problem? Any time that the velocity is not constant, things will get messed up. I will appreciate any help on this. Thanks!
Advertisement
When you switch to frame rate independent movement, you can''t change variables by a fixed amount each frame. Everything has to be relative to the same real time clock. Instead of modifying the velocity based on how many frames have passed, you instead want to modify it based on how many seconds have passed.

In your case, you have a jumping character. When he''s in the air, gravity pulls down on him reducing his vertical velocity by 9.8 m/s for each second that passes. If you do that in your game, it wont matter what the frame rate is, the jump will still be right.
What Krunk said.

Say your sprite''s velocity is 30 pixels/sec in the positive X direction. The amount that you move your
sprite in that direction depends on the elapsed time since the last frame was rendered. If a tenth of
a second has elapsed (slow machine), you move your sprite 3 pixels. On a faster machine, say 1/30th
of a second has elapsed, you move your sprite 1 pixel. The object moves at the same rate regardless
of how many fps are being rendered.

Your object''s x,y coords need to be floats to be able to do this, and you''ll need to use the same type of
system to advance your animation frames. I picked examples above where the delta ended up an integer,
but you will be adjusting your positions by fractions of a world and/or screen coordinate.

This topic is closed to new replies.

Advertisement