mirror of
https://github.com/redstrate/Novus.git
synced 2025-04-26 13:47:46 +00:00
mdlpart: Add clamping to the camera to stop getting into fun situations
This commit is contained in:
parent
0efbc581e0
commit
a4b516e754
2 changed files with 20 additions and 28 deletions
|
@ -12,6 +12,7 @@
|
||||||
#include <QVulkanInstance>
|
#include <QVulkanInstance>
|
||||||
#include <QVulkanWindow>
|
#include <QVulkanWindow>
|
||||||
#include <QWindow>
|
#include <QWindow>
|
||||||
|
#include <cmath>
|
||||||
#include <glm/gtc/quaternion.hpp>
|
#include <glm/gtc/quaternion.hpp>
|
||||||
#include <glm/gtc/type_ptr.inl>
|
#include <glm/gtc/type_ptr.inl>
|
||||||
|
|
||||||
|
@ -55,20 +56,14 @@ public:
|
||||||
case QEvent::MouseButtonPress: {
|
case QEvent::MouseButtonPress: {
|
||||||
auto mouseEvent = dynamic_cast<QMouseEvent*>(e);
|
auto mouseEvent = dynamic_cast<QMouseEvent*>(e);
|
||||||
|
|
||||||
if (mouseEvent->button() == Qt::MouseButton::LeftButton) {
|
if (mouseEvent->button() == Qt::MouseButton::LeftButton || mouseEvent->button() == Qt::MouseButton::RightButton) {
|
||||||
part->lastX = mouseEvent->x();
|
part->lastX = mouseEvent->position().x();
|
||||||
part->lastY = mouseEvent->y();
|
part->lastY = mouseEvent->position().y();
|
||||||
part->cameraMode = MDLPart::CameraMode::Orbit;
|
part->cameraMode = mouseEvent->button() == Qt::MouseButton::LeftButton ? MDLPart::CameraMode::Orbit : MDLPart::CameraMode::Move;
|
||||||
|
|
||||||
setKeyboardGrabEnabled(true);
|
|
||||||
setMouseGrabEnabled(true);
|
|
||||||
} else if (mouseEvent->button() == Qt::MouseButton::RightButton) {
|
|
||||||
part->lastX = mouseEvent->x();
|
|
||||||
part->lastY = mouseEvent->y();
|
|
||||||
part->cameraMode = MDLPart::CameraMode::Move;
|
|
||||||
|
|
||||||
setKeyboardGrabEnabled(true);
|
setKeyboardGrabEnabled(true);
|
||||||
setMouseGrabEnabled(true);
|
setMouseGrabEnabled(true);
|
||||||
|
setCursor(Qt::BlankCursor);
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
case QEvent::MouseButtonRelease: {
|
case QEvent::MouseButtonRelease: {
|
||||||
|
@ -76,40 +71,37 @@ public:
|
||||||
|
|
||||||
setKeyboardGrabEnabled(false);
|
setKeyboardGrabEnabled(false);
|
||||||
setMouseGrabEnabled(false);
|
setMouseGrabEnabled(false);
|
||||||
|
setCursor({});
|
||||||
} break;
|
} break;
|
||||||
case QEvent::MouseMove: {
|
case QEvent::MouseMove: {
|
||||||
auto mouseEvent = dynamic_cast<QMouseEvent*>(e);
|
auto mouseEvent = dynamic_cast<QMouseEvent*>(e);
|
||||||
if (part->cameraMode != MDLPart::CameraMode::None) {
|
if (part->cameraMode != MDLPart::CameraMode::None) {
|
||||||
const int deltaX = mouseEvent->x() - part->lastX;
|
const int deltaX = mouseEvent->position().x() - part->lastX;
|
||||||
const int deltaY = mouseEvent->y() - part->lastY;
|
const int deltaY = mouseEvent->position().y() - part->lastY;
|
||||||
|
|
||||||
if (part->cameraMode == MDLPart::CameraMode::Orbit) {
|
if (part->cameraMode == MDLPart::CameraMode::Orbit) {
|
||||||
part->yaw += deltaX * 0.01f; // TODO: remove these magic numbers
|
part->yaw += deltaX * 0.01f; // TODO: remove these magic numbers
|
||||||
part->pitch += deltaY * 0.01f;
|
part->pitch += deltaY * 0.01f;
|
||||||
} else {
|
} else {
|
||||||
glm::vec3 position(
|
const glm::vec3 position(part->cameraDistance * std::sin(part->yaw),
|
||||||
part->cameraDistance * sin(part->yaw),
|
part->cameraDistance * part->pitch,
|
||||||
part->cameraDistance * part->pitch,
|
part->cameraDistance * std::cos(part->yaw));
|
||||||
part->cameraDistance * cos(part->yaw));
|
|
||||||
|
|
||||||
glm::quat rot = glm::quatLookAt((part->position + position) - part->position, {0, 1, 0});
|
const glm::quat rot = glm::quatLookAt((part->position + position) - part->position, {0, 1, 0});
|
||||||
|
|
||||||
glm::vec3 up, right;
|
part->position += glm::vec3{0, 1, 0} * (float)deltaY * 0.01f;
|
||||||
up = rot * glm::vec3{0, 1, 0};
|
part->position.y = std::clamp(part->position.y, 0.0f, 10.0f);
|
||||||
right = rot * glm::vec3{1, 0, 0};
|
|
||||||
|
|
||||||
part->position += up * (float)deltaY * 0.01f;
|
|
||||||
part->position += right * (float)deltaX * 0.01f;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
part->lastX = mouseEvent->x();
|
part->lastX = mouseEvent->position().x();
|
||||||
part->lastY = mouseEvent->y();
|
part->lastY = mouseEvent->position().y();
|
||||||
}
|
}
|
||||||
} break;
|
} break;
|
||||||
case QEvent::Wheel: {
|
case QEvent::Wheel: {
|
||||||
auto scrollEvent = dynamic_cast<QWheelEvent*>(e);
|
auto scrollEvent = dynamic_cast<QWheelEvent*>(e);
|
||||||
|
|
||||||
part->cameraDistance -= scrollEvent->angleDelta().y() / 120.0f; // FIXME: why 120?
|
part->cameraDistance -= (scrollEvent->angleDelta().y() / 120.0f) * 0.1f; // FIXME: why 120?
|
||||||
|
part->cameraDistance = std::clamp(part->cameraDistance, 1.0f, 4.0f);
|
||||||
} break;
|
} break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,7 +35,7 @@ public:
|
||||||
CameraMode cameraMode = CameraMode::None;
|
CameraMode cameraMode = CameraMode::None;
|
||||||
float pitch = 0.0f;
|
float pitch = 0.0f;
|
||||||
float yaw = 0.0f;
|
float yaw = 0.0f;
|
||||||
float cameraDistance = 5.0f;
|
float cameraDistance = 2.0f;
|
||||||
glm::vec3 position{0, 0, 0};
|
glm::vec3 position{0, 0, 0};
|
||||||
|
|
||||||
std::unique_ptr<physis_Skeleton> skeleton;
|
std::unique_ptr<physis_Skeleton> skeleton;
|
||||||
|
|
Loading…
Add table
Reference in a new issue