• Advertisement
Sign in to follow this  

[SOLVED] Choppyness even with interpolation

This topic is 2609 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Edit: Nevermind, solved it. In case anyone ever stumbles on the same problem: Apparently GetTickCount() is unreliable, it's best to use QueryPerformanceCounter() or QueryPerformanceFrequency() instead.

Hello again everyone!

I'm here again with a (hopefully) small issue I'm having.

I'm having some choppyness in my game project, using C++ and SFML as library for graphics.
I wasn't sure where the choppyness came from, it could have been the forced vsync by calling UseVerticalSync( true );, or limiting the frames to 60 by calling SetFramerateLimit( 60 );.

I read a lot about game loops lately, about fixing your timestep and interpolation.
I have given it a try several times but kept getting stuck and it not working properly.

However today I thought I'd put together a very bare project displaying nothing but 1 scrolling sprite. Did that, and added a "Constant Game Speed independent of Variable FPS" game loop going at 60 frames per second.

I was surprised to find that, having the exact same choppyness my game project had. So here I am again, hoping that one of the pros/gurus could once again point out what I'm perhaps doing wrong.

This is the source of the small sample I put together that has the choppyness.

int main()
// Create main window
sf::RenderWindow App(sf::VideoMode( 800, 600 ), "Interpolation Test");

const int TICKS_PER_SECOND = 60;
const int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
const int MAX_FRAMESKIP = 5;
DWORD next_game_tick = GetTickCount();
int loops;
float interpolation;

// Create the Image/Sprite
sf::Sprite sprite;
sf::Image spriteImage;
spriteImage.LoadFromFile( "data/images/entity.png" );
sprite.SetImage( spriteImage );

// Set the sprite's variables
float spritevelx = 0;
float spritevely = 0;
float spriteposx = 0;
float spriteposy = 400;
float speed = 2;
spritevelx = speed;

// Start game loop
while (App.IsOpened())
// Get Input
const sf::Input& Input = App.GetInput();
left_pressed = Input.IsKeyDown(sf::Key::Left);
right_pressed = Input.IsKeyDown(sf::Key::Right);
up_pressed = Input.IsKeyDown(sf::Key::Up);
down_pressed = Input.IsKeyDown(sf::Key::Down);
z_pressed = Input.IsKeyDown(sf::Key::Z);
z_released = Input.IsKeyDown(sf::Key::Z);
x_pressed = Input.IsKeyDown(sf::Key::X);

// Process events
sf::Event Event;
while (App.GetEvent(Event))
// Close window : exit
if( Event.Type == sf::Event::Closed ){ App.Close(); }

// The Loop where we do the movement of the sprite (and or other logic/ai/movement/etc)
loops = 0;
while( GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP)
// Determine whether to bounce back or not
if( ( spriteposx + spriteImage.GetWidth() ) >= 800 )
//spritevelx = -speed;
spriteposx = 0;
if( spriteposx <= 0 )
//spritevelx = speed;

// Move the sprite further and set it's position
spriteposx += spritevelx;
sprite.SetPosition( spriteposx, spriteposy );

next_game_tick += SKIP_TICKS;

interpolation = float( GetTickCount() + SKIP_TICKS - next_game_tick ) / float( SKIP_TICKS );

// Draw the sprite (as often as we can, like the guide says?)
sprite.SetPosition( ( spriteposx + ( interpolation * speed ) ), spriteposy );
App.Draw( sprite );

// Show the buffer

// Clear screen


The sprite I used is a simple blue square block included below;

Edit: Changed TICKS_PER_SECOND to 60, it was set to 200 when I was testing something quick, forgot to change it back before pasting it in here.

[Edited by - Auriya on December 31, 2010 12:55:07 AM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement