[Fix] Fixed timestep physics.

This commit is contained in:
Allanis 2018-01-23 19:32:27 +00:00
parent 768401686c
commit 1a56533d3e
2 changed files with 10 additions and 5 deletions

View File

@ -46,7 +46,7 @@ public:
static double GetGameTime(void) { return gameTime; }
static void SetTimeAccel(float s);
static float GetTimeAccel(void) { return timeAccel; }
static float GetTimeStep(void) { return timeAccel*frameTime; }
static float GetTimeStep(void) { return timeAccel*(1.0/62.5); }
static int GetScrWidth(void) { return scrWidth; }
static int GetScrHeight(void) { return scrHeight; }
static int GetScrAspect(void) { return scrAspect; }

View File

@ -275,8 +275,10 @@ void L3D::MainLoop(void) {
Uint32 last_stats = SDL_GetTicks();
int frame_stat = 0;
int phys_stat = 0;
char fps_readout[32];
Uint32 time_before_frame = SDL_GetTicks();
Uint32 last_phys_update = time_before_frame;
for(;;) {
frame_stat++;
@ -315,19 +317,22 @@ void L3D::MainLoop(void) {
//if(glGetError()) printf("GL: %s\n", gluErrorString(glGetError()));
L3D::frameTime = 0.001*(SDL_GetTicks() - time_before_frame);
float step = L3D::timeAccel * L3D::frameTime;
time_before_frame = SDL_GetTicks();
/* Game state update stuff. */
if(step) {
/* Fixed 62.5hz physics. */
while(time_before_frame - last_phys_update > 16) {
last_phys_update += 16;
const float step = L3D::GetTimeStep();
Space::TimeStep(step);
gameTime += step;
phys_stat++;
}
currentView->Update();
if(SDL_GetTicks() - last_stats > 1000) {
snprintf(fps_readout, sizeof(fps_readout), "%d fps", frame_stat);
snprintf(fps_readout, sizeof(fps_readout), "%d fps, %d phys updates", frame_stat, phys_stat);
frame_stat = 0;
phys_stat = 0;
last_stats += 1000;
}
}