Vector3.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #ifndef VECTOR3_H
  2. #define VECTOR3_H
  3. #include <gdnative/vector3.h>
  4. #include "Defs.hpp"
  5. #include "String.hpp"
  6. #include <Math.hpp>
  7. namespace godot {
  8. class Basis;
  9. struct Vector3 {
  10. enum Axis {
  11. AXIS_X,
  12. AXIS_Y,
  13. AXIS_Z,
  14. };
  15. union {
  16. struct {
  17. real_t x;
  18. real_t y;
  19. real_t z;
  20. };
  21. real_t coord[3]; // Not for direct access, use [] operator instead
  22. };
  23. inline Vector3(real_t x, real_t y, real_t z) {
  24. this->x = x;
  25. this->y = y;
  26. this->z = z;
  27. }
  28. inline Vector3() {
  29. this->x = 0;
  30. this->y = 0;
  31. this->z = 0;
  32. }
  33. inline const real_t &operator[](int p_axis) const {
  34. return coord[p_axis];
  35. }
  36. inline real_t &operator[](int p_axis) {
  37. return coord[p_axis];
  38. }
  39. inline Vector3 &operator+=(const Vector3 &p_v) {
  40. x += p_v.x;
  41. y += p_v.y;
  42. z += p_v.z;
  43. return *this;
  44. }
  45. inline Vector3 operator+(const Vector3 &p_v) const {
  46. Vector3 v = *this;
  47. v += p_v;
  48. return v;
  49. }
  50. inline Vector3 &operator-=(const Vector3 &p_v) {
  51. x -= p_v.x;
  52. y -= p_v.y;
  53. z -= p_v.z;
  54. return *this;
  55. }
  56. inline Vector3 operator-(const Vector3 &p_v) const {
  57. Vector3 v = *this;
  58. v -= p_v;
  59. return v;
  60. }
  61. inline Vector3 &operator*=(const Vector3 &p_v) {
  62. x *= p_v.x;
  63. y *= p_v.y;
  64. z *= p_v.z;
  65. return *this;
  66. }
  67. inline Vector3 operator*(const Vector3 &p_v) const {
  68. Vector3 v = *this;
  69. v *= p_v;
  70. return v;
  71. }
  72. inline Vector3 &operator/=(const Vector3 &p_v) {
  73. x /= p_v.x;
  74. y /= p_v.y;
  75. z /= p_v.z;
  76. return *this;
  77. }
  78. inline Vector3 operator/(const Vector3 &p_v) const {
  79. Vector3 v = *this;
  80. v /= p_v;
  81. return v;
  82. }
  83. inline Vector3 &operator*=(real_t p_scalar) {
  84. *this *= Vector3(p_scalar, p_scalar, p_scalar);
  85. return *this;
  86. }
  87. inline Vector3 operator*(real_t p_scalar) const {
  88. Vector3 v = *this;
  89. v *= p_scalar;
  90. return v;
  91. }
  92. inline Vector3 &operator/=(real_t p_scalar) {
  93. *this /= Vector3(p_scalar, p_scalar, p_scalar);
  94. return *this;
  95. }
  96. inline Vector3 operator/(real_t p_scalar) const {
  97. Vector3 v = *this;
  98. v /= p_scalar;
  99. return v;
  100. }
  101. inline Vector3 operator-() const {
  102. return Vector3(-x, -y, -z);
  103. }
  104. inline bool operator==(const Vector3 &p_v) const {
  105. return (x == p_v.x && y == p_v.y && z == p_v.z);
  106. }
  107. inline bool operator!=(const Vector3 &p_v) const {
  108. return (x != p_v.x || y != p_v.y || z != p_v.z);
  109. }
  110. bool operator<(const Vector3 &p_v) const;
  111. bool operator<=(const Vector3 &p_v) const;
  112. inline Vector3 abs() const {
  113. return Vector3(::fabs(x), ::fabs(y), ::fabs(z));
  114. }
  115. inline Vector3 ceil() const {
  116. return Vector3(::ceil(x), ::ceil(y), ::ceil(z));
  117. }
  118. inline Vector3 cross(const Vector3 &b) const {
  119. Vector3 ret(
  120. (y * b.z) - (z * b.y),
  121. (z * b.x) - (x * b.z),
  122. (x * b.y) - (y * b.x));
  123. return ret;
  124. }
  125. inline Vector3 linear_interpolate(const Vector3 &p_b, real_t p_t) const {
  126. return Vector3(
  127. x + (p_t * (p_b.x - x)),
  128. y + (p_t * (p_b.y - y)),
  129. z + (p_t * (p_b.z - z)));
  130. }
  131. inline Vector3 slerp(const Vector3 &p_b, real_t p_t) const {
  132. real_t theta = angle_to(p_b);
  133. return rotated(cross(p_b).normalized(), theta * p_t);
  134. }
  135. Vector3 cubic_interpolate(const Vector3 &b, const Vector3 &pre_a, const Vector3 &post_b, const real_t t) const;
  136. Vector3 move_toward(const Vector3 &p_to, const real_t p_delta) const {
  137. Vector3 v = *this;
  138. Vector3 vd = p_to - v;
  139. real_t len = vd.length();
  140. return len <= p_delta || len < CMP_EPSILON ? p_to : v + vd / len * p_delta;
  141. }
  142. Vector3 bounce(const Vector3 &p_normal) const {
  143. return -reflect(p_normal);
  144. }
  145. inline real_t length() const {
  146. real_t x2 = x * x;
  147. real_t y2 = y * y;
  148. real_t z2 = z * z;
  149. return ::sqrt(x2 + y2 + z2);
  150. }
  151. inline real_t length_squared() const {
  152. real_t x2 = x * x;
  153. real_t y2 = y * y;
  154. real_t z2 = z * z;
  155. return x2 + y2 + z2;
  156. }
  157. inline real_t distance_squared_to(const Vector3 &b) const {
  158. return (b - *this).length_squared();
  159. }
  160. inline real_t distance_to(const Vector3 &b) const {
  161. return (b - *this).length();
  162. }
  163. inline real_t dot(const Vector3 &b) const {
  164. return x * b.x + y * b.y + z * b.z;
  165. }
  166. inline Vector3 project(const Vector3 &p_b) const {
  167. return p_b * (dot(p_b) / p_b.length_squared());
  168. }
  169. inline real_t angle_to(const Vector3 &b) const {
  170. return std::atan2(cross(b).length(), dot(b));
  171. }
  172. inline Vector3 direction_to(const Vector3 &p_b) const {
  173. Vector3 ret(p_b.x - x, p_b.y - y, p_b.z - z);
  174. ret.normalize();
  175. return ret;
  176. }
  177. inline Vector3 floor() const {
  178. return Vector3(::floor(x), ::floor(y), ::floor(z));
  179. }
  180. inline Vector3 inverse() const {
  181. return Vector3(1.f / x, 1.f / y, 1.f / z);
  182. }
  183. inline bool is_normalized() const {
  184. return std::abs(length_squared() - 1.f) < 0.00001f;
  185. }
  186. Basis outer(const Vector3 &b) const;
  187. int max_axis() const;
  188. int min_axis() const;
  189. inline void normalize() {
  190. real_t l = length();
  191. if (l == 0) {
  192. x = y = z = 0;
  193. } else {
  194. x /= l;
  195. y /= l;
  196. z /= l;
  197. }
  198. }
  199. inline Vector3 normalized() const {
  200. Vector3 v = *this;
  201. v.normalize();
  202. return v;
  203. }
  204. inline Vector3 reflect(const Vector3 &p_normal) const {
  205. return -(*this - p_normal * this->dot(p_normal) * 2.0);
  206. }
  207. inline Vector3 rotated(const Vector3 &axis, const real_t phi) const {
  208. Vector3 v = *this;
  209. v.rotate(axis, phi);
  210. return v;
  211. }
  212. void rotate(const Vector3 &p_axis, real_t p_phi);
  213. inline Vector3 slide(const Vector3 &by) const {
  214. return *this - by * this->dot(by);
  215. }
  216. void snap(real_t p_val);
  217. inline Vector3 snapped(const float by) {
  218. Vector3 v = *this;
  219. v.snap(by);
  220. return v;
  221. }
  222. operator String() const;
  223. };
  224. inline Vector3 operator*(real_t p_scalar, const Vector3 &p_vec) {
  225. return p_vec * p_scalar;
  226. }
  227. inline Vector3 vec3_cross(const Vector3 &p_a, const Vector3 &p_b) {
  228. return p_a.cross(p_b);
  229. }
  230. } // namespace godot
  231. #endif // VECTOR3_H