Change clock to system clock, fix bugs

This commit is contained in:
Vladislav Khorev committed 2026-01-17 13:59:02 +03:00
1 parent f967807296
commit e0a60dcd6f
4 files changed
+28 -27

No files matched your search

+8 -6
View File
@@ -242,7 +242,7 @@ namespace ZL
if (newVel != Environment::shipSelectedVelocity) {
Environment::shipSelectedVelocity = newVel;
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();
std::string msg = "VEL:" + std::to_string(now_ms) + ":" + std::to_string(Environment::shipSelectedVelocity);
networkClient->Send(msg);
@@ -634,7 +634,7 @@ namespace ZL
if (pingTimer >= 1000.0f) {
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();
// 1. Извлекаем кватернион из матрицы поворота
@@ -707,12 +707,14 @@ namespace ZL
int discreteAngle = static_cast<int>(radians * 180.0f / M_PI);
if (discreteAngle < 0) discreteAngle += 360;
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<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()
std::chrono::system_clock::now().time_since_epoch()
).count();
// Формируем сетевой пакет
@@ -776,7 +778,7 @@ namespace ZL
Environment::lastSentAngle = discreteAngle;
Environment::lastSentMagnitude = discreteMag;
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();
// Формируем сетевой пакет
@@ -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)
{
@@ -1102,7 +1104,7 @@ namespace ZL
void Game::extrapolateRemotePlayers() {
auto now = std::chrono::steady_clock::now();
auto now = std::chrono::system_clock::now();
latestRemotePlayers = networkClient->getRemotePlayers();
+6 -12
View File
@@ -24,7 +24,7 @@ struct ClientState {
int discreteAngle = -1;
// Для расчета лага
std::chrono::steady_clock::time_point lastUpdateServerTime;
std::chrono::system_clock::time_point lastUpdateServerTime;
void simulate_physics(size_t delta) {
// Константы из Game.cpp, приведенные к секундам (умножаем на 1000)
@@ -87,6 +87,10 @@ struct ClientState {
Eigen::Quaternionf rotateQuat(Eigen::AngleAxisf(deltaAlpha, axis));
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;
}
}
std::cout << "velocity=" << velocity << " delta=" << delta << std::endl;
if (fabs(velocity) > 0.01f)
{
@@ -164,16 +167,7 @@ struct ClientState {
}
}
void apply_lag_compensation(std::chrono::steady_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();*/
void apply_lag_compensation(std::chrono::system_clock::time_point nowTime) {
// 2. Вычисляем задержку
long long deltaMs = 0;
+6 -3
View File
@@ -69,7 +69,7 @@ namespace ZL {
void WebSocketClient::Poll() {
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>(
nowTime.time_since_epoch()
@@ -98,6 +98,7 @@ namespace ZL {
rp.discreteAngle = std::stoi(parts[4]);
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.currentAngularVelocity = { std::stof(vals[9]), std::stof(vals[10]), std::stof(vals[11]) };
rp.selectedVelocity = std::stoi(vals[12]);
rp.discreteMag = std::stoi(vals[13]);
rp.discreteMag = std::stof(vals[13]);
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;