I'm trying to implement network entity interpolation described in this article: http://www.gabrielgambetta.com/fpm3.html
so far I have this and the circle's movement looks jittery. What did I do wrong?
struct Circle
{
sf::Vector2f current;
sf::Vector2f last;
sf::CircleShape shape;
Circle()
{
shape.setRadius(100);
}
void interpolate()
{
sf::Vector2f difference = current - last;
current.x += difference.x * 0.1;
current.y += difference.y * 0.1;
}
void draw(sf::RenderWindow & window)
{
shape.setPosition(current.x, current.y);
window.draw(shape);
}
void setPosition(sf::Vector2f v)
{
last = current;
current = v;
}
};
//server
void server()
{
sf::TcpListener listener;
sf::TcpSocket client;
listener.listen(PORT);
listener.accept(client);
client.setBlocking(false);
Circle circle;
sf::RenderWindow window(sf::VideoMode(800, 600), "server");
sf::Clock clock;
sf::Time elapsed = sf::Time::Zero;
sf::Time timePerFrame = sf::seconds(1 / 20.f);
while (window.isOpen())
{
sf::Time dt = clock.restart();
elapsed += dt;
while (elapsed > timePerFrame)
{
elapsed -= timePerFrame;
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
sf::Packet packet;
sf::Socket::Status s = client.receive(packet);
while (s == sf::Socket::Done)
{
//new position sent from the client;
float x, y;
packet >> x >> y;
circle.setPosition({ x, y });
packet.clear();
s = client.receive(packet);
}
circle.interpolate();
}
window.clear();
circle.draw(window);
window.display();
}
}
what's wrong with my code?