Vector3.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*************************************************************************/
  2. /* Vector3.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef VECTOR3_H
  31. #define VECTOR3_H
  32. #include <gdnative/vector3.h>
  33. #include "Defs.hpp"
  34. #include "String.hpp"
  35. #include <Math.hpp>
  36. namespace godot {
  37. class Basis;
  38. struct Vector3 {
  39. enum Axis {
  40. AXIS_X,
  41. AXIS_Y,
  42. AXIS_Z,
  43. AXIS_COUNT
  44. };
  45. static const Vector3 ZERO;
  46. static const Vector3 ONE;
  47. static const Vector3 INF;
  48. // Coordinate system of the 3D engine
  49. static const Vector3 LEFT;
  50. static const Vector3 RIGHT;
  51. static const Vector3 UP;
  52. static const Vector3 DOWN;
  53. static const Vector3 FORWARD;
  54. static const Vector3 BACK;
  55. union {
  56. struct {
  57. real_t x;
  58. real_t y;
  59. real_t z;
  60. };
  61. real_t coord[3]; // Not for direct access, use [] operator instead
  62. };
  63. inline Vector3(real_t x, real_t y, real_t z) {
  64. this->x = x;
  65. this->y = y;
  66. this->z = z;
  67. }
  68. inline Vector3() {
  69. this->x = 0;
  70. this->y = 0;
  71. this->z = 0;
  72. }
  73. inline const real_t &operator[](int p_axis) const {
  74. return coord[p_axis];
  75. }
  76. inline real_t &operator[](int p_axis) {
  77. return coord[p_axis];
  78. }
  79. inline Vector3 &operator+=(const Vector3 &p_v) {
  80. x += p_v.x;
  81. y += p_v.y;
  82. z += p_v.z;
  83. return *this;
  84. }
  85. inline Vector3 operator+(const Vector3 &p_v) const {
  86. Vector3 v = *this;
  87. v += p_v;
  88. return v;
  89. }
  90. inline Vector3 &operator-=(const Vector3 &p_v) {
  91. x -= p_v.x;
  92. y -= p_v.y;
  93. z -= p_v.z;
  94. return *this;
  95. }
  96. inline Vector3 operator-(const Vector3 &p_v) const {
  97. Vector3 v = *this;
  98. v -= p_v;
  99. return v;
  100. }
  101. inline Vector3 &operator*=(const Vector3 &p_v) {
  102. x *= p_v.x;
  103. y *= p_v.y;
  104. z *= p_v.z;
  105. return *this;
  106. }
  107. inline Vector3 operator*(const Vector3 &p_v) const {
  108. Vector3 v = *this;
  109. v *= p_v;
  110. return v;
  111. }
  112. inline Vector3 &operator/=(const Vector3 &p_v) {
  113. x /= p_v.x;
  114. y /= p_v.y;
  115. z /= p_v.z;
  116. return *this;
  117. }
  118. inline Vector3 operator/(const Vector3 &p_v) const {
  119. Vector3 v = *this;
  120. v /= p_v;
  121. return v;
  122. }
  123. inline Vector3 &operator*=(real_t p_scalar) {
  124. *this *= Vector3(p_scalar, p_scalar, p_scalar);
  125. return *this;
  126. }
  127. inline Vector3 operator*(real_t p_scalar) const {
  128. Vector3 v = *this;
  129. v *= p_scalar;
  130. return v;
  131. }
  132. inline Vector3 &operator/=(real_t p_scalar) {
  133. *this /= Vector3(p_scalar, p_scalar, p_scalar);
  134. return *this;
  135. }
  136. inline Vector3 operator/(real_t p_scalar) const {
  137. Vector3 v = *this;
  138. v /= p_scalar;
  139. return v;
  140. }
  141. inline Vector3 operator-() const {
  142. return Vector3(-x, -y, -z);
  143. }
  144. inline bool operator==(const Vector3 &p_v) const {
  145. return (x == p_v.x && y == p_v.y && z == p_v.z);
  146. }
  147. inline bool operator!=(const Vector3 &p_v) const {
  148. return (x != p_v.x || y != p_v.y || z != p_v.z);
  149. }
  150. bool operator<(const Vector3 &p_v) const;
  151. bool operator<=(const Vector3 &p_v) const;
  152. inline Vector3 abs() const {
  153. return Vector3(::fabs(x), ::fabs(y), ::fabs(z));
  154. }
  155. inline Vector3 ceil() const {
  156. return Vector3(::ceil(x), ::ceil(y), ::ceil(z));
  157. }
  158. inline Vector3 cross(const Vector3 &b) const {
  159. Vector3 ret(
  160. (y * b.z) - (z * b.y),
  161. (z * b.x) - (x * b.z),
  162. (x * b.y) - (y * b.x));
  163. return ret;
  164. }
  165. inline Vector3 linear_interpolate(const Vector3 &p_b, real_t p_t) const {
  166. return Vector3(
  167. x + (p_t * (p_b.x - x)),
  168. y + (p_t * (p_b.y - y)),
  169. z + (p_t * (p_b.z - z)));
  170. }
  171. inline Vector3 slerp(const Vector3 &p_b, real_t p_t) const {
  172. real_t theta = angle_to(p_b);
  173. return rotated(cross(p_b).normalized(), theta * p_t);
  174. }
  175. Vector3 cubic_interpolate(const Vector3 &b, const Vector3 &pre_a, const Vector3 &post_b, const real_t t) const;
  176. Vector3 move_toward(const Vector3 &p_to, const real_t p_delta) const {
  177. Vector3 v = *this;
  178. Vector3 vd = p_to - v;
  179. real_t len = vd.length();
  180. return len <= p_delta || len < CMP_EPSILON ? p_to : v + vd / len * p_delta;
  181. }
  182. Vector3 bounce(const Vector3 &p_normal) const {
  183. return -reflect(p_normal);
  184. }
  185. inline real_t length() const {
  186. real_t x2 = x * x;
  187. real_t y2 = y * y;
  188. real_t z2 = z * z;
  189. return ::sqrt(x2 + y2 + z2);
  190. }
  191. inline real_t length_squared() const {
  192. real_t x2 = x * x;
  193. real_t y2 = y * y;
  194. real_t z2 = z * z;
  195. return x2 + y2 + z2;
  196. }
  197. inline real_t distance_squared_to(const Vector3 &b) const {
  198. return (b - *this).length_squared();
  199. }
  200. inline real_t distance_to(const Vector3 &b) const {
  201. return (b - *this).length();
  202. }
  203. inline real_t dot(const Vector3 &b) const {
  204. return x * b.x + y * b.y + z * b.z;
  205. }
  206. inline Vector3 project(const Vector3 &p_b) const {
  207. return p_b * (dot(p_b) / p_b.length_squared());
  208. }
  209. inline real_t angle_to(const Vector3 &b) const {
  210. return std::atan2(cross(b).length(), dot(b));
  211. }
  212. inline Vector3 direction_to(const Vector3 &p_b) const {
  213. Vector3 ret(p_b.x - x, p_b.y - y, p_b.z - z);
  214. ret.normalize();
  215. return ret;
  216. }
  217. inline Vector3 floor() const {
  218. return Vector3(::floor(x), ::floor(y), ::floor(z));
  219. }
  220. inline Vector3 inverse() const {
  221. return Vector3(1.f / x, 1.f / y, 1.f / z);
  222. }
  223. inline bool is_normalized() const {
  224. return std::abs(length_squared() - 1.f) < 0.00001f;
  225. }
  226. Basis outer(const Vector3 &b) const;
  227. int max_axis() const;
  228. int min_axis() const;
  229. inline void normalize() {
  230. real_t l = length();
  231. if (l == 0) {
  232. x = y = z = 0;
  233. } else {
  234. x /= l;
  235. y /= l;
  236. z /= l;
  237. }
  238. }
  239. inline Vector3 normalized() const {
  240. Vector3 v = *this;
  241. v.normalize();
  242. return v;
  243. }
  244. inline Vector3 reflect(const Vector3 &p_normal) const {
  245. return -(*this - p_normal * this->dot(p_normal) * 2.0);
  246. }
  247. inline Vector3 rotated(const Vector3 &axis, const real_t phi) const {
  248. Vector3 v = *this;
  249. v.rotate(axis, phi);
  250. return v;
  251. }
  252. void rotate(const Vector3 &p_axis, real_t p_phi);
  253. inline Vector3 slide(const Vector3 &by) const {
  254. return *this - by * this->dot(by);
  255. }
  256. void snap(real_t p_val);
  257. inline Vector3 snapped(const float by) {
  258. Vector3 v = *this;
  259. v.snap(by);
  260. return v;
  261. }
  262. operator String() const;
  263. };
  264. inline Vector3 operator*(real_t p_scalar, const Vector3 &p_vec) {
  265. return p_vec * p_scalar;
  266. }
  267. inline Vector3 vec3_cross(const Vector3 &p_a, const Vector3 &p_b) {
  268. return p_a.cross(p_b);
  269. }
  270. } // namespace godot
  271. #endif // VECTOR3_H