Change clock to system clock, fix bugs
This commit is contained in:
parent
f967807296
commit
e0a60dcd6f
@ -40,7 +40,7 @@ class Session : public std::enable_shared_from_this<Session> {
|
|||||||
ClientState state_;
|
ClientState state_;
|
||||||
|
|
||||||
void process_message(const std::string& msg) {
|
void process_message(const std::string& msg) {
|
||||||
auto now_server = std::chrono::steady_clock::now();
|
auto now_server = std::chrono::system_clock::now();
|
||||||
|
|
||||||
// Ïðåäïîëîæèì ôîðìàò ñîîáùåíèé:
|
// Ïðåäïîëîæèì ôîðìàò ñîîáùåíèé:
|
||||||
// ROT:ANGLE:MAG:TIMESTAMP
|
// ROT:ANGLE:MAG:TIMESTAMP
|
||||||
@ -65,7 +65,7 @@ class Session : public std::enable_shared_from_this<Session> {
|
|||||||
|
|
||||||
if (deltaMs > 0) state_.simulate_physics(deltaMs);
|
if (deltaMs > 0) state_.simulate_physics(deltaMs);
|
||||||
|
|
||||||
std::chrono::steady_clock::time_point uptime_timepoint{ std::chrono::duration_cast<std::chrono::steady_clock::time_point::duration>(std::chrono::milliseconds(clientTimestamp)) };
|
std::chrono::system_clock::time_point uptime_timepoint{ std::chrono::duration_cast<std::chrono::system_clock::time_point::duration>(std::chrono::milliseconds(clientTimestamp)) };
|
||||||
state_.lastUpdateServerTime = uptime_timepoint;
|
state_.lastUpdateServerTime = uptime_timepoint;
|
||||||
|
|
||||||
|
|
||||||
@ -73,6 +73,7 @@ class Session : public std::enable_shared_from_this<Session> {
|
|||||||
if (parts[0] == "ROT") {
|
if (parts[0] == "ROT") {
|
||||||
state_.discreteAngle = std::stoi(parts[2]);
|
state_.discreteAngle = std::stoi(parts[2]);
|
||||||
state_.discreteMag = std::stof(parts[3]);
|
state_.discreteMag = std::stof(parts[3]);
|
||||||
|
std::cout << "ROT id = " << this->id_ << " discreteMag=" << state_.discreteMag << std::endl;
|
||||||
state_.apply_lag_compensation(now_server);
|
state_.apply_lag_compensation(now_server);
|
||||||
state_.lastUpdateServerTime = now_server;
|
state_.lastUpdateServerTime = now_server;
|
||||||
retranslateMessage(msg);
|
retranslateMessage(msg);
|
||||||
@ -85,6 +86,7 @@ class Session : public std::enable_shared_from_this<Session> {
|
|||||||
}
|
}
|
||||||
else if (parts[0] == "PING") {
|
else if (parts[0] == "PING") {
|
||||||
state_.handle_full_sync(parts);
|
state_.handle_full_sync(parts);
|
||||||
|
std::cout << "PING id = " << this->id_ <<" discreteMag=" << state_.discreteMag << std::endl;
|
||||||
state_.apply_lag_compensation(now_server);
|
state_.apply_lag_compensation(now_server);
|
||||||
state_.lastUpdateServerTime = now_server;
|
state_.lastUpdateServerTime = now_server;
|
||||||
}
|
}
|
||||||
@ -110,7 +112,7 @@ public:
|
|||||||
|
|
||||||
void init()
|
void init()
|
||||||
{
|
{
|
||||||
state_.lastUpdateServerTime = std::chrono::steady_clock::now();
|
state_.lastUpdateServerTime = std::chrono::system_clock::now();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string get_state_string() {
|
std::string get_state_string() {
|
||||||
@ -133,7 +135,7 @@ public:
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void tick_physics_global(std::chrono::steady_clock::time_point now) {
|
void tick_physics_global(std::chrono::system_clock::time_point now) {
|
||||||
long long deltaMs = 0;
|
long long deltaMs = 0;
|
||||||
|
|
||||||
// Åñëè ýòî ñàìûé ïåðâûé òèê, ïðîñòî çàïîìèíàåì âðåìÿ
|
// Åñëè ýòî ñàìûé ïåðâûé òèê, ïðîñòî çàïîìèíàåì âðåìÿ
|
||||||
@ -181,7 +183,7 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
void update_world(net::steady_timer& timer, net::io_context& ioc) {
|
void update_world(net::steady_timer& timer, net::io_context& ioc) {
|
||||||
auto now = std::chrono::steady_clock::now();
|
auto now = std::chrono::system_clock::now();
|
||||||
|
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(g_sessions_mutex);
|
std::lock_guard<std::mutex> lock(g_sessions_mutex);
|
||||||
@ -196,7 +198,7 @@ void update_world(net::steady_timer& timer, net::io_context& ioc) {
|
|||||||
last_broadcast = now;
|
last_broadcast = now;
|
||||||
|
|
||||||
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||||
std::chrono::steady_clock::now().time_since_epoch()
|
std::chrono::system_clock::now().time_since_epoch()
|
||||||
).count();
|
).count();
|
||||||
|
|
||||||
// Ñîáèðàåì äàííûå âñåõ èãðîêîâ â îäèí ïàêåò
|
// Ñîáèðàåì äàííûå âñåõ èãðîêîâ â îäèí ïàêåò
|
||||||
|
|||||||
14
src/Game.cpp
14
src/Game.cpp
@ -242,7 +242,7 @@ namespace ZL
|
|||||||
if (newVel != Environment::shipSelectedVelocity) {
|
if (newVel != Environment::shipSelectedVelocity) {
|
||||||
Environment::shipSelectedVelocity = newVel;
|
Environment::shipSelectedVelocity = newVel;
|
||||||
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||||
std::chrono::steady_clock::now().time_since_epoch()
|
std::chrono::system_clock::now().time_since_epoch()
|
||||||
).count();
|
).count();
|
||||||
std::string msg = "VEL:" + std::to_string(now_ms) + ":" + std::to_string(Environment::shipSelectedVelocity);
|
std::string msg = "VEL:" + std::to_string(now_ms) + ":" + std::to_string(Environment::shipSelectedVelocity);
|
||||||
networkClient->Send(msg);
|
networkClient->Send(msg);
|
||||||
@ -634,7 +634,7 @@ namespace ZL
|
|||||||
if (pingTimer >= 1000.0f) {
|
if (pingTimer >= 1000.0f) {
|
||||||
|
|
||||||
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||||
std::chrono::steady_clock::now().time_since_epoch()
|
std::chrono::system_clock::now().time_since_epoch()
|
||||||
).count();
|
).count();
|
||||||
|
|
||||||
// 1. Извлекаем кватернион из матрицы поворота
|
// 1. Извлекаем кватернион из матрицы поворота
|
||||||
@ -707,12 +707,14 @@ namespace ZL
|
|||||||
int discreteAngle = static_cast<int>(radians * 180.0f / M_PI);
|
int discreteAngle = static_cast<int>(radians * 180.0f / M_PI);
|
||||||
if (discreteAngle < 0) discreteAngle += 360;
|
if (discreteAngle < 0) discreteAngle += 360;
|
||||||
|
|
||||||
|
std::cout << "OUTPUT discreteAngle=" << discreteAngle << std::endl;
|
||||||
|
|
||||||
// 3. Проверяем, изменились ли параметры значимо для отправки на сервер
|
// 3. Проверяем, изменились ли параметры значимо для отправки на сервер
|
||||||
if (discreteAngle != Environment::lastSentAngle || discreteMag != Environment::lastSentMagnitude) {
|
if (discreteAngle != Environment::lastSentAngle || discreteMag != Environment::lastSentMagnitude) {
|
||||||
Environment::lastSentAngle = discreteAngle;
|
Environment::lastSentAngle = discreteAngle;
|
||||||
Environment::lastSentMagnitude = discreteMag;
|
Environment::lastSentMagnitude = discreteMag;
|
||||||
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||||
std::chrono::steady_clock::now().time_since_epoch()
|
std::chrono::system_clock::now().time_since_epoch()
|
||||||
).count();
|
).count();
|
||||||
|
|
||||||
// Формируем сетевой пакет
|
// Формируем сетевой пакет
|
||||||
@ -776,7 +778,7 @@ namespace ZL
|
|||||||
Environment::lastSentAngle = discreteAngle;
|
Environment::lastSentAngle = discreteAngle;
|
||||||
Environment::lastSentMagnitude = discreteMag;
|
Environment::lastSentMagnitude = discreteMag;
|
||||||
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||||
std::chrono::steady_clock::now().time_since_epoch()
|
std::chrono::system_clock::now().time_since_epoch()
|
||||||
).count();
|
).count();
|
||||||
|
|
||||||
// Формируем сетевой пакет
|
// Формируем сетевой пакет
|
||||||
@ -809,7 +811,7 @@ namespace ZL
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "shipVelocity=" << Environment::shipVelocity << " delta=" << delta << std::endl;
|
//std::cout << "shipVelocity=" << Environment::shipVelocity << " delta=" << delta << std::endl;
|
||||||
// Движение вперед (существующая логика)
|
// Движение вперед (существующая логика)
|
||||||
if (fabs(Environment::shipVelocity) > 0.01f)
|
if (fabs(Environment::shipVelocity) > 0.01f)
|
||||||
{
|
{
|
||||||
@ -1102,7 +1104,7 @@ namespace ZL
|
|||||||
|
|
||||||
void Game::extrapolateRemotePlayers() {
|
void Game::extrapolateRemotePlayers() {
|
||||||
|
|
||||||
auto now = std::chrono::steady_clock::now();
|
auto now = std::chrono::system_clock::now();
|
||||||
|
|
||||||
latestRemotePlayers = networkClient->getRemotePlayers();
|
latestRemotePlayers = networkClient->getRemotePlayers();
|
||||||
|
|
||||||
|
|||||||
@ -24,7 +24,7 @@ struct ClientState {
|
|||||||
int discreteAngle = -1;
|
int discreteAngle = -1;
|
||||||
|
|
||||||
// Для расчета лага
|
// Для расчета лага
|
||||||
std::chrono::steady_clock::time_point lastUpdateServerTime;
|
std::chrono::system_clock::time_point lastUpdateServerTime;
|
||||||
|
|
||||||
void simulate_physics(size_t delta) {
|
void simulate_physics(size_t delta) {
|
||||||
// Константы из Game.cpp, приведенные к секундам (умножаем на 1000)
|
// Константы из Game.cpp, приведенные к секундам (умножаем на 1000)
|
||||||
@ -87,6 +87,10 @@ struct ClientState {
|
|||||||
Eigen::Quaternionf rotateQuat(Eigen::AngleAxisf(deltaAlpha, axis));
|
Eigen::Quaternionf rotateQuat(Eigen::AngleAxisf(deltaAlpha, axis));
|
||||||
|
|
||||||
rotation = rotation * rotateQuat.toRotationMatrix();
|
rotation = rotation * rotateQuat.toRotationMatrix();
|
||||||
|
//std::cout << "Rotating ship. d="<< delta <<" DeltaAlpha: " << deltaAlpha << ", Axis: [" << axis.x() << ", " << axis.y() << ", " << axis.z() << "]\n";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
//std::cout << "NOT Rotating ship. speedScale=" << speedScale << " discreteMag=" << discreteMag << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -154,7 +158,6 @@ struct ClientState {
|
|||||||
velocity = shipDesiredVelocity;
|
velocity = shipDesiredVelocity;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::cout << "velocity=" << velocity << " delta=" << delta << std::endl;
|
|
||||||
|
|
||||||
if (fabs(velocity) > 0.01f)
|
if (fabs(velocity) > 0.01f)
|
||||||
{
|
{
|
||||||
@ -164,16 +167,7 @@ struct ClientState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void apply_lag_compensation(std::chrono::steady_clock::time_point nowTime) {
|
void apply_lag_compensation(std::chrono::system_clock::time_point nowTime) {
|
||||||
|
|
||||||
// 1. Получаем текущее время сервера в той же шкале (мс)
|
|
||||||
/*uto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
||||||
std::chrono::steady_clock::now().time_since_epoch()
|
|
||||||
).count();
|
|
||||||
|
|
||||||
long long deltaMs = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
||||||
std::chrono::steady_clock::now().time_since_epoch() - lastUpdateServerTime
|
|
||||||
).count();*/
|
|
||||||
|
|
||||||
// 2. Вычисляем задержку
|
// 2. Вычисляем задержку
|
||||||
long long deltaMs = 0;
|
long long deltaMs = 0;
|
||||||
|
|||||||
@ -69,7 +69,7 @@ namespace ZL {
|
|||||||
void WebSocketClient::Poll() {
|
void WebSocketClient::Poll() {
|
||||||
std::lock_guard<std::mutex> lock(queueMutex);
|
std::lock_guard<std::mutex> lock(queueMutex);
|
||||||
|
|
||||||
auto nowTime = std::chrono::steady_clock::now();
|
auto nowTime = std::chrono::system_clock::now();
|
||||||
|
|
||||||
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||||
nowTime.time_since_epoch()
|
nowTime.time_since_epoch()
|
||||||
@ -98,6 +98,7 @@ namespace ZL {
|
|||||||
rp.discreteAngle = std::stoi(parts[4]);
|
rp.discreteAngle = std::stoi(parts[4]);
|
||||||
rp.discreteMag = std::stof(parts[5]);
|
rp.discreteMag = std::stof(parts[5]);
|
||||||
}
|
}
|
||||||
|
std::cout << "EVENT Received discreteMag=" << rp.discreteMag << std::endl;
|
||||||
|
|
||||||
// --- ÊÎÌÏÅÍÑÀÖÈß ËÀÃÀ ---
|
// --- ÊÎÌÏÅÍÑÀÖÈß ËÀÃÀ ---
|
||||||
// Âû÷èñëÿåì çàäåðæêó â ñåêóíäàõ
|
// Âû÷èñëÿåì çàäåðæêó â ñåêóíäàõ
|
||||||
@ -160,9 +161,11 @@ namespace ZL {
|
|||||||
rp.velocity = std::stof(vals[8]);
|
rp.velocity = std::stof(vals[8]);
|
||||||
rp.currentAngularVelocity = { std::stof(vals[9]), std::stof(vals[10]), std::stof(vals[11]) };
|
rp.currentAngularVelocity = { std::stof(vals[9]), std::stof(vals[10]), std::stof(vals[11]) };
|
||||||
rp.selectedVelocity = std::stoi(vals[12]);
|
rp.selectedVelocity = std::stoi(vals[12]);
|
||||||
rp.discreteMag = std::stoi(vals[13]);
|
rp.discreteMag = std::stof(vals[13]);
|
||||||
rp.discreteAngle = std::stoi(vals[14]);
|
rp.discreteAngle = std::stoi(vals[14]);
|
||||||
std::chrono::steady_clock::time_point uptime_timepoint{ std::chrono::duration_cast<std::chrono::steady_clock::time_point::duration>(std::chrono::milliseconds(serverTime)) };
|
std::chrono::system_clock::time_point uptime_timepoint{ std::chrono::duration_cast<std::chrono::system_clock::time_point::duration>(std::chrono::milliseconds(serverTime)) };
|
||||||
|
|
||||||
|
std::cout << "PING Received discreteMag=" << rp.discreteMag << std::endl;
|
||||||
|
|
||||||
rp.lastUpdateServerTime = uptime_timepoint;
|
rp.lastUpdateServerTime = uptime_timepoint;
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user