Advertisement

Am I doing the interpolation correctly?

Started by December 29, 2013 05:31 AM
1 comment, last by rpiller 10 years, 10 months ago

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?

An invisible text.
It's unlikely someone will debug your code for you for free, in a web browser.

You might want to look at a second implementation of interpolation/extrapolation here: http://www.mindcontrol.org/~hplus/epic/
enum Bool { True, False, FileNotFound };
Advertisement

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.

This topic is closed to new replies.

Advertisement