Skip to content

Commit

Permalink
Reduce physics update jitter
Browse files Browse the repository at this point in the history
Added Main::target_iters, modifying physics iteration exit condition
and target_iters so that the number of physics frame per render frame
tries to be between target_iters and target_iters+1. Reduces jitter
when the physics framerate almost, but not quite, matches the render
framerate or an integer multiple thereof.
  • Loading branch information
zmanuel committed Jan 2, 2018
1 parent 14772d2 commit 90a11ef
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
9 changes: 8 additions & 1 deletion main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,7 @@ float Main::time_accum = 0;
uint32_t Main::frames = 0;
uint32_t Main::frame = 0;
bool Main::force_redraw_requested = false;
int Main::target_iters = 0;

//for performance metrics
static uint64_t physics_process_max = 0;
Expand Down Expand Up @@ -1692,7 +1693,7 @@ bool Main::iteration() {

Engine::get_singleton()->_in_physics = true;

while (time_accum > frame_slice) {
while (time_accum + (target_iters - iters) * frame_slice * .5 > frame_slice) {

uint64_t physics_begin = OS::get_singleton()->get_ticks_usec();

Expand Down Expand Up @@ -1723,6 +1724,12 @@ bool Main::iteration() {
Engine::get_singleton()->_physics_frames++;
}

// update target_iters so that iters fluctuates between target_iters and target_iters+1
if(iters <= target_iters)
target_iters = iters;
else
target_iters = iters-1;

Engine::get_singleton()->_in_physics = false;

uint64_t idle_begin = OS::get_singleton()->get_ticks_usec();
Expand Down
1 change: 1 addition & 0 deletions main/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Main {
static uint32_t frames;
static uint32_t frame;
static bool force_redraw_requested;
static int target_iters;

public:
static Error setup(const char *execpath, int argc, char *argv[], bool p_second_phase = true);
Expand Down

0 comments on commit 90a11ef

Please sign in to comment.