Sign in to follow this  

Am I doing the interpolation correctly?

Recommended Posts

I'm trying to implement network entity interpolation described in this article:


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;
	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);
	void setPosition(sf::Vector2f v)
		last = current;
		current = v;

void server()
	sf::TcpListener listener;
	sf::TcpSocket client;


	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)

			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 });
				s = client.receive(packet);


what's wrong with my code?


Share this post

Link to post
Share on other sites

Are you interpolating on the server? I would think you really only need to interpolate on the clients. Looks like you have the clients having authority over their positions. So the server gets their positions and just snaps to that position. I don't see anything wrong with snapping on the server side. It's when the server sends it's snapshots to the clients, the clients will want to interpolate between the last snapshot's so the movement seems visually smooth to the client. The server doesn't really care if the movement is smooth or not.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this