English 中文(简体)
Race car game, car moving faster on faster computer
原标题:

I understand why it does that but I don t really have any idea of how to prevent that. So the scenario is, every frame I move the car by a certain of predefined pixels. What happens is when I go on slow or faster computer... well I get less or more frames per seconds so the car moves either slower or faster. I was wondering how I could prevent that.

I suspect that I would have the same problem using any library... This is my first time doing real time stuff like that.

最佳回答

I suspect your current code looks somehow like this

 // to be called every frame
 void OnEveryFrame()
 {
     MoveCar();
     DrawCarToScreen();
 }

But it should be like this:

 // to be called - for example - 50 times per second:
 void OnEveryTimerEvent()
 {
     MoveCar();
 }

 // to be called every frame
 void OnEveryFrame()
 {
     LockTimerEvents();
     DrawCarToScreen();
     UnlockAndProcessOutstandingTimerEvents();
 }

You have to set up an according timer event, of course.

问题回答

Move car according to timers and not framerate. i.e. Car model should be independent of the display representation.

You can solve this by using precise timers and vector maths.

So, for the sake of argument lets suggest your drawing code could be called at any point in time, e.g. 1 second apart or 3 second apart or 0.02 seconds apart.

Take the car movement speed to be 40 pixels a second.

Hence, the number of pixels it should move is (TIME_NOW - LAST_FRAME_TIME) * 40px.

The movement should be constrained by a "real" time delay, i.e. you car will move at the speed of x pixel per time slice.

Read the real-time clock, and move the car an appropriate distance for the elapsed time. This may look somewhat "jerky" if the computer is too slow, but makes the car speed independent of the CPU speed.

You need to cap the framerate to X frames per second (60 FPS is the most common). This is a common feature in most multimedia frameworks, including SFML. For SFML I d look into the Window/RenderWindow method SetFramerateLimit(unsigned int Limit).

You need to fix your timestep. Basically, every frame you move the car a distance that varies based on how much time has actually elapsed since the last update call. This way, you get the correct speed regardless of framerate.

You have to save the time before moving car and drawings.
Save the time after all calculations.
Move your car by Npixels/second





相关问题
Race car game, car moving faster on faster computer

I understand why it does that but I don t really have any idea of how to prevent that. So the scenario is, every frame I move the car by a certain of predefined pixels. What happens is when I go on ...

FPS how calculate this?

In my OpenGL book, it says this: "What often happens on such a system is that the frame is too complicated to draw in 1/60 second, so each frame is displayed more than once. If, for example, it ...

Counting digits in a float

I am following some beginner tutorials for OpenGL in c++ but as I started off as a c# programmer it has made me take a lot of things for granted. So my problem occurred when I was debug printing my ...

Help with the ffmpeg

I can read now the last line from the FFmpeg procees executed in a cmd window. with this source using Scripting host model object reference. Private Sub Command1_Click() Dim oExec As ...

Understand Flex Application and Frames

Driven on by curiosity, I’m trying to understand the Life Cycle used by Flex Application. So, I’ve done a little research on this argument; the two key concepts used into the FlashPlayer are: SWF ...

表3

我正在利用开放式GES1.1为Pi and iPod Touch开发2D游戏。 在Peter、Peit 3G和所有iPod Touch模式上,一切都做工。 这些游戏通常使大约60个科索沃警察部队的景象。

热门标签