[Add] Smooth correction for server reconciliation.
This commit is contained in:
parent
6aa204672e
commit
8d59e79f8a
@ -9,9 +9,9 @@
|
|||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#include <cstdlib>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <future>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "bettola.h"
|
#include "bettola.h"
|
||||||
@ -32,7 +32,10 @@ Bettola::Bettola(void) :
|
|||||||
_gl_context(nullptr),
|
_gl_context(nullptr),
|
||||||
_vao(0),
|
_vao(0),
|
||||||
_vbo(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));
|
memset(&_server_addr, 0, sizeof(_server_addr));
|
||||||
}
|
}
|
||||||
@ -200,6 +203,20 @@ void Bettola::update(double dt) {
|
|||||||
|
|
||||||
_player.update(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) {
|
for(auto& remote_player : _remote_players) {
|
||||||
remote_player.update(dt);
|
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;
|
float dy = _player.get_y() - player_state.y;
|
||||||
if((dx*dx+dy*dy) > 0.0001f) { /* If distance is not negligibale. */
|
if((dx*dx+dy*dy) > 0.0001f) { /* If distance is not negligibale. */
|
||||||
/* Simple correction, snap to server position. */
|
/* 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 {
|
} else {
|
||||||
/* Remote player. Find if we already know about them.. */
|
/* Remote player. Find if we already know about them.. */
|
||||||
|
|||||||
@ -60,4 +60,8 @@ private:
|
|||||||
BettolaLib::Network::TCPSocket _tcp_socket;
|
BettolaLib::Network::TCPSocket _tcp_socket;
|
||||||
BettolaLib::Network::UDPSocket _udp_socket;
|
BettolaLib::Network::UDPSocket _udp_socket;
|
||||||
sockaddr_in _server_addr;
|
sockaddr_in _server_addr;
|
||||||
|
|
||||||
|
bool _needs_correction;
|
||||||
|
float _correction_target_x;
|
||||||
|
float _correction_target_y;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user