8#include <glm/gtc/quaternion.hpp>
27 Log::Warning(
"InputManager resource not found in core. OrbitalChaseBehavior will not function properly.");
32 Log::Warning(
"CameraManager resource not found in core. OrbitalChaseBehavior will not function properly.");
38 inputManager.RegisterMouseButtonCallback([
this](
Engine::Core &core,
int button,
int action,
int mods) {
39 HandleMouseButton(core, button, action, mods);
43 [
this](
Engine::Core &core,
double xpos,
double ypos) { HandleCursorPos(core, xpos, ypos); });
46 [
this](
Engine::Core &core,
double xoffset,
double yoffset) { HandleScroll(core, xoffset, yoffset); });
74 catch (
const std::exception &e)
78 Log::Error(fmt::format(
"Failed to unregister camera behavior callbacks: {}", e.what()));
80 catch (
const std::exception &logError)
95 Log::Warning(
"Camera target entity is not alive. OrbitalChaseBehavior will not update.");
123 Log::Warning(
"MinDistance of OrbitalChaseBehavior is greater than MaxDistance");
134 Log::Warning(
"MaxDistance of OrbitalChaseBehavior is less than MinDistance");
142 const auto &targetPos = targetTransform.
GetPosition();
143 const auto &targetRot = targetTransform.GetRotation();
146 glm::vec3 localOffset;
147 localOffset.x = horizontalDistance * std::sin(
_yaw);
149 localOffset.z = -horizontalDistance * std::cos(
_yaw);
151 glm::vec3 horizontalOffset = targetRot * glm::vec3(localOffset.x, 0.0f, localOffset.z);
152 glm::vec3 cameraPosition = targetPos + horizontalOffset + glm::vec3(0.0f, localOffset.y, 0.0f);
161 cameraTransform.
GetPosition(), targetPosition, glm::vec3(0.0f, 1.0f, 0.0f));
167 if (button == GLFW_MOUSE_BUTTON_RIGHT)
183 float sensitivity = cameraManager.GetMouseSensitivity() * 2.0f;
188 _yaw -= dx * sensitivity;
189 _pitch += dy * sensitivity;
191 _yaw = glm::mod(
_yaw, 2.0f * glm::pi<float>());
192 _pitch = glm::clamp(
_pitch, -glm::half_pi<float>() + 0.01f, glm::half_pi<float>() - 0.01f);
CameraManager is a resource that manages the active camera entity.
Definition CameraManager.hpp:24
void SetWasCursorMasked(bool masked)
Set whether the cursor was masked in the previous frame.
Definition CameraManager.hpp:177
bool WasCursorMasked() const
Check if the cursor was masked in the previous frame.
Definition CameraManager.hpp:184
Base interface for camera behaviors.
Definition CameraBehavior.hpp:18
void HandleScroll(Engine::Core &, double, double yoffset)
Definition OrbitalChaseBehavior.hpp:199
float GetMaxDistance() const
Definition OrbitalChaseBehavior.hpp:127
float _distance
Definition OrbitalChaseBehavior.hpp:210
FunctionUtils::FunctionID _cursorPosCallbackId
Definition OrbitalChaseBehavior.hpp:221
void SetMinDistance(float newMinDistance)
Definition OrbitalChaseBehavior.hpp:118
Engine::Entity _target
Definition OrbitalChaseBehavior.hpp:206
float GetMinDistance() const
Definition OrbitalChaseBehavior.hpp:116
float _scrollSensitivity
Definition OrbitalChaseBehavior.hpp:214
bool _hasWarnedInvalidTarget
Definition OrbitalChaseBehavior.hpp:224
float _minDistance
Definition OrbitalChaseBehavior.hpp:211
Engine::Entity GetTarget() const
Definition OrbitalChaseBehavior.hpp:108
void HandleCursorPos(Engine::Core &core, float xpos, float ypos)
Definition OrbitalChaseBehavior.hpp:173
void SetMaxDistance(float newMaxDistance)
Definition OrbitalChaseBehavior.hpp:129
bool _isDragging
Definition OrbitalChaseBehavior.hpp:216
Engine::Core * _core
Definition OrbitalChaseBehavior.hpp:205
void UpdateRotation(Object::Component::Transform &cameraTransform)
Definition OrbitalChaseBehavior.hpp:157
float _yaw
Definition OrbitalChaseBehavior.hpp:208
void Update(Engine::Core &core, CameraMovement::Resource::CameraManager &manager, Object::Component::Transform &cameraTransform, Object::Component::Camera &, float) override
Update the camera behavior.
Definition OrbitalChaseBehavior.hpp:86
FunctionUtils::FunctionID _scrollCallbackId
Definition OrbitalChaseBehavior.hpp:222
float GetScrollSensitivity() const
Definition OrbitalChaseBehavior.hpp:112
FunctionUtils::FunctionID _mouseButtonCallbackId
Definition OrbitalChaseBehavior.hpp:220
float _pitch
Definition OrbitalChaseBehavior.hpp:209
float _lastMouseY
Definition OrbitalChaseBehavior.hpp:218
void UpdatePosition(Object::Component::Transform &cameraTransform)
Definition OrbitalChaseBehavior.hpp:139
void SetScrollSensitivity(float newScrollSensitivity)
Definition OrbitalChaseBehavior.hpp:114
void HandleMouseButton(Engine::Core &, int button, int action, int)
Definition OrbitalChaseBehavior.hpp:165
void SetTarget(Engine::Entity newTarget)
Definition OrbitalChaseBehavior.hpp:110
float _maxDistance
Definition OrbitalChaseBehavior.hpp:212
OrbitalChaseBehavior(Engine::Core &core, Engine::Entity target)
Definition OrbitalChaseBehavior.hpp:23
float _lastMouseX
Definition OrbitalChaseBehavior.hpp:217
~OrbitalChaseBehavior() override
Definition OrbitalChaseBehavior.hpp:53
The core is the place where all the data of the engine is stored. It contains the registry (entities)...
Definition Core.hpp:33
TResource & GetResource()
Get a reference of a resource.
Definition Core.ipp:14
bool HasResource() const
Check if a resource is registered in the core.
Definition Core.ipp:24
Wrapper class providing a convenient interface for entity manipulation with the Core....
Definition Entity.hpp:20
Definition CameraManager.hpp:12
glm::quat ComputeLookAtQuaternion(const glm::vec3 &eye, const glm::vec3 &target, const glm::vec3 &up)
Compute a quaternion that represents a "look at" rotation.
Definition CameraUtils.hpp:19
std::size_t FunctionID
FunctionID class to represent a unique identifier for functions.
Definition FunctionID.hpp:9
void Error(const T &msg) noexcept(false)
Definition Logger.hpp:34
void Warning(const T &msg) noexcept(false)
Definition Logger.hpp:32