Camera using CameraMan with working keys

This commit is contained in:
2026-03-31 23:45:59 +03:00
parent e5f4bbfb90
commit abd961ea0f
2 changed files with 75 additions and 68 deletions

View File

@@ -14,7 +14,6 @@ EditorCamera::EditorCamera(Ogre::SceneManager *sceneMgr,
, m_pitch(-20.0f)
, m_rotating(false)
, m_panning(false)
, m_zooming(false)
, m_fpsMode(false)
, m_lastMouseX(0)
, m_lastMouseY(0)
@@ -44,6 +43,13 @@ EditorCamera::EditorCamera(Ogre::SceneManager *sceneMgr,
Ogre::Viewport *vp = window->addViewport(m_camera);
vp->setBackgroundColour(Ogre::ColourValue(0.1f, 0.1f, 0.1f));
// Setup CameraMan for proper camera control (no roll)
m_cameraMan = std::make_unique<OgreBites::CameraMan>(m_cameraNode);
m_cameraMan->setStyle(OgreBites::CS_ORBIT);
m_cameraMan->setTarget(m_targetNode);
m_cameraMan->setYawPitchDist(Ogre::Degree(m_yaw), Ogre::Degree(m_pitch),
m_distance);
updateCameraPosition();
}
@@ -54,7 +60,52 @@ void EditorCamera::update(float deltaTime)
if (m_fpsMode) {
updateFPSMovement(deltaTime);
}
updateCameraPosition();
// CameraMan handles the positioning
m_cameraMan->setYawPitchDist(Ogre::Degree(m_yaw), Ogre::Degree(m_pitch),
m_distance);
m_cameraMan->frameRendered(Ogre::FrameEvent{ deltaTime });
}
void EditorCamera::updateFPSMovement(float deltaTime)
{
// Get camera's forward and right vectors from CameraMan's derived orientation
Ogre::Quaternion orientation = m_camera->getDerivedOrientation();
Ogre::Vector3 forward = orientation * Ogre::Vector3::UNIT_Z;
Ogre::Vector3 right = orientation * Ogre::Vector3::UNIT_X;
Ogre::Vector3 up = Ogre::Vector3::UNIT_Y; // World up for Q/E
// Flatten forward vector to horizontal plane for WSAD movement
Ogre::Vector3 forwardHorizontal =
Ogre::Vector3(forward.x, 0, forward.z);
if (forwardHorizontal.squaredLength() > 0.0001f) {
forwardHorizontal.normalise();
}
Ogre::Vector3 movement = Ogre::Vector3::ZERO;
// WSAD movement (horizontal plane)
if (m_keyW)
movement -= forwardHorizontal;
if (m_keyS)
movement += forwardHorizontal;
if (m_keyA)
movement -= right;
if (m_keyD)
movement += right;
// Q/E for vertical movement
if (m_keyQ)
movement -= up;
if (m_keyE)
movement += up;
// Apply movement
if (movement.squaredLength() > 0.0001f) {
movement.normalise();
m_target += movement * FPS_SPEED * deltaTime;
m_targetNode->setPosition(m_target);
m_cameraMan->setTarget(m_targetNode);
}
}
void EditorCamera::handleMouseMove(const OgreBites::MouseMotionEvent &evt)
@@ -86,6 +137,8 @@ void EditorCamera::handleMouseMove(const OgreBites::MouseMotionEvent &evt)
up * (dy * PAN_SPEED * m_distance);
m_target += pan;
m_targetNode->setPosition(m_target);
m_cameraMan->setTarget(m_targetNode);
}
}
@@ -99,8 +152,6 @@ void EditorCamera::handleMousePress(const OgreBites::MouseButtonEvent &evt)
m_fpsMode = true; // Enable FPS mode when right mouse is held
} else if (evt.button == OgreBites::BUTTON_MIDDLE) {
m_panning = true;
} else if (evt.button == OgreBites::BUTTON_LEFT) {
// Left click is for selection, handled by app
}
}
@@ -150,56 +201,18 @@ void EditorCamera::handleKeyboard(const OgreBites::KeyboardEvent &evt)
}
}
void EditorCamera::updateFPSMovement(float deltaTime)
{
// Get camera's forward and right vectors
Ogre::Quaternion orientation = m_camera->getDerivedOrientation();
Ogre::Vector3 forward = orientation * Ogre::Vector3::UNIT_Z;
Ogre::Vector3 right = orientation * Ogre::Vector3::UNIT_X;
Ogre::Vector3 up = Ogre::Vector3::UNIT_Y; // World up for Q/E
// Flatten forward vector to horizontal plane for WSAD movement
Ogre::Vector3 forwardHorizontal =
Ogre::Vector3(forward.x, 0, forward.z);
if (forwardHorizontal.squaredLength() > 0.0001f) {
forwardHorizontal.normalise();
}
Ogre::Vector3 movement = Ogre::Vector3::ZERO;
// WSAD movement (horizontal plane)
if (m_keyW)
movement -= forwardHorizontal;
if (m_keyS)
movement += forwardHorizontal;
if (m_keyA)
movement -= right;
if (m_keyD)
movement += right;
// Q/E for vertical movement
if (m_keyQ)
movement -= up;
if (m_keyE)
movement += up;
// Apply movement
if (movement.squaredLength() > 0.0001f) {
movement.normalise();
m_target += movement * FPS_SPEED * deltaTime;
}
}
void EditorCamera::focusOn(const Ogre::Vector3 &point)
{
m_target = point;
updateCameraPosition();
m_targetNode->setPosition(m_target);
m_cameraMan->setTarget(m_targetNode);
}
void EditorCamera::setPosition(const Ogre::Vector3 &pos)
{
m_target = pos;
updateCameraPosition();
m_targetNode->setPosition(m_target);
m_cameraMan->setTarget(m_targetNode);
}
Ogre::Ray EditorCamera::getMouseRay(float screenX, float screenY) const
@@ -217,20 +230,8 @@ Ogre::Ray EditorCamera::getMouseRay(float screenX, float screenY) const
void EditorCamera::updateCameraPosition()
{
// Calculate camera position based on spherical coordinates
float yawRad = Ogre::Degree(m_yaw).valueRadians();
float pitchRad = Ogre::Degree(m_pitch).valueRadians();
Ogre::Vector3 offset;
offset.x = m_distance * Ogre::Math::Cos(pitchRad) *
Ogre::Math::Sin(yawRad);
offset.y = m_distance * Ogre::Math::Sin(pitchRad);
offset.z = m_distance * Ogre::Math::Cos(pitchRad) *
Ogre::Math::Cos(yawRad);
m_position = m_target + offset;
m_cameraNode->setPosition(m_position);
m_cameraNode->lookAt(m_target, Ogre::Node::TS_WORLD);
// CameraMan handles the positioning
m_targetNode->setPosition(m_target);
m_cameraMan->setYawPitchDist(Ogre::Degree(m_yaw), Ogre::Degree(m_pitch),
m_distance);
}

