Vector3.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. AXIS_COUNT
  15. };
  16. static const Vector3 ZERO;
  17. static const Vector3 ONE;
  18. static const Vector3 INF;
  19. // Coordinate system of the 3D engine
  20. static const Vector3 LEFT;
  21. static const Vector3 RIGHT;
  22. static const Vector3 UP;
  23. static const Vector3 DOWN;
  24. static const Vector3 FORWARD;
  25. static const Vector3 BACK;
  26. union {
  27. struct {
  28. real_t x;
  29. real_t y;
  30. real_t z;
  31. };
  32. real_t coord[3]; // Not for direct access, use [] operator instead
  33. };
  34. inline Vector3(real_t x, real_t y, real_t z) {
  35. this->x = x;
  36. this->y = y;
  37. this->z = z;
  38. }
  39. inline Vector3() {
  40. this->x = 0;
  41. this->y = 0;
  42. this->z = 0;
  43. }
  44. inline const real_t &operator[](int p_axis) const {
  45. return coord[p_axis];
  46. }
  47. inline real_t &operator[](int p_axis) {
  48. return coord[p_axis];
  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/=(const Vector3 &p_v) {
  84. x /= p_v.x;
  85. y /= p_v.y;
  86. z /= p_v.z;
  87. return *this;
  88. }
  89. inline Vector3 operator/(const Vector3 &p_v) const {
  90. Vector3 v = *this;
  91. v /= p_v;
  92. return v;
  93. }
  94. inline Vector3 &operator*=(real_t p_scalar) {
  95. *this *= Vector3(p_scalar, p_scalar, p_scalar);
  96. return *this;
  97. }
  98. inline Vector3 operator*(real_t p_scalar) const {
  99. Vector3 v = *this;
  100. v *= p_scalar;
  101. return v;
  102. }
  103. inline Vector3 &operator/=(real_t p_scalar) {
  104. *this /= Vector3(p_scalar, p_scalar, p_scalar);
  105. return *this;
  106. }
  107. inline Vector3 operator/(real_t p_scalar) const {
  108. Vector3 v = *this;
  109. v /= p_scalar;
  110. return v;
  111. }
  112. inline Vector3 operator-() const {
  113. return Vector3(-x, -y, -z);
  114. }
  115. inline bool operator==(const Vector3 &p_v) const {
  116. return (x == p_v.x && y == p_v.y && z == p_v.z);
  117. }
  118. inline bool operator!=(const Vector3 &p_v) const {
  119. return (x != p_v.x || y != p_v.y || z != p_v.z);
  120. }
  121. bool operator<(const Vector3 &p_v) const;
  122. bool operator<=(const Vector3 &p_v) const;
  123. inline Vector3 abs() const {
  124. return Vector3(::fabs(x), ::fabs(y), ::fabs(z));
  125. }
  126. inline Vector3 ceil() const {
  127. return Vector3(::ceil(x), ::ceil(y), ::ceil(z));
  128. }
  129. inline Vector3 cross(const Vector3 &b) const {
  130. Vector3 ret(
  131. (y * b.z) - (z * b.y),
  132. (z * b.x) - (x * b.z),
  133. (x * b.y) - (y * b.x));
  134. return ret;
  135. }
  136. inline Vector3 linear_interpolate(const Vector3 &p_b, real_t p_t) const {
  137. return Vector3(
  138. x + (p_t * (p_b.x - x)),
  139. y + (p_t * (p_b.y - y)),
  140. z + (p_t * (p_b.z - z)));
  141. }
  142. inline Vector3 slerp(const Vector3 &p_b, real_t p_t) const {
  143. real_t theta = angle_to(p_b);
  144. return rotated(cross(p_b).normalized(), theta * p_t);
  145. }
  146. Vector3 cubic_interpolate(const Vector3 &b, const Vector3 &pre_a, const Vector3 &post_b, const real_t t) const;
  147. Vector3 move_toward(const Vector3 &p_to, const real_t p_delta) const {
  148. Vector3 v = *this;
  149. Vector3 vd = p_to - v;
  150. real_t len = vd.length();
  151. return len <= p_delta || len < CMP_EPSILON ? p_to : v + vd / len * p_delta;
  152. }
  153. Vector3 bounce(const Vector3 &p_normal) const {
  154. return -reflect(p_normal);
  155. }
  156. inline real_t length() const {
  157. real_t x2 = x * x;
  158. real_t y2 = y * y;
  159. real_t z2 = z * z;
  160. return ::sqrt(x2 + y2 + z2);
  161. }
  162. inline real_t length_squared() const {
  163. real_t x2 = x * x;
  164. real_t y2 = y * y;
  165. real_t z2 = z * z;
  166. return x2 + y2 + z2;
  167. }
  168. inline real_t distance_squared_to(const Vector3 &b) const {
  169. return (b - *this).length_squared();
  170. }
  171. inline real_t distance_to(const Vector3 &b) const {
  172. return (b - *this).length();
  173. }
  174. inline real_t dot(const Vector3 &b) const {
  175. return x * b.x + y * b.y + z * b.z;
  176. }
  177. inline Vector3 project(const Vector3 &p_b) const {
  178. return p_b * (dot(p_b) / p_b.length_squared());
  179. }
  180. inline real_t angle_to(const Vector3 &b) const {
  181. return std::atan2(cross(b).length(), dot(b));
  182. }
  183. inline Vector3 direction_to(const Vector3 &p_b) const {
  184. Vector3 ret(p_b.x - x, p_b.y - y, p_b.z - z);
  185. ret.normalize();
  186. return ret;
  187. }
  188. inline Vector3 floor() const {
  189. return Vector3(::floor(x), ::floor(y), ::floor(z));
  190. }
  191. inline Vector3 inverse() const {
  192. return Vector3(1.f / x, 1.f / y, 1.f / z);
  193. }
  194. inline bool is_normalized() const {
  195. return std::abs(length_squared() - 1.f) < 0.00001f;
  196. }
  197. Basis outer(const Vector3 &b) const;
  198. int max_axis() const;
  199. int min_axis() const;
  200. inline void normalize() {
  201. real_t l = length();
  202. if (l == 0) {
  203. x = y = z = 0;
  204. } else {
  205. x /= l;
  206. y /= l;
  207. z /= l;
  208. }
  209. }
  210. inline Vector3 normalized() const {
  211. Vector3 v = *this;
  212. v.normalize();
  213. return v;
  214. }
  215. inline Vector3 reflect(const Vector3 &p_normal) const {
  216. return -(*this - p_normal * this->dot(p_normal) * 2.0);
  217. }
  218. inline Vector3 rotated(const Vector3 &axis, const real_t phi) const {
  219. Vector3 v = *this;
  220. v.rotate(axis, phi);
  221. return v;
  222. }
  223. void rotate(const Vector3 &p_axis, real_t p_phi);
  224. inline Vector3 slide(const Vector3 &by) const {
  225. return *this - by * this->dot(by);
  226. }
  227. void snap(real_t p_val);
  228. inline Vector3 snapped(const float by) {
  229. Vector3 v = *this;
  230. v.snap(by);
  231. return v;
  232. }
  233. operator String() const;
  234. };
  235. inline Vector3 operator*(real_t p_scalar, const Vector3 &p_vec) {
  236. return p_vec * p_scalar;
  237. }
  238. inline Vector3 vec3_cross(const Vector3 &p_a, const Vector3 &p_b) {
  239. return p_a.cross(p_b);
  240. }
  241. } // namespace godot
  242. #endif // VECTOR3_H