ARTICLE AD BOX
I'm working on the rotate function for a camera in a custom game engine that I am making for fun.
void Camera::rotate(const int deltaX, const int deltaY, const float sensitivity) { // Update yaw and pitch based on input this->yaw -= static_cast<float>(deltaX) * sensitivity; this->pitch -= static_cast<float>(deltaY) * sensitivity; // Important! Do not clamp the pitch in the same line as you subtract it this->pitch = glm::clamp(this->pitch, -pitchLimit, pitchLimit); // Create a new rotation quaternion const glm::quat yawQuat = glm::angleAxis(this->yaw, Math::LinearAlgebra::Vector3::UP); const glm::quat pitchQuat = glm::angleAxis(this->pitch, Math::LinearAlgebra::Vector3::RIGHT); // Combine yaw and pitch rotations // Apply the rotation to the camera's transform this->getTransform()->getRotation() = glm::normalize(yawQuat * pitchQuat); }This works, however I want also to be able to apply this to other components. Is there a way I can do it without the yaw and pitch variables? Can I alter it through the quaternion getRotation() itself?
// // Created by miles on 11/3/2024. // #pragma once #include "Component.hpp" /// @class Transform Transform.hpp /// @brief Manages position, rotation, and scale of an Entity. class Transform final : public Component { public: /// @brief Overloaded constructor /// @param entity explicit Transform(Entity *entity); /// @brief Overloaded constructor /// @param entity /// @param position /// @param rotation /// @param scale Transform(Entity *entity, glm::vec3 position, glm::quat rotation, glm::vec3 scale); /// @brief Copy constructor /// @param toCopy the Transform to copy Transform(const Transform &toCopy) = default; /// @brief Move constructor /// @param toMove the Transform to move Transform(Transform &&toMove) noexcept = default; /// @brief Copy assignment operator /// @param rhs right hand side /// @return a reference to the Transform Transform &operator=(const Transform &rhs) = default; /// @brief Move assignment operator /// @param rhs right hand side /// @return a reference to the Transform Transform &operator=(Transform &&rhs) noexcept = default; /// @brief Destructor ~Transform() override = default; /// @brief Overrides Component::update void update(); /// @brief Attaches a child to the Transform /// @param child void attachChild(Transform *child); /// @brief Detaches a child from the Transform /// @param index void detachChild(long long index); /// @brief Removes all children from the Transform void detachChildren(); /// @brief Getter for local matrix /// @return the local matrix [[nodiscard]] glm::mat4 getLocalMatrix() const; /// @brief Getter for world matrix /// @return the world matrix [[nodiscard]] glm::mat4 getWorldMatrix() const; [[nodiscard]] glm::vec3 forward() const; [[nodiscard]] glm::vec3 back() const; [[nodiscard]] glm::vec3 left() const; [[nodiscard]] glm::vec3 right() const; [[nodiscard]] glm::vec3 up() const; [[nodiscard]] glm::vec3 down() const; /// @brief Getter for position /// @return a reference to the position glm::vec3 &getPosition(); /// @brief Getter for position /// @return a const reference to the position [[nodiscard]] const glm::vec3 &getPosition() const; /// @brief Getter for rotation /// @return a reference to the rotation glm::quat &getRotation(); /// @brief Getter for rotation /// @return a const reference to the rotation [[nodiscard]] const glm::quat &getRotation() const; /// @brief Getter for scale /// @return a reference to the scale glm::vec3 &getScale(); /// @brief Getter for scale /// @return a const reference to the scale [[nodiscard]] const glm::vec3 &getScale() const; [[nodiscard]] const Transform *getParent() const; void setParent(Transform *value); [[nodiscard]] Transform *getChild(std::size_t index) const; /// @brief Getter for number of children /// @return the number of children this Transform has [[nodiscard]] std::size_t childCount() const noexcept; /// @brief Enables iteration /// @return the beginning iterator std::vector<Transform *>::iterator begin(); /// @brief Enables const iteration /// @return the beginning iterator [[nodiscard]] std::vector<Transform *>::const_iterator begin() const noexcept; /// @brief Enables reverse iteration /// @return the beginning reverse iterator std::vector<Transform *>::reverse_iterator rbegin(); /// @brief Enables reverse const iteration /// @return the beginning reverse iterator [[nodiscard]] std::vector<Transform *>::const_reverse_iterator rbegin() const noexcept; /// @brief Enables iteration /// @return the ending iterator std::vector<Transform *>::iterator end(); /// @brief Enables const iteration /// @return the ending iterator [[nodiscard]] std::vector<Transform *>::const_iterator end() const noexcept; /// @brief Enables reverse iteration /// @return the ending reverse iterator std::vector<Transform *>::reverse_iterator rend(); /// @brief Enables reverse const iteration /// @return the ending reverse iterator [[nodiscard]] std::vector<Transform *>::const_reverse_iterator rend() const noexcept; Transform *operator[](std::size_t index) const; friend std::ostream &operator<<(std::ostream &stream, const Transform &rhs); private: Transform *parent; std::vector<Transform *> children; glm::vec3 position; glm::quat rotation; glm::vec3 scale; };