View File

@@ -1,15 +1,17 @@
#ifndef EDITSCENE_EDITORCAMERA_HPP
#define EDITSCENE_EDITORCAMERA_HPP
#pragma once
#include <memory>
#include <Ogre.h>
#include <OgreCamera.h>
#include <OgreSceneManager.h>
#include <OgreRenderWindow.h>
#include <OgreInput.h>
#include <OgreCameraMan.h>
/**
* Simple editor camera controller
* Supports orbit and fly modes
* Editor camera controller using OgreBites::CameraMan
* Supports orbit (default) and FPS modes
*/
class EditorCamera {
public:
@@ -18,7 +20,7 @@ public:
~EditorCamera();
/**
* Update camera movement
* Update camera (called each frame)
*/
void update(float deltaTime);
@@ -50,6 +52,11 @@ public:
*/
Ogre::Ray getMouseRay(float screenX, float screenY) const;
/**
* Check if in FPS mode
*/
bool isFPSMode() const { return m_fpsMode; }
private:
void updateCameraPosition();
void updateFPSMovement(float deltaTime);
@@ -58,6 +65,9 @@ private:
Ogre::Camera *m_camera;
Ogre::SceneNode *m_cameraNode;
Ogre::SceneNode *m_targetNode;
// Use OgreBites::CameraMan for proper camera control
std::unique_ptr<OgreBites::CameraMan> m_cameraMan;
// Camera state
Ogre::Vector3 m_position;
@@ -69,7 +79,6 @@ private:
// Input state
bool m_rotating;
bool m_panning;
bool m_zooming;
bool m_fpsMode;
int m_lastMouseX;
int m_lastMouseY;
@@ -85,10 +94,7 @@ private:
// Movement speeds
static constexpr float ROTATION_SPEED = 0.3f;
static constexpr float PAN_SPEED = 0.01f;
static constexpr float ZOOM_SPEED = 0.5f;
static constexpr float FPS_SPEED = 10.0f;
static constexpr float MIN_DISTANCE = 1.0f;
static constexpr float MAX_DISTANCE = 1000.0f;
};
#endif // EDITSCENE_EDITORCAMERA_HPP