1
General / Smooth Movement
« on: November 10, 2014, 02:03:21 am »
I'm trying to make a sprite move at a constant and smooth rate by using a constant speed multiplied by the time elapsed from last frame.
This does not result in smooth movement. Occasionally the dt value will spike from 0.0003 to .0010 or something like that, causing the sprite to jump forward even though the difference in time is negligible.
Is there some way to account for these small variations or am I approaching smooth movement wrong all together?
Thanks.
sf::Clock clk;
clk.restart();
while (window.isOpen())
{
sf::Event event;
float dt;
while (window.pollEvent(event) || true)
{
dt = clk.restart().asSeconds();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
sprite.move(playerSpeed*dt,0);
}
window.clear();
window.draw(sprite);
window.display();
}
}
clk.restart();
while (window.isOpen())
{
sf::Event event;
float dt;
while (window.pollEvent(event) || true)
{
dt = clk.restart().asSeconds();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
sprite.move(playerSpeed*dt,0);
}
window.clear();
window.draw(sprite);
window.display();
}
}
This does not result in smooth movement. Occasionally the dt value will spike from 0.0003 to .0010 or something like that, causing the sprite to jump forward even though the difference in time is negligible.
Is there some way to account for these small variations or am I approaching smooth movement wrong all together?
Thanks.