aboutsummaryrefslogtreecommitdiff
path: root/src/shared/math
diff options
context:
space:
mode:
authorNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-12 21:57:46 +1100
committerNicolas James <Eele1Ephe7uZahRie@tutanota.com>2025-02-12 21:57:46 +1100
commite4483eca01b48b943cd0461e24a74ae1a3139ed4 (patch)
treeed58c3c246e3af1af337697695d780aa31f6ad9a /src/shared/math
parent1cc08c51eb4b0f95c30c0a98ad1fc5ad3459b2df (diff)
Update to most recent version (old initial commit)
Diffstat (limited to 'src/shared/math')
-rw-r--r--src/shared/math/angles.cc57
-rw-r--r--src/shared/math/angles.hh44
-rw-r--r--src/shared/math/coords.cc30
-rw-r--r--src/shared/math/coords.hh31
-rw-r--r--src/shared/math/math.cc5
-rw-r--r--src/shared/math/math.hh22
6 files changed, 189 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
diff --git a/src/shared/math/angles.hh b/src/shared/math/angles.hh
new file mode 100644
index 0000000..aec388f
--- /dev/null
+++ b/src/shared/math/angles.hh
@@ -0,0 +1,44 @@
+#ifndef SHARED_MATH_ANGLES_HH_
+#define SHARED_MATH_ANGLES_HH_
+
+#include <algorithm>
+#include <compare>
+
+#include <glm/glm.hpp>
+
+namespace shared {
+namespace math {
+
+struct angles {
+public:
+ float pitch;
+ float yaw;
+
+public:
+ // Returns the closest yaw from this to other, assumes they are normalised.
+ static float get_yaw_delta(const float& a, const float& b) noexcept;
+
+public:
+ glm::vec3 to_dir() const noexcept;
+
+public:
+ // Removes bad floating point values!
+ // Clamps pitch to its max positions of += glm::radians(89).
+ // Clamps yaw to its max positions of += glm::radians(180).
+ angles& clamp() noexcept;
+
+ // Returns yaw angles to [glm::radians(-180), glm::radians(180)].
+ angles& normalise() noexcept;
+
+public:
+ angles operator+(const angles& a) const noexcept;
+ angles operator*(const float& f) const noexcept;
+
+public:
+ auto operator<=>(const angles&) const noexcept = default;
+};
+
+} // namespace math
+} // namespace shared
+
+#endif
diff --git a/src/shared/math/coords.cc b/src/shared/math/coords.cc
new file mode 100644
index 0000000..37fb19a
--- /dev/null
+++ b/src/shared/math/coords.cc
@@ -0,0 +1,30 @@
+#include "shared/math/coords.hh"
+
+namespace shared {
+namespace math {
+
+bool coords::is_inside_draw(const coords& a, const coords& b,
+ const std::int32_t draw_distance) noexcept {
+ const auto x2 = (a.x - b.x) * (a.x - b.x);
+ const auto z2 = (a.z - b.z) * (a.z - b.z);
+ const auto dd2 = draw_distance * draw_distance;
+
+ return (x2 < dd2 - z2) && (z2 < dd2 - x2);
+}
+
+coords coords::operator+(const coords& c) const noexcept {
+ auto ret = *this;
+ ret.x += c.x;
+ ret.z += c.z;
+ return ret;
+}
+
+coords coords::operator-(const coords& c) const noexcept {
+ auto ret = *this;
+ ret.x -= c.x;
+ ret.z -= c.z;
+ return ret;
+}
+
+} // namespace math
+} // namespace shared
diff --git a/src/shared/math/coords.hh b/src/shared/math/coords.hh
new file mode 100644
index 0000000..7a21bf9
--- /dev/null
+++ b/src/shared/math/coords.hh
@@ -0,0 +1,31 @@
+#ifndef SHARED_MATH_COORDS_HH_
+#define SHARED_MATH_COORDS_HH_
+
+#include <compare>
+#include <cstdint>
+
+namespace shared {
+namespace math {
+
+// 2D coordinates.
+struct coords {
+public:
+ std::int32_t x;
+ std::int32_t z;
+
+public:
+ static bool is_inside_draw(const coords& a, const coords& b,
+ const std::int32_t draw_distance) noexcept;
+
+public:
+ coords operator+(const coords& c) const noexcept;
+ coords operator-(const coords& c) const noexcept;
+
+public:
+ auto operator<=>(const coords&) const noexcept = default;
+};
+
+} // namespace math
+} // namespace shared
+
+#endif
diff --git a/src/shared/math/math.cc b/src/shared/math/math.cc
new file mode 100644
index 0000000..407e202
--- /dev/null
+++ b/src/shared/math/math.cc
@@ -0,0 +1,5 @@
+#include "shared/math/math.hh"
+
+namespace shared {
+namespace math {} // namespace math
+} // namespace shared
diff --git a/src/shared/math/math.hh b/src/shared/math/math.hh
new file mode 100644
index 0000000..9eef88c
--- /dev/null
+++ b/src/shared/math/math.hh
@@ -0,0 +1,22 @@
+#ifndef SHARED_MATH_MATH_HH_
+#define SHARED_MATH_MATH_HH_
+
+#include <algorithm>
+#include <cmath>
+
+#include <glm/glm.hpp>
+#include <glm/gtc/matrix_transform.hpp>
+#include <glm/gtc/type_ptr.hpp>
+
+#include "shared/math/angles.hh"
+#include "shared/math/coords.hh"
+
+namespace shared {
+namespace math {
+
+// Convenience include, might include others later too.
+
+} // namespace math
+} // namespace shared
+
+#endif