diff options
| author | Nicolas James <Eele1Ephe7uZahRie@tutanota.com> | 2025-02-12 21:57:46 +1100 |
|---|---|---|
| committer | Nicolas James <Eele1Ephe7uZahRie@tutanota.com> | 2025-02-12 21:57:46 +1100 |
| commit | e4483eca01b48b943cd0461e24a74ae1a3139ed4 (patch) | |
| tree | ed58c3c246e3af1af337697695d780aa31f6ad9a /src/shared/math/angles.cc | |
| parent | 1cc08c51eb4b0f95c30c0a98ad1fc5ad3459b2df (diff) | |
Update to most recent version (old initial commit)
Diffstat (limited to 'src/shared/math/angles.cc')
| -rw-r--r-- | src/shared/math/angles.cc | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/shared/math/angles.cc b/src/shared/math/angles.cc new file mode 100644 index 0000000..9343dc3 --- /dev/null +++ b/src/shared/math/angles.cc @@ -0,0 +1,57 @@ +#include "shared/math/angles.hh" + +namespace shared { +namespace math { + +float angles::get_yaw_delta(const float& a, const float& b) noexcept { + const float inner = glm::distance(a, b); + const float outer = glm::radians(360.0f) - inner; + + const bool less = a < b; + return inner <= outer ? (less ? inner : -inner) : (less ? -outer : outer); +} + +glm::vec3 angles::to_dir() const noexcept { + const float x = std::cos(this->pitch) * std::cos(this->yaw); + const float y = std::sin(this->pitch); + const float z = std::cos(this->pitch) * std::sin(this->yaw); + return {x, y, z}; +} + +angles& angles::clamp() noexcept { + this->pitch = + std::clamp(this->pitch, glm::radians(-89.0f), glm::radians(89.0f)); + this->yaw = + std::clamp(this->yaw, glm::radians(-180.0f), glm::radians(180.0f)); + return *this; +} + +angles& angles::normalise() noexcept { + this->yaw = std::fmod(this->yaw, glm::radians(360.0f)); + + if (const auto fmod = std::fmod(this->yaw, glm::radians(180.0f)); + fmod != this->yaw) { + + this->yaw = fmod + (std::signbit(this->yaw) ? glm::radians(180.0f) + : glm::radians(-180.0f)); + } + + return *this; +} + +angles angles::operator+(const angles& a) const noexcept { + auto ret = *this; + ret.pitch += a.pitch; + ret.yaw += a.yaw; + return ret; +} + +angles angles::operator*(const float& f) const noexcept { + auto ret = *this; + ret.pitch *= f; + ret.yaw *= f; + return ret; +} + +} // namespace math +} // namespace shared |
