Vector3.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #ifndef VECTOR3_H
  2. #define VECTOR3_H
  3. #include <gdnative/vector3.h>
  4. #include "Defs.hpp"
  5. #include "String.hpp"
  6. #include <cmath>
  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. Vector3 cubic_interpolate(const Vector3 &b, const Vector3 &pre_a, const Vector3 &post_b, const real_t t) const;
  132. Vector3 bounce(const Vector3 &p_normal) const {
  133. return -reflect(p_normal);
  134. }
  135. inline real_t length() const {
  136. real_t x2 = x * x;
  137. real_t y2 = y * y;
  138. real_t z2 = z * z;
  139. return ::sqrt(x2 + y2 + z2);
  140. }
  141. inline real_t length_squared() const {
  142. real_t x2 = x * x;
  143. real_t y2 = y * y;
  144. real_t z2 = z * z;
  145. return x2 + y2 + z2;
  146. }
  147. inline real_t distance_squared_to(const Vector3 &b) const {
  148. return (b - *this).length_squared();
  149. }
  150. inline real_t distance_to(const Vector3 &b) const {
  151. return (b - *this).length();
  152. }
  153. inline real_t dot(const Vector3 &b) const {
  154. return x * b.x + y * b.y + z * b.z;
  155. }
  156. inline real_t angle_to(const Vector3 &b) const {
  157. return std::atan2(cross(b).length(), dot(b));
  158. }
  159. inline Vector3 floor() const {
  160. return Vector3(::floor(x), ::floor(y), ::floor(z));
  161. }
  162. inline Vector3 inverse() const {
  163. return Vector3(1.f / x, 1.f / y, 1.f / z);
  164. }
  165. inline bool is_normalized() const {
  166. return std::abs(length_squared() - 1.f) < 0.00001f;
  167. }
  168. Basis outer(const Vector3 &b) const;
  169. int max_axis() const;
  170. int min_axis() const;
  171. inline void normalize() {
  172. real_t l = length();
  173. if (l == 0) {
  174. x = y = z = 0;
  175. } else {
  176. x /= l;
  177. y /= l;
  178. z /= l;
  179. }
  180. }
  181. inline Vector3 normalized() const {
  182. Vector3 v = *this;
  183. v.normalize();
  184. return v;
  185. }
  186. inline Vector3 reflect(const Vector3 &by) const {
  187. return by - *this * this->dot(by) * 2.f;
  188. }
  189. inline Vector3 rotated(const Vector3 &axis, const real_t phi) const {
  190. Vector3 v = *this;
  191. v.rotate(axis, phi);
  192. return v;
  193. }
  194. void rotate(const Vector3 &p_axis, real_t p_phi);
  195. inline Vector3 slide(const Vector3 &by) const {
  196. return by - *this * this->dot(by);
  197. }
  198. void snap(real_t p_val);
  199. inline Vector3 snapped(const float by) {
  200. Vector3 v = *this;
  201. v.snap(by);
  202. return v;
  203. }
  204. operator String() const;
  205. };
  206. inline Vector3 operator*(real_t p_scalar, const Vector3 &p_vec) {
  207. return p_vec * p_scalar;
  208. }
  209. inline Vector3 vec3_cross(const Vector3 &p_a, const Vector3 &p_b) {
  210. return p_a.cross(p_b);
  211. }
  212. } // namespace godot
  213. #endif // VECTOR3_H