From 64385ba15c0ae75013a3c95bd6a378a1898e4feb Mon Sep 17 00:00:00 2001 From: Vladislav Khorev Date: Sat, 17 Jan 2026 19:16:06 +0300 Subject: [PATCH] Fixing bug, implementing delays, remove WORLD_UPDATE --- server/server.cpp | 136 ++++++++++++++++++++++------ src/Game.cpp | 104 ++++++++++++++++++++-- src/Game.h | 4 +- src/network/ClientState.h | 60 ++----------- src/network/NetworkInterface.h | 63 ++++++++++++- src/network/WebSocketClient.cpp | 153 +++++++++++++++++++++++++------- src/network/WebSocketClient.h | 14 +-- 7 files changed, 398 insertions(+), 136 deletions(-) diff --git a/server/server.cpp b/server/server.cpp index b15f016..c3f025e 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #define _USE_MATH_DEFINES #include @@ -37,7 +38,10 @@ class Session : public std::enable_shared_from_this { websocket::stream ws_; beast::flat_buffer buffer_; int id_; - ClientState state_; + //ClientState state_; + std::vector timedClientStates; + + void process_message(const std::string& msg) { auto now_server = std::chrono::system_clock::now(); @@ -51,8 +55,16 @@ class Session : public std::enable_shared_from_this { now_server.time_since_epoch() ).count(); + //Apply server delay: + now_ms -= SERVER_DELAY; + uint64_t clientTimestamp = std::stoull(parts[1]); + ClientState receivedState; + + receivedState.id = id_; + + /* // " " long long deltaMs = 0.0f; if (state_.lastUpdateServerTime.time_since_epoch().count() > 0) { @@ -60,34 +72,42 @@ class Session : public std::enable_shared_from_this { } - if (deltaMs > 0) state_.simulate_physics(deltaMs); + if (deltaMs > 0) state_.simulate_physics(deltaMs);*/ std::chrono::system_clock::time_point uptime_timepoint{ std::chrono::duration_cast(std::chrono::milliseconds(clientTimestamp)) }; - state_.lastUpdateServerTime = uptime_timepoint; + receivedState.lastUpdateServerTime = uptime_timepoint; // if (parts[0] == "ROT") { - state_.discreteAngle = std::stoi(parts[2]); - state_.discreteMag = std::stof(parts[3]); - state_.handle_full_sync(parts, 4); - std::cout << "ROT id = " << this->id_ << " discreteMag=" << state_.discreteMag << std::endl; - state_.apply_lag_compensation(now_server); - state_.lastUpdateServerTime = now_server; + receivedState.discreteAngle = std::stoi(parts[2]); + receivedState.discreteMag = std::stof(parts[3]); + receivedState.handle_full_sync(parts, 4); + //std::cout << "ROT id = " << this->id_ << " discreteMag=" << state_.discreteMag << std::endl; + //receivedState.apply_lag_compensation(now_server); + //receivedState.lastUpdateServerTime = now_server; retranslateMessage(msg); } else if (parts[0] == "VEL") { - state_.selectedVelocity = std::stoi(parts[2]); - state_.handle_full_sync(parts, 3); - state_.apply_lag_compensation(now_server); - state_.lastUpdateServerTime = now_server; + receivedState.selectedVelocity = std::stoi(parts[2]); + receivedState.handle_full_sync(parts, 3); + //receivedState.apply_lag_compensation(now_server); + //receivedState.lastUpdateServerTime = now_server; retranslateMessage(msg); } else if (parts[0] == "PING") { - state_.handle_full_sync(parts, 2); - std::cout << "PING id = " << this->id_ <<" discreteMag=" << state_.discreteMag << std::endl; - state_.apply_lag_compensation(now_server); - state_.lastUpdateServerTime = now_server; + receivedState.handle_full_sync(parts, 2); + retranslateMessage(msg); + //receivedState.apply_lag_compensation(now_server); + //receivedState.lastUpdateServerTime = now_server; + } + timedClientStates.push_back(receivedState); + + auto cutoff_time = now_server - std::chrono::milliseconds(CUTOFF_TIME); + + while (timedClientStates.size() > 0 && timedClientStates[0].lastUpdateServerTime < cutoff_time) + { + timedClientStates.erase(timedClientStates.begin()); } } @@ -111,12 +131,12 @@ public: void init() { - state_.lastUpdateServerTime = std::chrono::system_clock::now(); + //state_.lastUpdateServerTime = std::chrono::system_clock::now(); } - + /* std::string get_state_string() { return state_.get_state_string(id_); - } + }*/ void run() { @@ -134,9 +154,63 @@ public: }); } - void tick_physics_global(std::chrono::system_clock::time_point now) { - long long deltaMs = 0; + bool canFetchClientStateAtTime(std::chrono::system_clock::time_point targetTime) + { + if (timedClientStates.empty()) + { + return false; + } + if (timedClientStates[0].lastUpdateServerTime > targetTime) + { + return false; + } + return true; + } + + ClientState fetchClientStateAtTime(std::chrono::system_clock::time_point targetTime) { + + ClientState closestState; + + if (timedClientStates.empty()) + { + throw std::runtime_error("No timed client states available"); + return closestState; + } + if (timedClientStates[0].lastUpdateServerTime > targetTime) + { + throw std::runtime_error("Found time but it is in future"); + return closestState; + } + if (timedClientStates.size() == 1) + { + closestState = timedClientStates[0]; + closestState.apply_lag_compensation(targetTime); + return closestState; + } + + + for (size_t i = 0; i < timedClientStates.size() - 1; ++i) + { + const auto& earlierState = timedClientStates[i]; + const auto& laterState = timedClientStates[i + 1]; + if (earlierState.lastUpdateServerTime <= targetTime && laterState.lastUpdateServerTime >= targetTime) + { + closestState = earlierState; + closestState.apply_lag_compensation(targetTime); + return closestState; + } + } + + closestState = timedClientStates[timedClientStates.size() - 1]; + closestState.apply_lag_compensation(targetTime); + return closestState; + } + + void tick_physics_global(std::chrono::system_clock::time_point now) { + /*long long deltaMs = 0; + + // , if (state_.lastUpdateServerTime.time_since_epoch().count() > 0) { deltaMs = std::chrono::duration_cast( @@ -147,7 +221,7 @@ public: if (deltaMs > 0) { state_.simulate_physics(deltaMs); state_.lastUpdateServerTime = now; // - } + }*/ } void send_message(std::string msg) { @@ -184,7 +258,10 @@ private: void update_world(net::steady_timer& timer, net::io_context& ioc) { auto now = std::chrono::system_clock::now(); - { + //Apply server delay + now -= std::chrono::milliseconds(SERVER_DELAY); + + /* { std::lock_guard lock(g_sessions_mutex); // 1. @@ -197,7 +274,7 @@ void update_world(net::steady_timer& timer, net::io_context& ioc) { last_broadcast = now; auto now_ms = std::chrono::duration_cast( - std::chrono::system_clock::now().time_since_epoch() + now.time_since_epoch() ).count(); // @@ -205,8 +282,11 @@ void update_world(net::steady_timer& timer, net::io_context& ioc) { snapshot += std::to_string(now_ms) + "|"; snapshot += std::to_string(g_sessions.size()) + "|"; for (size_t i = 0; i < g_sessions.size(); ++i) { - snapshot += g_sessions[i]->get_state_string(); - if (i < g_sessions.size() - 1) snapshot += ";"; // + if (g_sessions[i]->canFetchClientStateAtTime(now)) + { + snapshot += g_sessions[i]->fetchClientStateAtTime(now).get_state_string(); + if (i < g_sessions.size() - 1) snapshot += ";"; // + } } // @@ -215,7 +295,7 @@ void update_world(net::steady_timer& timer, net::io_context& ioc) { } } - } + }*/ // : (50), (1000 ) timer.expires_after(std::chrono::milliseconds(50)); diff --git a/src/Game.cpp b/src/Game.cpp index 30628ef..7d56b67 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -240,13 +240,14 @@ namespace ZL uiManager.setSliderCallback("velocitySlider", [this](const std::string& name, float value) { int newVel = roundf(value * 10); if (newVel != Environment::shipSelectedVelocity) { + velocityChanged = true; Environment::shipSelectedVelocity = newVel; - auto now_ms = std::chrono::duration_cast( + /*auto now_ms = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch() ).count(); std::string msg = "VEL:" + std::to_string(now_ms) + ":" + std::to_string(Environment::shipSelectedVelocity); msg = msg + ":" + formPingMessageContent(); - networkClient->Send(msg); + networkClient->Send(msg);*/ } }); @@ -577,17 +578,34 @@ namespace ZL // Биндим текстуру корабля один раз для всех удаленных игроков (оптимизация батчинга) glBindTexture(GL_TEXTURE_2D, spaceshipTexture->getTexID()); + auto now = std::chrono::system_clock::now(); + + //Apply server delay: + now -= std::chrono::milliseconds(CLIENT_DELAY); + + latestRemotePlayers = networkClient->getRemotePlayers(); + // Итерируемся по актуальным данным из extrapolateRemotePlayers - for (auto const& [id, playerState] : latestRemotePlayers) { + for (auto const& [id, remotePlayer] : latestRemotePlayers) { //if (id == networkClient->GetClientId()) continue; // Не рисуем себя через этот цикл + if (!remotePlayer.canFetchClientStateAtTime(now)) + { + continue; + } + + ClientState playerState = remotePlayer.fetchClientStateAtTime(now); + + renderer.PushMatrix(); renderer.LoadIdentity(); renderer.TranslateMatrix({ 0,0, -1.0f * Environment::zoom }); + renderer.TranslateMatrix({ 0, -6.f, 0 }); //Ship camera offset renderer.RotateMatrix(Environment::inverseShipMatrix); renderer.TranslateMatrix(-Environment::shipPosition); + Eigen::Vector3f relativePos = playerState.position;// -Environment::shipPosition; renderer.TranslateMatrix(relativePos); @@ -705,7 +723,25 @@ namespace ZL } } - static const float ANGULAR_ACCEL = 0.005f; + if (velocityChanged) + { + velocityChanged = false; + //if (newVel != Environment::shipSelectedVelocity) { + // velocityChanged = true; + //Environment::shipSelectedVelocity = newVel; + auto now_ms = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch() + ).count(); + std::string msg = "VEL:" + std::to_string(now_ms) + ":" + std::to_string(Environment::shipSelectedVelocity); + msg = msg + ":" + formPingMessageContent(); + networkClient->Send(msg); + //} + } + + + + + //static const float ANGULAR_ACCEL = 0.005f; if (Environment::tapDownHold) { float diffx = Environment::tapDownCurrentPos(0) - Environment::tapDownStartPos(0); @@ -725,13 +761,17 @@ namespace ZL int discreteAngle = static_cast(radians * 180.0f / M_PI); if (discreteAngle < 0) discreteAngle += 360; + bool sendRotation = false; + std::cout << "OUTPUT discreteAngle=" << discreteAngle << std::endl; // 3. Проверяем, изменились ли параметры значимо для отправки на сервер if (discreteAngle != Environment::lastSentAngle || discreteMag != Environment::lastSentMagnitude) { Environment::lastSentAngle = discreteAngle; Environment::lastSentMagnitude = discreteMag; - auto now_ms = std::chrono::duration_cast( + + sendRotation = true; + /*auto now_ms = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch() ).count(); @@ -740,6 +780,7 @@ namespace ZL std::string msg = "ROT:" + std::to_string(now_ms) + ":" + std::to_string(discreteAngle) + ":" + std::to_string(discreteMag); msg = msg + ":" + formPingMessageContent(); networkClient->Send(msg); + std::cout << "Sending: " << msg << std::endl;*/ } // 4. Логика вращения (угловое ускорение) @@ -786,6 +827,20 @@ namespace ZL Environment::shipMatrix = Environment::shipMatrix * rotateQuat.toRotationMatrix(); Environment::inverseShipMatrix = Environment::shipMatrix.inverse(); } + + if (sendRotation) + { + auto now_ms = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch() + ).count(); + + // Формируем сетевой пакет + // Нам нужно отправить: дискретный угол, дискретную силу и текущую матрицу/позицию для синхронизации + std::string msg = "ROT:" + std::to_string(now_ms) + ":" + std::to_string(discreteAngle) + ":" + std::to_string(discreteMag); + msg = msg + ":" + formPingMessageContent(); + networkClient->Send(msg); + std::cout << "Sending: " << msg << std::endl; + } } } else { @@ -793,9 +848,14 @@ namespace ZL int discreteAngle = -1; float discreteMag = 0.0f; + bool sendRotation = false; + if (discreteAngle != Environment::lastSentAngle || discreteMag != Environment::lastSentMagnitude) { Environment::lastSentAngle = discreteAngle; Environment::lastSentMagnitude = discreteMag; + + sendRotation = true; + /* auto now_ms = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch() ).count(); @@ -805,6 +865,7 @@ namespace ZL std::string msg = "ROT:" + std::to_string(now_ms) + ":" + std::to_string(discreteAngle) + ":" + std::to_string(discreteMag); msg = msg + ":" + formPingMessageContent(); networkClient->Send(msg); + std::cout << "Sending: " << msg << std::endl;*/ } @@ -829,6 +890,20 @@ namespace ZL Environment::shipMatrix = Environment::shipMatrix * rotateQuat.toRotationMatrix(); Environment::inverseShipMatrix = Environment::shipMatrix.inverse(); } + + if (sendRotation) + { + auto now_ms = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch() + ).count(); + + // Формируем сетевой пакет + // Нам нужно отправить: дискретный угол, дискретную силу и текущую матрицу/позицию для синхронизации + std::string msg = "ROT:" + std::to_string(now_ms) + ":" + std::to_string(discreteAngle) + ":" + std::to_string(discreteMag); + msg = msg + ":" + formPingMessageContent(); + networkClient->Send(msg); + std::cout << "Sending: " << msg << std::endl; + } } //std::cout << "shipVelocity=" << Environment::shipVelocity << " delta=" << delta << std::endl; @@ -1124,8 +1199,24 @@ namespace ZL void Game::extrapolateRemotePlayers() { + /*auto now = std::chrono::system_clock::now(); + + //Apply server delay: + now -= std::chrono::milliseconds(CLIENT_DELAY); + + latestRemotePlayers = networkClient->getRemotePlayers(); + + for (auto& [id, rp] : latestRemotePlayers) { + + }*/ + + /* + * auto now = std::chrono::system_clock::now(); + //Apply server delay: + now -= std::chrono::milliseconds(CLIENT_DELAY); + latestRemotePlayers = networkClient->getRemotePlayers(); for (auto& [id, rp] : latestRemotePlayers) { @@ -1139,6 +1230,7 @@ namespace ZL // Ограничим экстраполяцию (например, не более 2 секунд), // чтобы в случае лага корабли не улетали в бесконечность + if (deltaMs < 0) deltaMs = 0; if (deltaMs > 2000) deltaMs = 2000; // 2. Сбрасываем физическое состояние rp.state в значения из последнего пакета @@ -1155,7 +1247,7 @@ namespace ZL } networkClient->updateRemotePlayers(latestRemotePlayers); - + */ } std::string Game::formPingMessageContent() diff --git a/src/Game.h b/src/Game.h index 0b916ed..d76f738 100644 --- a/src/Game.h +++ b/src/Game.h @@ -65,7 +65,9 @@ namespace ZL { std::vector boxCoordsArr; std::vector boxRenderArr; - std::unordered_map latestRemotePlayers; + std::unordered_map latestRemotePlayers; + + bool velocityChanged = false; static const size_t CONST_TIMER_INTERVAL = 10; static const size_t CONST_MAX_TIME_INTERVAL = 1000; diff --git a/src/network/ClientState.h b/src/network/ClientState.h index 0074c8a..ea11745 100644 --- a/src/network/ClientState.h +++ b/src/network/ClientState.h @@ -13,6 +13,9 @@ constexpr float ANGULAR_ACCEL = 0.005f * 1000.0f; constexpr float SHIP_ACCEL = 1.0f * 1000.0f; constexpr float ROTATION_SENSITIVITY = 0.002f; +constexpr long long SERVER_DELAY = 0; //ms +constexpr long long CLIENT_DELAY = 1500; //ms +constexpr long long CUTOFF_TIME = 5000; //ms struct ClientState { int id = 0; Eigen::Vector3f position = { 0, 0, 45000.0f }; @@ -27,13 +30,6 @@ struct ClientState { std::chrono::system_clock::time_point lastUpdateServerTime; void simulate_physics(size_t delta) { - // Константы из Game.cpp, приведенные к секундам (умножаем на 1000) - //const float ANGULAR_ACCEL = 0.005f * 1000.0f; - //const float ROTATION_SENSITIVITY = 0.002f; - //const float SHIP_ACCEL = 1.0f * 1000.0f; // CONST_ACCELERATION - - // 1. Вычисляем targetAngularVelocity на лету из дискретных значений - if (discreteMag > 0.01f) { float rad = static_cast(discreteAngle) * static_cast(M_PI) / 180.0f; @@ -93,52 +89,6 @@ struct ClientState { //std::cout << "NOT Rotating ship. speedScale=" << speedScale << " discreteMag=" << discreteMag << "\n"; } - /* - Eigen::Vector3f targetAngularVelocity = Eigen::Vector3f::Zero(); - - if (discreteMag > 0.001f) { - float rad = static_cast(discreteAngle) * static_cast(M_PI) / 180.0f; - // Направление из угла (как в твоем обновленном клиентском коде) - Eigen::Vector3f targetDir(sinf(rad), cosf(rad), 0.0f); - targetAngularVelocity = targetDir * discreteMag; - } - - // 2. Линейное изменение текущей угловой скорости к вычисленной цели - Eigen::Vector3f diffVel = targetAngularVelocity - currentAngularVelocity; - float diffLen = diffVel.norm(); - - if (diffLen > 0.0001f) { - float maxChange = ANGULAR_ACCEL * dt_s; - if (diffLen <= maxChange) { - currentAngularVelocity = targetAngularVelocity; - } - else { - currentAngularVelocity += (diffVel / diffLen) * maxChange; - } - } - else if (discreteMag < 0.001f && currentAngularVelocity.norm() > 0.0001f) { - // Если джойстик отпущен, используем ту же логику торможения (или ANGULAR_ACCEL) - float currentSpeed = currentAngularVelocity.norm(); - float drop = ANGULAR_ACCEL * dt_s; - if (currentSpeed <= drop) { - currentAngularVelocity = Eigen::Vector3f::Zero(); - } - else { - currentAngularVelocity -= (currentAngularVelocity / currentSpeed) * drop; - } - }*/ - - /* - // 3. Применение вращения к матрице (Интеграция) - float speedScale = currentAngularVelocity.norm(); - if (speedScale > 0.0001f) { - float deltaAlpha = speedScale * dt_s * ROTATION_SENSITIVITY; - Eigen::Vector3f axis = currentAngularVelocity.normalized(); - - Eigen::Quaternionf rotateQuat(Eigen::AngleAxisf(deltaAlpha, axis)); - rotation = rotation * rotateQuat.toRotationMatrix(); - }*/ - // 4. Линейное изменение линейной скорости float shipDesiredVelocity = selectedVelocity * 100.f; @@ -177,7 +127,7 @@ struct ClientState { // 3. Защита от слишком больших скачков (Clamp) // Если лаг более 500мс, ограничиваем его, чтобы избежать резких рывков - long long final_lag_ms = min(deltaMs, 500ll); + long long final_lag_ms = deltaMs;//min(deltaMs, 500ll); if (final_lag_ms > 0) { // Доматываем симуляцию на величину задержки @@ -186,7 +136,7 @@ struct ClientState { } } - std::string get_state_string(int id) { + std::string get_state_string() { // Используем кватернион для передачи вращения (4 числа вместо 9) Eigen::Quaternionf q(rotation); diff --git a/src/network/NetworkInterface.h b/src/network/NetworkInterface.h index 8f1eb33..c50f0f9 100644 --- a/src/network/NetworkInterface.h +++ b/src/network/NetworkInterface.h @@ -1,10 +1,70 @@ #pragma once #include #include +#include #include "ClientState.h" // NetworkInterface.h - namespace ZL { + + struct RemotePlayer + { + std::vector timedRemoteStates; + + bool canFetchClientStateAtTime(std::chrono::system_clock::time_point targetTime) const + { + if (timedRemoteStates.empty()) + { + return false; + } + if (timedRemoteStates[0].lastUpdateServerTime > targetTime) + { + return false; + } + + return true; + } + + ClientState fetchClientStateAtTime(std::chrono::system_clock::time_point targetTime) const { + + ClientState closestState; + + if (timedRemoteStates.empty()) + { + throw std::runtime_error("No timed client states available"); + return closestState; + } + if (timedRemoteStates[0].lastUpdateServerTime > targetTime) + { + throw std::runtime_error("Found time but it is in future"); + return closestState; + } + if (timedRemoteStates.size() == 1) + { + closestState = timedRemoteStates[0]; + closestState.apply_lag_compensation(targetTime); + return closestState; + } + + + for (size_t i = 0; i < timedRemoteStates.size() - 1; ++i) + { + const auto& earlierState = timedRemoteStates[i]; + const auto& laterState = timedRemoteStates[i + 1]; + if (earlierState.lastUpdateServerTime <= targetTime && laterState.lastUpdateServerTime >= targetTime) + { + closestState = earlierState; + closestState.apply_lag_compensation(targetTime); + return closestState; + } + } + + closestState = timedRemoteStates[timedRemoteStates.size() - 1]; + closestState.apply_lag_compensation(targetTime); + return closestState; + } + }; + class INetworkClient { public: virtual ~INetworkClient() = default; @@ -12,7 +72,6 @@ namespace ZL { virtual void Send(const std::string& message) = 0; virtual bool IsConnected() const = 0; virtual void Poll() = 0; // - virtual std::unordered_map getRemotePlayers() = 0; - virtual void updateRemotePlayers(const std::unordered_map& newRemotePlayers) = 0; + virtual std::unordered_map getRemotePlayers() = 0; }; } diff --git a/src/network/WebSocketClient.cpp b/src/network/WebSocketClient.cpp index 7039c9b..8be73d9 100644 --- a/src/network/WebSocketClient.cpp +++ b/src/network/WebSocketClient.cpp @@ -73,10 +73,15 @@ namespace ZL { auto nowTime = std::chrono::system_clock::now(); + //Apply server delay: + nowTime -= std::chrono::milliseconds(CLIENT_DELAY); + auto now_ms = std::chrono::duration_cast( nowTime.time_since_epoch() ).count(); + + std::string msg = messageQueue.front(); messageQueue.pop(); @@ -88,6 +93,95 @@ namespace ZL { std::string subType = parts[2]; uint64_t sentTime = std::stoull(parts[3]); + ClientState remoteState; + remoteState.id = remoteId; + + std::chrono::system_clock::time_point uptime_timepoint{ std::chrono::duration_cast(std::chrono::milliseconds(sentTime)) }; + remoteState.lastUpdateServerTime = uptime_timepoint; + + if (subType == "VEL") { + remoteState.selectedVelocity = std::stoi(parts[4]); + int startFrom = 5; + remoteState.position = { std::stof(parts[startFrom]), std::stof(parts[startFrom + 1]), std::stof(parts[startFrom + 2]) }; + Eigen::Quaternionf q( + std::stof(parts[startFrom + 3]), + std::stof(parts[startFrom + 4]), + std::stof(parts[startFrom + 5]), + std::stof(parts[startFrom + 6])); + remoteState.rotation = q.toRotationMatrix(); + + remoteState.currentAngularVelocity = Eigen::Vector3f{ + std::stof(parts[startFrom + 7]), + std::stof(parts[startFrom + 8]), + std::stof(parts[startFrom + 9]) }; + remoteState.velocity = std::stof(parts[startFrom + 10]); + remoteState.selectedVelocity = std::stoi(parts[startFrom + 11]); + remoteState.discreteMag = std::stof(parts[startFrom + 12]); + remoteState.discreteAngle = std::stoi(parts[startFrom + 13]); + + } + else if (subType == "ROT") { + remoteState.discreteAngle = std::stoi(parts[4]); + remoteState.discreteMag = std::stof(parts[5]); + int startFrom = 6; + remoteState.position = { std::stof(parts[startFrom]), std::stof(parts[startFrom + 1]), std::stof(parts[startFrom + 2]) }; + Eigen::Quaternionf q( + std::stof(parts[startFrom + 3]), + std::stof(parts[startFrom + 4]), + std::stof(parts[startFrom + 5]), + std::stof(parts[startFrom + 6])); + remoteState.rotation = q.toRotationMatrix(); + + remoteState.currentAngularVelocity = Eigen::Vector3f{ + std::stof(parts[startFrom + 7]), + std::stof(parts[startFrom + 8]), + std::stof(parts[startFrom + 9]) }; + remoteState.velocity = std::stof(parts[startFrom + 10]); + remoteState.selectedVelocity = std::stoi(parts[startFrom + 11]); + remoteState.discreteMag = std::stof(parts[startFrom + 12]); + remoteState.discreteAngle = std::stoi(parts[startFrom + 13]); + } + else if (subType == "PING") { + //remoteState.discreteAngle = std::stoi(parts[4]); + //remoteState.discreteMag = std::stof(parts[5]); + int startFrom = 4; + remoteState.position = { std::stof(parts[startFrom]), std::stof(parts[startFrom + 1]), std::stof(parts[startFrom + 2]) }; + Eigen::Quaternionf q( + std::stof(parts[startFrom + 3]), + std::stof(parts[startFrom + 4]), + std::stof(parts[startFrom + 5]), + std::stof(parts[startFrom + 6])); + remoteState.rotation = q.toRotationMatrix(); + + remoteState.currentAngularVelocity = Eigen::Vector3f{ + std::stof(parts[startFrom + 7]), + std::stof(parts[startFrom + 8]), + std::stof(parts[startFrom + 9]) }; + remoteState.velocity = std::stof(parts[startFrom + 10]); + remoteState.selectedVelocity = std::stoi(parts[startFrom + 11]); + remoteState.discreteMag = std::stof(parts[startFrom + 12]); + remoteState.discreteAngle = std::stoi(parts[startFrom + 13]); + } + + { + std::lock_guard pLock(playersMutex); + auto& rp = remotePlayers[remoteId]; + rp.timedRemoteStates.push_back(remoteState); + + auto cutoff_time = nowTime - std::chrono::milliseconds(CUTOFF_TIME); + + while (rp.timedRemoteStates.size() > 0 && rp.timedRemoteStates[0].lastUpdateServerTime < cutoff_time) + { + rp.timedRemoteStates.erase(rp.timedRemoteStates.begin()); + } + } + + + + + + + /* std::lock_guard pLock(playersMutex); if (remotePlayers.count(remoteId)) { auto& rp = remotePlayers[remoteId]; @@ -111,26 +205,6 @@ namespace ZL { rp.selectedVelocity = std::stoi(parts[startFrom + 11]); rp.discreteMag = std::stof(parts[startFrom + 12]); rp.discreteAngle = std::stoi(parts[startFrom + 13]); - /*position = { std::stof(parts[startFrom]), std::stof(parts[startFrom+1]), std::stof(parts[startFrom+2]) }; - - // , . - // , 4 : - Eigen::Quaternionf q( - std::stof(parts[startFrom+3]), - std::stof(parts[startFrom+4]), - std::stof(parts[startFrom+5]), - std::stof(parts[startFrom+6])); - rotation = q.toRotationMatrix(); - - currentAngularVelocity = Eigen::Vector3f{ - std::stof(parts[startFrom+7]), - std::stof(parts[startFrom+8]), - std::stof(parts[startFrom+9]) }; - velocity = std::stof(parts[startFrom+10]); - selectedVelocity = std::stoi(parts[startFrom+11]); - discreteMag = std::stof(parts[startFrom+12]); - discreteAngle = std::stoi(parts[15]); - */ } else if (subType == "ROT") { @@ -174,7 +248,7 @@ namespace ZL { // , Game.cpp // , now_ms rp.lastUpdateServerTime = nowTime; - } + }*/ } else if (msg.rfind("WORLD_UPDATE|", 0) == 0) { parseWorldUpdate(msg, nowTime); @@ -208,24 +282,37 @@ namespace ZL { void WebSocketClient::updateRemotePlayer(int id, const std::vector& vals, uint64_t serverTime, std::chrono::system_clock::time_point now_ms) { - auto& rp = remotePlayers[id]; - rp.id = id; + //auto& rp = remotePlayers[id]; - rp.position = { std::stof(vals[1]), std::stof(vals[2]), std::stof(vals[3]) }; - rp.rotation = Eigen::Quaternionf(std::stof(vals[4]), std::stof(vals[5]), std::stof(vals[6]), std::stof(vals[7])); + ClientState remoteState; + + remoteState.id = id; + + remoteState.position = { std::stof(vals[1]), std::stof(vals[2]), std::stof(vals[3]) }; + remoteState.rotation = Eigen::Quaternionf(std::stof(vals[4]), std::stof(vals[5]), std::stof(vals[6]), std::stof(vals[7])); // 3. ( ) - rp.velocity = std::stof(vals[8]); - rp.currentAngularVelocity = { std::stof(vals[9]), std::stof(vals[10]), std::stof(vals[11]) }; - rp.selectedVelocity = std::stoi(vals[12]); - rp.discreteMag = std::stof(vals[13]); - rp.discreteAngle = std::stoi(vals[14]); + remoteState.velocity = std::stof(vals[8]); + remoteState.currentAngularVelocity = { std::stof(vals[9]), std::stof(vals[10]), std::stof(vals[11]) }; + remoteState.selectedVelocity = std::stoi(vals[12]); + remoteState.discreteMag = std::stof(vals[13]); + remoteState.discreteAngle = std::stoi(vals[14]); std::chrono::system_clock::time_point uptime_timepoint{ std::chrono::duration_cast(std::chrono::milliseconds(serverTime)) }; - std::cout << "PING Received discreteMag=" << rp.discreteMag << std::endl; + //std::cout << "PING Received discreteMag=" << rp.discreteMag << std::endl; - rp.lastUpdateServerTime = uptime_timepoint; - rp.apply_lag_compensation(now_ms); + remoteState.lastUpdateServerTime = uptime_timepoint; + + auto& rp = remotePlayers[id]; + rp.timedRemoteStates.push_back(remoteState); + + auto cutoff_time = now_ms - std::chrono::milliseconds(CUTOFF_TIME); + + while (rp.timedRemoteStates.size() > 0 && rp.timedRemoteStates[0].lastUpdateServerTime < cutoff_time) + { + rp.timedRemoteStates.erase(rp.timedRemoteStates.begin()); + } + //rp.apply_lag_compensation(now_ms); } diff --git a/src/network/WebSocketClient.h b/src/network/WebSocketClient.h index 54062d2..e551504 100644 --- a/src/network/WebSocketClient.h +++ b/src/network/WebSocketClient.h @@ -9,6 +9,7 @@ #include namespace ZL { + class WebSocketClient : public INetworkClient { private: // io_context TaskManager @@ -28,7 +29,7 @@ namespace ZL { bool connected = false; int clientId = -1; - std::unordered_map remotePlayers; + std::unordered_map remotePlayers; std::mutex playersMutex; void startAsyncRead(); @@ -49,18 +50,9 @@ namespace ZL { bool IsConnected() const override { return connected; } int GetClientId() const { return clientId; } - std::unordered_map getRemotePlayers() override { + std::unordered_map getRemotePlayers() override { std::lock_guard lock(playersMutex); return remotePlayers; } - - void updateRemotePlayers(const std::unordered_map& newRemotePlayers) override { - std::lock_guard lock(playersMutex); - - for (const auto& [id, newPlayer] : newRemotePlayers) { - remotePlayers[id] = newPlayer; - } - - } }; } \ No newline at end of file