diff --git a/src/Game.cpp b/src/Game.cpp index c6b5575..916b79a 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -315,7 +315,9 @@ namespace ZL //spaceshipBase.Move(Vector3f{ -0.52998, -13, 0 }); - //spaceshipBase.Move(Vector3f{ -0.52998, -10, 10 }); + spaceshipBase.Move(Vector3f{ -0.52998, 0, 10 }); + + //spaceshipBase.RotateByMatrix(Eigen::Quaternionf(Eigen::AngleAxisf(M_PI / 2.0, Eigen::Vector3f::UnitY())).toRotationMatrix()); spaceship.AssignFrom(spaceshipBase); spaceship.RefreshVBO(); @@ -449,11 +451,11 @@ namespace ZL static_cast(Environment::width) / static_cast(Environment::height), Environment::CONST_Z_NEAR, Environment::CONST_Z_FAR); renderer.PushMatrix(); - renderer.LoadIdentity(); renderer.TranslateMatrix({ 0,0, -1.0f * Environment::zoom }); renderer.TranslateMatrix({ 0, -Environment::zoom * 0.03f, 0 }); + renderer.RotateMatrix(rotateShipMat); glBindTexture(GL_TEXTURE_2D, spaceshipTexture->getTexID()); renderer.DrawVertexRenderStruct(spaceship); glEnable(GL_BLEND); @@ -602,37 +604,62 @@ namespace ZL sparkEmitter.update(static_cast(delta)); planetObject.update(static_cast(delta)); - if (Environment::tapDownHold) { - + if (Environment::tapDownHold && !uiManager.isUiInteraction()) + { float diffx = Environment::tapDownCurrentPos(0) - Environment::tapDownStartPos(0); float diffy = Environment::tapDownCurrentPos(1) - Environment::tapDownStartPos(1); - if (abs(diffy) > 5.0 || abs(diffx) > 5.0) //threshold - { + if (isDraggingShip) { + if (std::abs(diffy) > 5.0f || std::abs(diffx) > 5.0f) + { + float velocity = std::sqrt(diffx * diffx + diffy * diffy); + Environment::shipVelocity = velocity * static_cast(delta) / 100.0f; - float rotationPower = sqrtf(diffx * diffx + diffy * diffy); - float deltaAlpha = rotationPower * delta * static_cast(M_PI) / 500000.f; + float angleY = std::atan2(-diffx, -diffy); - Eigen::Vector3f rotationDirection(diffy, diffx, 0.0f); - rotationDirection.normalize(); // Eigen-way нормализация + Eigen::Quaternionf q(Eigen::AngleAxisf(angleY, Eigen::Vector3f::UnitY())); + rotateShipMat = q.toRotationMatrix(); - // Создаем кватернион через AngleAxis - // Конструктор принимает (угол_в_радианах, ось_вращения) - Eigen::Quaternionf rotateQuat(Eigen::AngleAxisf(deltaAlpha, rotationDirection)); + if (!shipMoveLockActive) { + lockedCameraMat = Environment::shipMatrix; + shipMoveLockActive = true; + } + } + else { + Environment::shipVelocity = 0.0f; + } + } + else if (isDraggingCamera) { + if (std::abs(diffy) > 5.0f || std::abs(diffx) > 5.0f) + { + float rotationPower = std::sqrt(diffx * diffx + diffy * diffy); + float deltaAlpha = rotationPower * static_cast(delta) * static_cast(M_PI) / 500000.f; - Matrix3f rotateMat = rotateQuat.toRotationMatrix(); + Eigen::Vector3f rotationDirection(diffy, diffx, 0.0f); + rotationDirection.normalize(); - Environment::shipMatrix = Environment::shipMatrix * rotateMat; - Environment::inverseShipMatrix = Environment::shipMatrix.inverse(); + Eigen::Quaternionf rotateQuat(Eigen::AngleAxisf(deltaAlpha, rotationDirection)); + Matrix3f rotateMat = rotateQuat.toRotationMatrix(); + Environment::shipMatrix = Environment::shipMatrix * rotateMat; + Environment::inverseShipMatrix = Environment::shipMatrix.inverse(); + + // плавное вращение (ВАЖНО!) + Environment::tapDownStartPos = Environment::tapDownCurrentPos; + } } } - if (fabs(Environment::shipVelocity) > 0.01f) + + if (std::fabs(Environment::shipVelocity) > 0.01f) { - Vector3f velocityDirection = { 0,0, -Environment::shipVelocity * delta / 1000.f }; - Vector3f velocityDirectionAdjusted = Environment::shipMatrix * velocityDirection; - Environment::shipPosition = Environment::shipPosition + velocityDirectionAdjusted; + Vector3f localMove = rotateShipMat * Vector3f{ 0, 0, -Environment::shipVelocity * static_cast(delta) / 1000.f }; + + Matrix3f camMat = shipMoveLockActive ? lockedCameraMat : Environment::shipMatrix; + + Vector3f worldMove = camMat * localMove; + + Environment::shipPosition = Environment::shipPosition + worldMove; } for (auto& p : projectiles) { @@ -687,7 +714,10 @@ namespace ZL const float size = 0.5f; Vector3f localForward = { 0,0,-1 }; - Vector3f worldForward = (Environment::shipMatrix * localForward).normalized(); + Matrix3f camMat1 = shipMoveLockActive ? lockedCameraMat : Environment::shipMatrix; + + Vector3f localForward1 = { 0,0,-1 }; + Vector3f worldForward = (camMat1 * (rotateShipMat * localForward1)).normalized(); for (const auto& lo : localOffsets) { Vector3f worldPos = Environment::shipPosition + Environment::shipMatrix * lo; @@ -847,13 +877,16 @@ namespace ZL }(); if (!uiManager.isUiInteraction()) { - Environment::tapDownHold = true; + if (mx < 400 && my > 400) { + isDraggingShip = true; + isDraggingCamera = false; - Environment::tapDownStartPos(0) = mx; - Environment::tapDownStartPos(1) = my; - - Environment::tapDownCurrentPos(0) = mx; - Environment::tapDownCurrentPos(1) = my; + shipMoveLockActive = false; // новый drag -> новый lock + } + else { + isDraggingShip = false; + isDraggingCamera = true; + } } } void Game::handleUp(int mx, int my) @@ -865,6 +898,16 @@ namespace ZL if (!uiManager.isUiInteraction()) { Environment::tapDownHold = false; + Environment::shipVelocity = 0.0f; + + shipMoveLockActive = false; + + if (isDraggingCamera) { + rotateShipMat = Matrix3f::Identity(); + } + + isDraggingShip = false; + isDraggingCamera = false; } } diff --git a/src/Game.h b/src/Game.h index 3b4bf8e..b805297 100644 --- a/src/Game.h +++ b/src/Game.h @@ -112,6 +112,11 @@ namespace ZL { TaskManager taskManager; + bool isDraggingShip = false; + bool isDraggingCamera = false; + Matrix3f rotateShipMat = Matrix3f::Identity(); + bool shipMoveLockActive = false; + Matrix3f lockedCameraMat = Matrix3f::Identity(); };