Golias Engine
A C++ 20 'game engine' built with SDL3 with wide platform support.
math.h
Go to the documentation of this file.
1 #pragma once
2 #define GLM_ENABLE_EXPERIMENTAL
3 #define GLM_FORCE_INTRINSICS
4 #include "core/math/vector2.h"
5 #include "core/math/vector3.h"
6 #include "core/math/vector4.h"
7 #include "core/math/quaternion.h"
8 #include "core/math/basis.h"
9 #include "core/math/matrix4.h"
10 
11 #include <glm/gtc/type_ptr.hpp>
12 
13 namespace math {
14  constexpr float PI = 3.14159265358979323846f;
15  constexpr float TAU = 6.28318530717958647692f;
16  constexpr float E = 2.71828182845904523536f;
17  constexpr float DEG2RAD = PI / 180.0f;
18  constexpr float RAD2DEG = 180.0f / PI;
19  constexpr float EPSILON = 1e-6f;
20  constexpr float INF = __builtin_huge_valf();
21 
22  float sin(float x);
23  float cos(float x);
24  float tan(float x);
25  float asin(float x);
26  float acos(float x);
27  float atan(float x);
28  float atan2(float y, float x);
29 
30  float sinh(float x);
31  float cosh(float x);
32  float tanh(float x);
33 
34  float exp(float x);
35  float log(float x);
36  float log2(float x);
37  float log10(float x);
38  float pow(float base, float exp);
39  float sqrt(float x);
40  float cbrt(float x);
41 
42  float floor(float x);
43  float ceil(float x);
44  float round(float x);
45  float trunc(float x);
46 
47  template<typename T>
48  inline T min(T a, T b) { return (a < b) ? a : b; }
49 
50  template<typename T>
51  inline T max(T a, T b) { return (a > b) ? a : b; }
52 
53  template<typename T>
54  inline T clamp(T value, T min_val, T max_val) {
55  return min(max(value, min_val), max_val);
56  }
57 
58  float abs(float x);
59  int abs(int x);
60 
61  template<typename T>
62  inline T sign(T x) { return (x > T(0)) ? T(1) : ((x < T(0)) ? T(-1) : T(0)); }
63 
64  template<typename T>
65  inline T lerp(T a, T b, float t) {
66  return a + (b - a) * t;
67  }
68 
69  template<typename T>
70  inline T smoothstep(T edge0, T edge1, T x) {
71  T t = clamp((x - edge0) / (edge1 - edge0), T(0), T(1));
72  return t * t * (T(3) - T(2) * t);
73  }
74 
75  float deg2rad(float degrees);
76  float rad2deg(float radians);
77 
78  bool is_zero_approx(float x);
79  bool is_equal_approx(float a, float b);
80 
81  float random(float min_val, float max_val);
82  int random(int min_val, int max_val);
83 
84  template<typename T>
85  inline T move_toward(T current, T target, T delta) {
86  if (abs(target - current) <= delta) {
87  return target;
88  }
89  return current + sign(target - current) * delta;
90  }
91 
92  float snap(float value, float step);
93 
94 }
95 
96 
97 
Definition: math.cpp:6
bool is_zero_approx(float x)
Definition: math.cpp:115
float snap(float value, float step)
Definition: math.cpp:134
T clamp(T value, T min_val, T max_val)
Definition: math.h:54
T move_toward(T current, T target, T delta)
Definition: math.h:85
float log(float x)
Definition: math.cpp:53
constexpr float PI
Definition: math.h:14
constexpr float EPSILON
Definition: math.h:19
float cbrt(float x)
Definition: math.cpp:73
float atan2(float y, float x)
Definition: math.cpp:33
float cos(float x)
Definition: math.cpp:13
float tan(float x)
Definition: math.cpp:17
constexpr float E
Definition: math.h:16
float random(float min_val, float max_val)
Definition: math.cpp:125
float asin(float x)
Definition: math.cpp:21
float deg2rad(float degrees)
Definition: math.cpp:105
float log10(float x)
Definition: math.cpp:61
float acos(float x)
Definition: math.cpp:25
float cosh(float x)
Definition: math.cpp:41
float floor(float x)
Definition: math.cpp:78
float exp(float x)
Definition: math.cpp:49
constexpr float RAD2DEG
Definition: math.h:18
T max(T a, T b)
Definition: math.h:51
float log2(float x)
Definition: math.cpp:57
T smoothstep(T edge0, T edge1, T x)
Definition: math.h:70
float sin(float x)
Definition: math.cpp:9
float rad2deg(float radians)
Definition: math.cpp:109
T sign(T x)
Definition: math.h:62
constexpr float DEG2RAD
Definition: math.h:17
float trunc(float x)
Definition: math.cpp:90
float abs(float x)
Definition: math.cpp:96
constexpr float INF
Definition: math.h:20
constexpr float TAU
Definition: math.h:15
float ceil(float x)
Definition: math.cpp:82
float tanh(float x)
Definition: math.cpp:45
float atan(float x)
Definition: math.cpp:29
float sinh(float x)
Definition: math.cpp:37
float sqrt(float x)
Definition: math.cpp:69
float pow(float base, float exp)
Definition: math.cpp:65
T min(T a, T b)
Definition: math.h:48
bool is_equal_approx(float a, float b)
Definition: math.cpp:119
T lerp(T a, T b, float t)
Definition: math.h:65
float round(float x)
Definition: math.cpp:86