Added planet physics and simple base

This commit is contained in:
Vladislav Khorev committed 2026-01-25 20:55:36 +03:00
1 parent 96d879076a
commit d425167dce
10 files changed
+6321 -22

No files matched your search

+63
View File
@@ -45,6 +45,69 @@ void ClientState::simulate_physics(size_t delta) {
}
}
float distToCenter = position.norm(); // Расстояние до {0,0,0}
float landingZone = PLANET_RADIUS * PLANET_ALIGN_ZONE;
if (distToCenter <= landingZone) {
Eigen::Vector3f planetNormal = position.normalized();
// --- 1. ВЫРАВНИВАНИЕ КРЕНА (Roll - ось Z) ---
Eigen::Vector3f localX = rotation.col(0);
float rollError = localX.dot(planetNormal);
if (std::abs(rollError) > 0.001f) {
currentAngularVelocity.z() -= rollError * PLANET_ANGULAR_ACCEL * delta;
currentAngularVelocity.z() = std::max(-PLANET_MAX_ANGULAR_VELOCITY,
std::min(currentAngularVelocity.z(), PLANET_MAX_ANGULAR_VELOCITY));
}
// --- 2. ОГРАНИЧЕНИЕ ТАНГАЖА (Pitch - ось X) ---
// Нос корабля в локальных координатах — это -Z (третий столбец матрицы)
Eigen::Vector3f forwardDir = -rotation.col(2);
// В твоем случае dot < 0 означает, что нос направлен К планете
float pitchSin = forwardDir.dot(planetNormal);
float currentPitchAngle = asinf(std::clamp(pitchSin, -1.0f, 1.0f));
// Лимит у нас M_PI / 6.0 (примерно 0.523)
// По твоим данным: -0.89 < -0.523, значит мы превысили наклон вниз
if (currentPitchAngle < -PITCH_LIMIT) {
// Вычисляем ошибку (насколько мы ушли "ниже" лимита)
// -0.89 - (-0.52) = -0.37
float pitchError = currentPitchAngle + PITCH_LIMIT;
// Теперь важно: нам нужно ПОДНЯТЬ нос.
// Если pitchError отрицательный, а нам нужно уменьшить вращение,
// пробуем прибавлять или вычитать в зависимости от твоей оси X.
// Судя по стандартной логике Eigen, нам нужно ПЛЮСОВАТЬ:
currentAngularVelocity.x() -= pitchError * PLANET_ANGULAR_ACCEL * delta;
}
}
else {
// Вне зоны: тормозим Z (крен) И X (тангаж), если они были активны
float drop = ANGULAR_ACCEL * delta;
if (std::abs(currentAngularVelocity[2]) > 0.0001f) {
if (std::abs(currentAngularVelocity[2]) <= drop) {
currentAngularVelocity[2] = 0.0f;
}
else {
currentAngularVelocity[2] -= (currentAngularVelocity[2] > 0 ? 1.0f : -1.0f) * drop;
}
}
}
// Ограничение скорости
currentAngularVelocity.x() = std::max(-PLANET_MAX_ANGULAR_VELOCITY,
std::min(currentAngularVelocity.x(), PLANET_MAX_ANGULAR_VELOCITY));
// Ограничение скорости
currentAngularVelocity.y() = std::max(-PLANET_MAX_ANGULAR_VELOCITY,
std::min(currentAngularVelocity.y(), PLANET_MAX_ANGULAR_VELOCITY));
// Ограничение скорости
currentAngularVelocity.z() = std::max(-PLANET_MAX_ANGULAR_VELOCITY,
std::min(currentAngularVelocity.z(), PLANET_MAX_ANGULAR_VELOCITY));
float speedScale = currentAngularVelocity.norm();
if (speedScale > 0.0001f) {
// Коэффициент чувствительности вращения
+6
View File
@@ -13,6 +13,12 @@ constexpr float ANGULAR_ACCEL = 0.005f * 1000.0f;
constexpr float SHIP_ACCEL = 1.0f * 1000.0f;
constexpr float ROTATION_SENSITIVITY = 0.002f;
constexpr float PLANET_RADIUS = 20000.f;
constexpr float PLANET_ALIGN_ZONE = 1.05f;
constexpr float PLANET_ANGULAR_ACCEL = 0.01f; // Подбери под динамику
constexpr float PLANET_MAX_ANGULAR_VELOCITY = 10.f;
constexpr float PITCH_LIMIT = static_cast<float>(M_PI) / 9.f;//18.0f;
constexpr long long SERVER_DELAY = 0; //ms
constexpr long long CLIENT_DELAY = 200; //ms
constexpr long long CUTOFF_TIME = 5000; //ms