38 Log::Warning(
"InputManager resource not found, cannot register camera callbacks");
71 catch (
const std::exception &e)
73 Log::Error(fmt::format(
"Exception in DefaultBehavior destructor: {}", e.what()));
88 float speed,
float deltaTime)
90 glm::vec3 movement(0.0f);
112 movement += glm::vec3(0.0f, 1.0f, 0.0f);
116 movement -= glm::vec3(0.0f, 1.0f, 0.0f);
119 if (glm::length(movement) > 0.0f)
121 movement = glm::normalize(movement);
129 constexpr int PS5_L3_LR_AXIS = 0;
130 constexpr int PS5_L3_UD_AXIS = 1;
131 constexpr int PS5_R3_LR_AXIS = 2;
132 constexpr int PS5_L2_TRIGGER_AXIS = 3;
133 constexpr int PS5_R2_TRIGGER_AXIS = 4;
134 constexpr int PS5_R3_UD_AXIS = 5;
135 constexpr float JOYSTICK_DEADZONE = 0.15f;
136 constexpr float JOYSTICK_LOOK_SENSITIVITY = 2.0f;
140 if (!glfwJoystickPresent(joystickId))
146 const float *axes = glfwGetJoystickAxes(joystickId, &axesCount);
148 if (axes ==
nullptr || axesCount < 6)
153 glm::vec3 movement(0.0f);
157 float horizontalAxis = axes[PS5_L3_LR_AXIS];
158 float verticalAxis = axes[PS5_L3_UD_AXIS];
159 float l2Trigger = axes[PS5_L2_TRIGGER_AXIS];
160 float r2Trigger = axes[PS5_R2_TRIGGER_AXIS];
162 if (std::abs(horizontalAxis) > JOYSTICK_DEADZONE)
164 movement += right * horizontalAxis;
166 if (std::abs(verticalAxis) > JOYSTICK_DEADZONE)
168 movement -= forward * verticalAxis;
171 float upMovement = (r2Trigger + 1.0f) / 2.0f;
172 float downMovement = (l2Trigger + 1.0f) / 2.0f;
174 if (upMovement > JOYSTICK_DEADZONE)
176 movement += glm::vec3(0.0f, 1.0f, 0.0f) * upMovement;
178 if (downMovement > JOYSTICK_DEADZONE)
180 movement -= glm::vec3(0.0f, 1.0f, 0.0f) * downMovement;
183 if (glm::length(movement) > 0.0f)
185 movement = glm::normalize(movement);
189 float lookHorizontal = axes[PS5_R3_LR_AXIS];
190 float lookVertical = axes[PS5_R3_UD_AXIS];
192 if (std::abs(lookHorizontal) <= JOYSTICK_DEADZONE)
194 lookHorizontal = 0.0f;
196 if (std::abs(lookVertical) <= JOYSTICK_DEADZONE)
201 if (lookHorizontal != 0.0f || lookVertical != 0.0f)
203 float yaw = lookHorizontal * JOYSTICK_LOOK_SENSITIVITY * deltaTime;
204 float pitch = lookVertical * JOYSTICK_LOOK_SENSITIVITY * deltaTime;
206 glm::quat currentRotation = transform.
GetRotation();
215 if (button != GLFW_MOUSE_BUTTON_RIGHT)
222 Log::Warning(
"CameraManager resource not found in MouseButtonCallback");
228 if (action == GLFW_PRESS)
230 cameraManager.SetMouseDragging(
true);
231 if (cameraManager.HasValidCamera())
233 auto entity = cameraManager.GetActiveCamera();
235 cameraManager.SetOriginRotation(transform.GetRotation());
238 else if (action == GLFW_RELEASE)
240 cameraManager.SetMouseDragging(
false);
248 Log::Warning(
"CameraManager resource not found in CursorPosCallback");
254 Log::Warning(
"Window resource not found in CursorPosCallback");
261 bool shouldRotate = (window.IsCursorMasked() || cameraManager.
IsMouseDragging()) &&
262 cameraManager.HasValidCamera() &&
268 auto yaw =
static_cast<float>((xpos - cameraManager.GetLastMouseX()) * cameraManager.GetMouseSensitivity());
270 static_cast<float>((ypos - cameraManager.GetLastMouseY()) * cameraManager.GetMouseSensitivity());
273 transform.SetRotation(newRotation);
274 cameraManager.SetOriginRotation(newRotation);
277 cameraManager.SetLastMousePosition(xpos, ypos);
278 cameraManager.SetWasCursorMasked(window.IsCursorMasked());
CameraManager is a resource that manages the active camera entity.
Definition CameraManager.hpp:24
bool IsMouseDragging() const
Check if the mouse is being dragged.
Definition CameraManager.hpp:156
float GetMovementSpeed() const
Get the movement speed of the camera.
Definition CameraManager.hpp:102
int GetJoystickId() const
Get the joystick ID used for camera control.
Definition CameraManager.hpp:205
bool WasCursorMasked() const
Check if the cursor was masked in the previous frame.
Definition CameraManager.hpp:184
~DefaultBehavior() override
Definition DefaultBehavior.hpp:47
static void MouseButtonCallback(Engine::Core &core, int button, int action, int)
Definition DefaultBehavior.hpp:213
void HandleTranslation(Input::Resource::InputManager &inputManager, Object::Component::Transform &transform, float speed, float deltaTime)
Definition DefaultBehavior.hpp:87
FunctionUtils::FunctionID _cursorPosCallbackId
Definition DefaultBehavior.hpp:283
Engine::Core * _core
Definition DefaultBehavior.hpp:281
FunctionUtils::FunctionID _mouseButtonCallbackId
Definition DefaultBehavior.hpp:282
void HandleJoystickInput(Resource::CameraManager &manager, Object::Component::Transform &transform, float deltaTime)
Definition DefaultBehavior.hpp:126
static void CursorPosCallback(Engine::Core &core, double xpos, double ypos)
Definition DefaultBehavior.hpp:244
DefaultBehavior()=default
DefaultBehavior(Engine::Core &core)
Construct and register camera-related input callbacks.
Definition DefaultBehavior.hpp:34
void Update(Engine::Core &core, Resource::CameraManager &manager, Object::Component::Transform &transform, Object::Component::Camera &camera, float deltaTime) override
Update the camera behavior.
Definition DefaultBehavior.hpp:77
ICameraBehavior()=default
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.inl:14
bool HasResource() const
Check if a resource is registered in the core.
Definition Core.inl:24
Registry & GetRegistry()
Get the entt::registry that contains all components. It should be used to update component through sy...
Definition Core.hpp:50
Definition CameraManager.hpp:12
glm::quat RotateQuaternion(const glm::quat ¤t, float pitch, float yaw)
Apply pitch and yaw rotation to a quaternion.
Definition CameraUtils.hpp:41
std::size_t FunctionID
FunctionID class to represent a unique identifier for functions.
Definition FunctionID.hpp:9
void Warning(const T &msg) noexcept
Definition Logger.hpp:49
void Error(const T &msg) noexcept
Definition Logger.hpp:51