Hi! (and thank u for reading this
)
Now that I've implemented interpolation and I have a very smooth movement, I encountered a problem while moving my rectangle.
If I move it slowly, it goes perfectly; but when I move it fast, there's like a motion blur effect that makes it strange.
I'm using Windows 10 with an Nvidia GPU (GeForce RTX 2060) and my program uses SFML 2.6.1.
Minimal Example of code:
#include <SFML/Graphics.hpp>
#include <Kairos/Timestep.hpp>
sf::Vector2f lerpV(sf::Vector2f a, sf::Vector2f b, float t)
{
return a * (1 - t) + b * t;
}
void Update(sf::Shape* s, float dt, sf::Vector2f* vel, sf::Vector2f* pos)
{
float mass = 1.f;
float f = 100.f;
sf::Vector2f force = sf::Vector2f(0.f, 0.f);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
force += sf::Vector2f(0.f, -f);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
force += sf::Vector2f(-f, 0.f);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
force += sf::Vector2f(0.f, f);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
force += sf::Vector2f(f, 0.f);
// Inertia:
force += sf::Vector2f(-vel->x * 1.f * mass * 9.81f, -vel->y * 1.f * mass * 9.81f);
float acceleration_x = force.x / mass;
float acceleration_y = force.y / mass;
*vel += sf::Vector2f(acceleration_x * dt, acceleration_y * dt);
*pos += *vel;
}
int main()
{
sf::RenderWindow window(sf::VideoMode(1200, 800), "Motion Blur Problem");
window.setVerticalSyncEnabled(false);
window.setFramerateLimit(144);
sf::RectangleShape shape(sf::Vector2f(200.f, 200.f));
shape.setFillColor(sf::Color::Green);
sf::Vector2f velocity = sf::Vector2f(0.f, 0.f);
sf::Vector2f pre = shape.getPosition();
sf::Vector2f position = shape.getPosition();
kairos::Timestep timestep;
timestep.setStep(1.f / 60.f);
timestep.setMaxAccumulation(0.25f);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
timestep.addFrame();
while (timestep.isUpdateRequired())
{
pre = position;
float dt = timestep.getStepAsFloat();
Update(&shape, dt, &velocity, &position);
}
float interpolation = timestep.getInterpolationAlphaAsFloat();
window.clear();
shape.setPosition(lerpV(pre, position, interpolation));
window.draw(shape);
window.display();
}
return 0;
}
My main questions are:
1. is this an optical illusion made by its movement? (so I should only move it slowly)
2. is this a problem of my monitor or my eyes? (so I should cry)
3. or is this a bug? and if yes, how can I fix it?