Racing car game, the car moves faster on a faster computer

I understand why this is so, but I really don't know how to prevent this. Thus, the scenario is that each frame changes the car to certain predetermined pixels. What happens when I go on a slower or faster computer ... well, I get fewer or more frames per second so that the machine moves either slower or faster. I was wondering how I can prevent this.

I suspect I will have the same problem using any library ... This is my first time I do such things in real time.

+5
source share
8 answers

I suspect your current code looks something 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();
 }

Of course, you must configure the appropriate timer event.

+12
source

Move the car according to timers, not frame rate. that is, the car model must be independent of the displayed view.

+10
source

.

, , , . 1 3 0,02 .

40 .

, , , (TIME_NOW - LAST_FRAME_TIME) * 40px.

+3

"" , .. x .

+2

. "", , .

+2

X ( 60 FPS). , SFML. SFML Window/RenderWindow SetFramerateLimit (unsigned int Limit).

+2

You need to fix your timestep . In principle, each frame moves the car a distance that depends on how much time has passed since the last update call. This way you get the right speed regardless of frame rate.

+1
source

You need to save time before moving your car and blueprints.
Save time after all calculations.
Move your car on Npixels / second

0
source

All Articles