I have a created a little program in SFML C++ where I want an object to move. But my problem is the movement speed is not constant, sometime speed is dropping randomly and the movement is not smooth at all. Video Link If you take a closer look the speed is not constant My Source code
#include <SFML/Graphics.hpp>
#include <iostream>
// 10^6
int main()
{
float dx = 0.000001;
float dy = 0.03;
int w = 720;
int h = 480;
sf::RenderWindow window(sf::VideoMode(w, h), "Sf Test");
sf::CircleShape r1(20);
r1.setFillColor(sf::Color(250, 0, 0));
sf::Vector2f objPos(0.0, 0.0);
r1.setOrigin(objPos);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
}
if (r1.getPosition().y > h || r1.getPosition().y < 0)
{
dy *= -1;
}
objPos.y += dy;
r1.setPosition(objPos);
// std::cout << r1.getPosition().y << std::endl;
window.clear();
window.draw(r1);
window.display();
}
}
Can anyone tell me how to stop this behavior also how to make the movement speed smooth.