Hello! I am trying to create a 2D racing game, in Blitz 2D. To get the idea of movement, it scrolls tiles across the screen from top to bottom. The problem is the scrolling is not really smooth. I know this is a well known problem, getting the frame rate right and the scrolling smooth without relying on some monitor refresh rate.
In a single-screen game I used the following solution to make sure a character moved say 40 times a second:
I know how long each frame should take: 1/40th of a second. So I measure how long it actually takes, and from these two figures I compute a correction_factor, to get from the actual to the desired duration.
Now, normally the player moves every single frame. So I would (theoretically) count the frames (adding 1 to this framecounter each frame), and the player would move if the count reaches 1 (and the framecounter is reset to 0). To get the correct animation speed however, I do not simply add 1 every frame, but instead I add 1.0 * correction_factor.
Notice that of course I am using floats here as otherwise the small differences would be lost. I also take care not to reset the frames counter to 0, but instead to frames_counter-1.0, so as to keep the remainder.
So I use this method in my racing game.
However... when I link my scroller's location to the location of the player (to get a scrolling screen when the player moves) it can be seen that movement is not really smooth. What happens is that most of the time, the player is "allowed" to move in every single frame, but sometimes, the player is not allowed to move (due to "correction", the player has to skip a turn) and this is seen in the scrolling as a slight delay.
Here's the code that says whether the player may move:
("AItime" counts the frames, "g_fpsc" is an object that computes the corrected value of the value that is passed in the function. So instead of counting 1 frame, the program counts, say, 0.9 frames if the game has to be slowed down.) I also print some debug info to show the effect:
And so on. Writing this all down it seems quite obvious that this would result in jerky scrolling, as whenever it says "wait" in the debuglog, at that moment the car does not move.
Does anyone have ideas or experience in creating smooth scrolling in Blitz 2D, or comments on what I am doing here?
The above method I got through advice in another post if I remember correctly, but it could be that I applied it differently.
A thing that I could try is to use the "correction" method to change the amount by which the car moves (also suggested earlier by others). Right now I use integer amounts, but changing this to floats I could apply the correction factor to the car's speed. Maybe that still results in jerky scrolling, as in the end, movement is by whole pixels, but maybe the problem will be smaller.
In a single-screen game I used the following solution to make sure a character moved say 40 times a second:
I know how long each frame should take: 1/40th of a second. So I measure how long it actually takes, and from these two figures I compute a correction_factor, to get from the actual to the desired duration.
Now, normally the player moves every single frame. So I would (theoretically) count the frames (adding 1 to this framecounter each frame), and the player would move if the count reaches 1 (and the framecounter is reset to 0). To get the correct animation speed however, I do not simply add 1 every frame, but instead I add 1.0 * correction_factor.
Notice that of course I am using floats here as otherwise the small differences would be lost. I also take care not to reset the frames counter to 0, but instead to frames_counter-1.0, so as to keep the remainder.
So I use this method in my racing game.
However... when I link my scroller's location to the location of the player (to get a scrolling screen when the player moves) it can be seen that movement is not really smooth. What happens is that most of the time, the player is "allowed" to move in every single frame, but sometimes, the player is not allowed to move (due to "correction", the player has to skip a turn) and this is seen in the scrolling as a slight delay.
Here's the code that says whether the player may move:
AItime = AItime + correct(g_fpsc,1.0) If (AItime>=1.0) Then player_move() AItime = AItime - 1.0 DebugLog(">>>>>>> ACTION!") Else DebugLog("wait...") End If
("AItime" counts the frames, "g_fpsc" is an object that computes the corrected value of the value that is passed in the function. So instead of counting 1 frame, the program counts, say, 0.9 frames if the game has to be slowed down.) I also print some debug info to show the effect:
>>>>>>> ACTION! >>>>>>> ACTION! >>>>>>> ACTION! >>>>>>> ACTION! >>>>>>> ACTION! >>>>>>> ACTION! >>>>>>> ACTION! >>>>>>> ACTION! >>>>>>> ACTION! >>>>>>> ACTION! >>>>>>> ACTION! >>>>>>> ACTION! wait... >>>>>>> ACTION! >>>>>>> ACTION! >>>>>>> ACTION! >>>>>>> ACTION! >>>>>>> ACTION! >>>>>>> ACTION! >>>>>>> ACTION! >>>>>>> ACTION! >>>>>>> ACTION! >>>>>>> ACTION! >>>>>>> ACTION! >>>>>>> ACTION! wait...
And so on. Writing this all down it seems quite obvious that this would result in jerky scrolling, as whenever it says "wait" in the debuglog, at that moment the car does not move.
Does anyone have ideas or experience in creating smooth scrolling in Blitz 2D, or comments on what I am doing here?
The above method I got through advice in another post if I remember correctly, but it could be that I applied it differently.
A thing that I could try is to use the "correction" method to change the amount by which the car moves (also suggested earlier by others). Right now I use integer amounts, but changing this to floats I could apply the correction factor to the car's speed. Maybe that still results in jerky scrolling, as in the end, movement is by whole pixels, but maybe the problem will be smaller.