Fixing bugs, unify velocity
This commit is contained in:
1 parent
fbc420e894
commit
f967807296
7 files changed
+153
-54
No files matched your search
+19
-8
@@ -627,7 +627,7 @@ namespace ZL
|
||||
|
||||
sparkEmitter.update(static_cast<float>(delta));
|
||||
planetObject.update(static_cast<float>(delta));
|
||||
extrapolateRemotePlayers(static_cast<float>(delta));
|
||||
extrapolateRemotePlayers();
|
||||
|
||||
static float pingTimer = 0.0f;
|
||||
pingTimer += delta;
|
||||
@@ -659,10 +659,11 @@ namespace ZL
|
||||
+ std::to_string(Environment::lastSentAngle);
|
||||
|
||||
networkClient->Send(pingMsg);
|
||||
std::cout << "Sending: " << pingMsg << std::endl;
|
||||
pingTimer = 0.0f;
|
||||
}
|
||||
|
||||
static const float CONST_ACCELERATION = 1.f;
|
||||
//static const float CONST_ACCELERATION = 1.f;
|
||||
|
||||
float shipDesiredVelocity = Environment::shipSelectedVelocity * 100.f;
|
||||
|
||||
@@ -670,7 +671,7 @@ namespace ZL
|
||||
{
|
||||
if (Environment::shipVelocity < shipDesiredVelocity)
|
||||
{
|
||||
Environment::shipVelocity += delta * CONST_ACCELERATION;
|
||||
Environment::shipVelocity += delta * SHIP_ACCEL;
|
||||
if (Environment::shipVelocity > shipDesiredVelocity)
|
||||
{
|
||||
Environment::shipVelocity = shipDesiredVelocity;
|
||||
@@ -678,7 +679,7 @@ namespace ZL
|
||||
}
|
||||
else if (Environment::shipVelocity > shipDesiredVelocity)
|
||||
{
|
||||
Environment::shipVelocity -= delta * CONST_ACCELERATION;
|
||||
Environment::shipVelocity -= delta * SHIP_ACCEL;
|
||||
if (Environment::shipVelocity < shipDesiredVelocity)
|
||||
{
|
||||
Environment::shipVelocity = shipDesiredVelocity;
|
||||
@@ -784,6 +785,7 @@ namespace ZL
|
||||
networkClient->Send(msg);
|
||||
}
|
||||
|
||||
|
||||
float currentSpeed = Environment::currentAngularVelocity.norm();
|
||||
|
||||
if (currentSpeed > 0.0001f) {
|
||||
@@ -807,6 +809,7 @@ namespace ZL
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "shipVelocity=" << Environment::shipVelocity << " delta=" << delta << std::endl;
|
||||
// Движение вперед (существующая логика)
|
||||
if (fabs(Environment::shipVelocity) > 0.01f)
|
||||
{
|
||||
@@ -1097,7 +1100,7 @@ namespace ZL
|
||||
}
|
||||
}
|
||||
|
||||
void Game::extrapolateRemotePlayers(float deltaMs) {
|
||||
void Game::extrapolateRemotePlayers() {
|
||||
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
|
||||
@@ -1105,11 +1108,16 @@ namespace ZL
|
||||
|
||||
for (auto& [id, rp] : latestRemotePlayers) {
|
||||
// 1. Рассчитываем, сколько времени прошло с момента получения последнего пакета (в секундах)
|
||||
float age_s = std::chrono::duration<float>(now - rp.lastUpdateServerTime).count();
|
||||
|
||||
auto deltaMs = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
now - rp.lastUpdateServerTime
|
||||
).count();
|
||||
|
||||
//float age_s = std::chrono::duration<float>(now - rp.lastUpdateServerTime).count();
|
||||
|
||||
// Ограничим экстраполяцию (например, не более 2 секунд),
|
||||
// чтобы в случае лага корабли не улетали в бесконечность
|
||||
if (age_s > 2.0f) age_s = 2.0f;
|
||||
if (deltaMs > 2000) deltaMs = 2000;
|
||||
|
||||
// 2. Сбрасываем физическое состояние rp.state в значения из последнего пакета
|
||||
// (Это важно: мы всегда экстраполируем от последнего достоверного серверного состояния)
|
||||
@@ -1117,11 +1125,14 @@ namespace ZL
|
||||
|
||||
// 3. Вызываем физику, чтобы "догнать" реальное время
|
||||
// Мы передаем age_s как один большой шаг симуляции
|
||||
rp.simulate_physics(age_s);
|
||||
rp.simulate_physics(deltaMs);
|
||||
rp.lastUpdateServerTime = now;
|
||||
|
||||
// Теперь rp.state.position и rp.state.rotation содержат актуальные
|
||||
// предсказанные данные для рендеринга в текущем кадре.
|
||||
}
|
||||
|
||||
networkClient->updateRemotePlayers(latestRemotePlayers);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user