• Create Account

# Few questions about fix timestep for physics

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1Sythical  Members   -  Reputation: 144

Like
0Likes
Like

Posted 12 July 2013 - 09:55 PM

Hello, I've been trying to implement the fix time-step technique and I'm having trouble understanding a couple of thing. I've listed the two issues separately so that they make more sense.

i) To interpolate between the old and the new state, I'm using two variables called old_pos_x and new_pos_x. Originally, I would copy new_pos_x to old_pos_x after rendering each time. This makes more sense to me since I want to interpolate between the circle on the screen and the one I'll be drawing next. However, this makes the animation stutter if I don't update the physics at 60 or 120 hertz (multiples of my refresh rate).

When I moved the copy statement inside the physics loop, the stutter goes away and I have no idea why. I would have thought that this will make the game stutter a lot more, especially when the the frame rate is lower than the update rate. Why does this work?

ii) I added Sleep(5) after the render bit in order to decrease the FPS to about 200 so that I can see how it works at low rates. The animation becomes very jumpy and not smooth at all. I understand that Sleep() isn't accurate but surely that shouldn't matter as long as the frame rate is above 60 FPS because of the monitor's refresh rate.

I've written a smaller version of the program which replicates the issue(s) so that it's easier to read. It's just a circle that moves horizontally. The code is still a bit long, sorry about that.

unsigned screen_width = 1200, screen_height = 800;
int old_pos_x = 100, new_pos_x = 100, velocity_x = 0;
LARGE_INTEGER counter, frequency;

LONGLONG
accumulator = 0, accumulator_max, physics_delta,
time_difference, new_time, previous_time;

sf::RenderWindow window(sf::VideoMode(screen_width, screen_height), "Example", sf::Style::Titlebar | sf::Style::Close);

sf::CircleShape graphics(15.f);
graphics.setFillColor(sf::Color(255, 255, 255));

QueryPerformanceFrequency(&frequency);
accumulator_max = physics_delta * 5;

QueryPerformanceCounter(&counter);

while(window.isOpen()) {
QueryPerformanceCounter(&counter);

time_difference = new_time - previous_time;
previous_time = new_time;
accumulator += time_difference;

if(accumulator > accumulator_max) {
accumulator = accumulator_max;
}

while(accumulator >= physics_delta) {
if(new_pos_x > 1000) velocity_x = -8;
else if(new_pos_x < 200) velocity_x = 8;

// if I copy the new state (position) to the old state here instead the animation is much more smooth
// but I don't understand why this is the case, I would have thought that this is more likely to cause stutter
// especially if the frame rate is lower than the game update rate

// old_pos_x = new_pos_x; // copy the current state to the old one
new_pos_x += velocity_x; // update the current state

accumulator -= physics_delta;
}

double interpolation_val = accumulator / (double) physics_delta;
graphics.setPosition(new_pos_x * interpolation_val + old_pos_x * (1 - interpolation_val), 400.f);

window.clear();
window.draw(graphics);
window.display();

// this is where I was originally copying the new state (position) to the old state
// this makes more sense to be because I want to interpolate between the last drawn circle
// and the current circle

old_pos_x = new_pos_x;

// Sleep(5);
}


I'd be very grateful if someone can have a read through the code. Thank you

Edit: I think I've figured out the reason for the first part. The game renders quicker than it updates so if I copy the states after rendering, the position will not be interpolated properly until it updates again. Still not sure about the second part.

Edited by Sythical, 13 July 2013 - 07:46 AM.

### #2Norman Barrows  Crossbones+   -  Reputation: 5878

Like
1Likes
Like

Posted 13 July 2013 - 02:05 PM

i think this may be the basic algo you're looking for:

game loop:

{

startimer(0)

drawscreen()

ET=elapsedtime(0)      // hi-res in ms or perhaps even ticks

update(ET)

}

update(ET):

numsteps=ET/stepsize

for (a=0; a<numsteps; a++)

update_by_one_step()

accumulator = ET-numsteps*stepsize

update_by_one_step():

last_position=current_position

// then calc new current position here

render:

render_position=lerp(from last_position, to current_position, by accumulator fraction)

draw using render_position.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

### #3Sythical  Members   -  Reputation: 144

Like
0Likes
Like

Posted 15 July 2013 - 04:05 AM

I tried my code with Allegro (made a couple of small changes) and it's now very smooth. Here is the code so that others may find it useful, I haven't refactored it:

#include <allegro5\allegro.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_primitives.h>
#include <allegro5\allegro_ttf.h>

#include <iostream>

#include <windows.h>

using namespace std;

int main(int argc, char *argv[]) {

if(!al_init()) return -1;

ALLEGRO_DISPLAY *display = al_create_display(1400, 800);

if(!display) return -1;

ALLEGRO_FONT *font = al_load_font("terminalscope.ttf", 16, 0);

// ------------------------------------------------------------

LARGE_INTEGER
counter, frequency;

double
interpolation_val = 0, counter_old_time = al_get_time();;

QueryPerformanceFrequency(&frequency);

unsigned
screen_width = 1200, screen_height = 800, frame_counter = 0, current_fps = 0;

int
old_pos_x = 100, new_pos_x = 100, velocity_x = 15;

LONGLONG
accumulator = 0, delta_time = 0, previous_time;

const unsigned
physics_rate = 80, max_step_count = 5;

const LONGLONG
accumulator_max = step_duration * max_step_count;

// ------------------------------------------------------------

QueryPerformanceCounter(&counter);

// ------------------------------------------------------------

ALLEGRO_EVENT event;
ALLEGRO_EVENT_QUEUE *event_queue;
ALLEGRO_TIMER *timer;

event_queue = al_create_event_queue();
timer = al_create_timer(1.0 / 60.0);

al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_start_timer(timer);

while(true) {
QueryPerformanceCounter(&counter);

accumulator += delta_time;

// ------------------------------------------------------------

if(accumulator > accumulator_max) {
accumulator = accumulator_max;
}

while(accumulator >= step_duration) {
if(new_pos_x > 1330) velocity_x = -15;
else if(new_pos_x < 70) velocity_x = 15;

old_pos_x = new_pos_x;
new_pos_x += velocity_x;
accumulator -= step_duration;
}

interpolation_val = accumulator / (double) step_duration;
double render_pos_x = old_pos_x + (new_pos_x - old_pos_x) * interpolation_val;

// ------------------------------------------------------------

al_clear_to_color(al_map_rgb(20, 20, 40));
al_draw_textf(font, al_map_rgb(255, 255, 255), 20, 20, 0, "current_fps: %i", current_fps);
al_draw_filled_circle(render_pos_x, 400, 15, al_map_rgb(255, 255, 255));

al_wait_for_event(event_queue, &event);

al_flip_display();

frame_counter++;

if(al_get_time() - counter_old_time >= 1) {
current_fps = frame_counter;
frame_counter = 0;
counter_old_time = al_get_time();
}
}

return 0;
}



Edited by Sythical, 15 July 2013 - 04:06 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS