[Fix] Fixed timestep physics.
This commit is contained in:
parent
768401686c
commit
1a56533d3e
@ -46,7 +46,7 @@ public:
|
|||||||
static double GetGameTime(void) { return gameTime; }
|
static double GetGameTime(void) { return gameTime; }
|
||||||
static void SetTimeAccel(float s);
|
static void SetTimeAccel(float s);
|
||||||
static float GetTimeAccel(void) { return timeAccel; }
|
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 GetScrWidth(void) { return scrWidth; }
|
||||||
static int GetScrHeight(void) { return scrHeight; }
|
static int GetScrHeight(void) { return scrHeight; }
|
||||||
static int GetScrAspect(void) { return scrAspect; }
|
static int GetScrAspect(void) { return scrAspect; }
|
||||||
|
13
src/main.cpp
13
src/main.cpp
@ -275,8 +275,10 @@ void L3D::MainLoop(void) {
|
|||||||
|
|
||||||
Uint32 last_stats = SDL_GetTicks();
|
Uint32 last_stats = SDL_GetTicks();
|
||||||
int frame_stat = 0;
|
int frame_stat = 0;
|
||||||
|
int phys_stat = 0;
|
||||||
char fps_readout[32];
|
char fps_readout[32];
|
||||||
Uint32 time_before_frame = SDL_GetTicks();
|
Uint32 time_before_frame = SDL_GetTicks();
|
||||||
|
Uint32 last_phys_update = time_before_frame;
|
||||||
|
|
||||||
for(;;) {
|
for(;;) {
|
||||||
frame_stat++;
|
frame_stat++;
|
||||||
@ -315,19 +317,22 @@ void L3D::MainLoop(void) {
|
|||||||
//if(glGetError()) printf("GL: %s\n", gluErrorString(glGetError()));
|
//if(glGetError()) printf("GL: %s\n", gluErrorString(glGetError()));
|
||||||
|
|
||||||
L3D::frameTime = 0.001*(SDL_GetTicks() - time_before_frame);
|
L3D::frameTime = 0.001*(SDL_GetTicks() - time_before_frame);
|
||||||
float step = L3D::timeAccel * L3D::frameTime;
|
|
||||||
|
|
||||||
time_before_frame = SDL_GetTicks();
|
time_before_frame = SDL_GetTicks();
|
||||||
/* Game state update stuff. */
|
/* Fixed 62.5hz physics. */
|
||||||
if(step) {
|
while(time_before_frame - last_phys_update > 16) {
|
||||||
|
last_phys_update += 16;
|
||||||
|
const float step = L3D::GetTimeStep();
|
||||||
Space::TimeStep(step);
|
Space::TimeStep(step);
|
||||||
gameTime += step;
|
gameTime += step;
|
||||||
|
phys_stat++;
|
||||||
}
|
}
|
||||||
currentView->Update();
|
currentView->Update();
|
||||||
|
|
||||||
if(SDL_GetTicks() - last_stats > 1000) {
|
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;
|
frame_stat = 0;
|
||||||
|
phys_stat = 0;
|
||||||
last_stats += 1000;
|
last_stats += 1000;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user