diff --git a/src/bettola.cpp b/src/bettola.cpp index c4b1930..d9f44fd 100644 --- a/src/bettola.cpp +++ b/src/bettola.cpp @@ -9,9 +9,9 @@ #include #include #include +#include #include #include -#include #include #include "bettola.h" @@ -32,7 +32,10 @@ Bettola::Bettola(void) : _gl_context(nullptr), _vao(0), _vbo(0), - _our_player_id(0) { + _our_player_id(0), + _needs_correction(false), + _correction_target_x(0.0f), + _correction_target_y(0.0f) { memset(&_server_addr, 0, sizeof(_server_addr)); } @@ -200,6 +203,20 @@ void Bettola::update(double dt) { _player.update(dt); + if(_needs_correction) { + const float interp_speed = 20.0f; /* We'll use a slightly higher speed for self-correction. */ + float current_x = _player.get_x(); + float current_y = _player.get_y(); + _player.set_position(current_x + (_correction_target_x - current_x) * interp_speed * dt, + current_y + (_correction_target_y - current_y) * interp_speed * dt); + + /* If we are very close.. Stop correcting. */ + if(std::abs(current_x - _correction_target_x) < 0.1f && + std::abs(current_y - _correction_target_y) < 0.1f) { + _needs_correction = false; + } + } + for(auto& remote_player : _remote_players) { remote_player.update(dt); } @@ -297,7 +314,11 @@ void Bettola::process_game_state(const BettolaLib::Network::GameStateMessage& ms float dy = _player.get_y() - player_state.y; if((dx*dx+dy*dy) > 0.0001f) { /* If distance is not negligibale. */ /* Simple correction, snap to server position. */ - _player.set_position(player_state.x, player_state.y); + //_player.set_position(player_state.x, player_state.y); + + _needs_correction = true; + _correction_target_x = player_state.x; + _correction_target_y = player_state.y; } } else { /* Remote player. Find if we already know about them.. */ diff --git a/src/bettola.h b/src/bettola.h index f97c869..0339382 100644 --- a/src/bettola.h +++ b/src/bettola.h @@ -60,4 +60,8 @@ private: BettolaLib::Network::TCPSocket _tcp_socket; BettolaLib::Network::UDPSocket _udp_socket; sockaddr_in _server_addr; + + bool _needs_correction; + float _correction_target_x; + float _correction_target_y; };