Sign in to follow this  
lride

Am I doing the interpolation correctly?

Recommended Posts

lride    674

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?

 

Share this post


Link to post
Share on other sites
rpiller    839

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