Quantcast
Channel: GameDev.net
Viewing all articles
Browse latest Browse all 17825

Interpolation - Can I make it better?

$
0
0
Hello everyone, I’m finishing up learning about interpolation and wanted to see if my code is calculating in the best way possible. When I programmed a toggle to turn it off and on, I can visually see smooth motion almost at any frame rate. The only issue is when render frames start to go lower than 30, it’s not as jittery and doesn’t skip with it on, with it off it will jitter, and sometimes warp around if the updates are not called fast enough. (This is normal as logic and input is going at 30 ticks per second, and rendering is happening as fast as possible. I did some extra code to lock the render frames at lower FPS to test it.) The basic loop code for the Time Step (I Took some stuff out just to make the example clearer, so you won’t see my loops code or max frames, ect… as I have code to break out of the loop if it falls behind.): const float ticksPerSecond = 30.f; // Updates per second const float skipTicks = 1000.f / ticksPerSecond; // This would be 33.33333333333333 sf::Clock mainClock; double nextTick = mainClock.restart().asMilliseconds(); while (mainClock.getElapsedTime().asMilliseconds() > nextTick) { // Updates – LOGIC testSpritePosPrev = testSpritePos; if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { testSpritePos.y -= testSpriteSpeed; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { testSpritePos.y += testSpriteSpeed; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { testSpritePos.x -= testSpriteSpeed; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { testSpritePos.x += testSpriteSpeed; } nextTick += skipTicks; } interpolation = float(((mainClock.getElapsedTime().asMilliseconds() + skipTicks) - nextTick)) / (skipTicks); if (toggleInterpolation == true) { testSprite.setPosition(testSpritePosPrev.x + ((testSpritePos.x - testSpritePosPrev.x) * interpolation), testSpritePosPrev.y + ((testSpritePos.y - testSpritePosPrev.y) * interpolation)); } else { testSprite.setPosition(testSpritePos.x, testSpritePos.y); } // Clear Window mainWindow.clear(); // Draw to Window mainWindow.draw(testSprite); // Display mainWindow.display(); Just to clarify. All logic and Input is handled at 30 ticks per second, and the rendering happens as fast as possible with no limitation.

Viewing all articles
Browse latest Browse all 17825

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>