Fixing bug, implementing delays, remove WORLD_UPDATE
This commit is contained in:
1 parent
3c55b59c8d
commit
64385ba15c
7 files changed
+398
-136
No files matched your search
+98
-6
@@ -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<std::chrono::milliseconds>(
|
||||
/*auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
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::milliseconds>(
|
||||
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<int>(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<std::chrono::milliseconds>(
|
||||
|
||||
sendRotation = true;
|
||||
/*auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
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::milliseconds>(
|
||||
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::milliseconds>(
|
||||
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::milliseconds>(
|
||||
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()
|
||||
|
||||
Reference in new issue
Block a user