vector3.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*************************************************************************/
  2. /* vector3.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 GODOT_VECTOR3_HPP
  31. #define GODOT_VECTOR3_HPP
  32. #include <godot_cpp/core/error_macros.hpp>
  33. #include <godot_cpp/core/math.hpp>
  34. namespace godot {
  35. class Basis;
  36. class String;
  37. class Vector2;
  38. class Vector3i;
  39. class Vector3 {
  40. _FORCE_INLINE_ GDNativeTypePtr _native_ptr() const { return (void *)this; }
  41. friend class Variant;
  42. public:
  43. enum Axis {
  44. AXIS_X,
  45. AXIS_Y,
  46. AXIS_Z,
  47. };
  48. union {
  49. struct {
  50. real_t x;
  51. real_t y;
  52. real_t z;
  53. };
  54. real_t coord[3] = { 0 };
  55. };
  56. _FORCE_INLINE_ const real_t &operator[](const int p_axis) const {
  57. DEV_ASSERT((unsigned int)p_axis < 3);
  58. return coord[p_axis];
  59. }
  60. _FORCE_INLINE_ real_t &operator[](const int p_axis) {
  61. DEV_ASSERT((unsigned int)p_axis < 3);
  62. return coord[p_axis];
  63. }
  64. void set_axis(const int p_axis, const real_t p_value);
  65. real_t get_axis(const int p_axis) const;
  66. _FORCE_INLINE_ void set_all(const real_t p_value) {
  67. x = y = z = p_value;
  68. }
  69. _FORCE_INLINE_ Vector3::Axis min_axis_index() const {
  70. return x < y ? (x < z ? Vector3::AXIS_X : Vector3::AXIS_Z) : (y < z ? Vector3::AXIS_Y : Vector3::AXIS_Z);
  71. }
  72. _FORCE_INLINE_ Vector3::Axis max_axis_index() const {
  73. return x < y ? (y < z ? Vector3::AXIS_Z : Vector3::AXIS_Y) : (x < z ? Vector3::AXIS_Z : Vector3::AXIS_X);
  74. }
  75. _FORCE_INLINE_ real_t length() const;
  76. _FORCE_INLINE_ real_t length_squared() const;
  77. _FORCE_INLINE_ void normalize();
  78. _FORCE_INLINE_ Vector3 normalized() const;
  79. _FORCE_INLINE_ bool is_normalized() const;
  80. _FORCE_INLINE_ Vector3 inverse() const;
  81. Vector3 limit_length(const real_t p_len = 1.0) const;
  82. _FORCE_INLINE_ void zero();
  83. void snap(const Vector3 p_val);
  84. Vector3 snapped(const Vector3 p_val) const;
  85. void rotate(const Vector3 &p_axis, const real_t p_angle);
  86. Vector3 rotated(const Vector3 &p_axis, const real_t p_angle) const;
  87. /* Static Methods between 2 vector3s */
  88. _FORCE_INLINE_ Vector3 lerp(const Vector3 &p_to, const real_t p_weight) const;
  89. _FORCE_INLINE_ Vector3 slerp(const Vector3 &p_to, const real_t p_weight) const;
  90. _FORCE_INLINE_ Vector3 cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, const real_t p_weight) const;
  91. _FORCE_INLINE_ Vector3 bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, const real_t p_t) const;
  92. Vector3 move_toward(const Vector3 &p_to, const real_t p_delta) const;
  93. Vector2 octahedron_encode() const;
  94. static Vector3 octahedron_decode(const Vector2 &p_oct);
  95. _FORCE_INLINE_ Vector3 cross(const Vector3 &p_with) const;
  96. _FORCE_INLINE_ real_t dot(const Vector3 &p_with) const;
  97. Basis outer(const Vector3 &p_with) const;
  98. _FORCE_INLINE_ Vector3 abs() const;
  99. _FORCE_INLINE_ Vector3 floor() const;
  100. _FORCE_INLINE_ Vector3 sign() const;
  101. _FORCE_INLINE_ Vector3 ceil() const;
  102. _FORCE_INLINE_ Vector3 round() const;
  103. Vector3 clamp(const Vector3 &p_min, const Vector3 &p_max) const;
  104. _FORCE_INLINE_ real_t distance_to(const Vector3 &p_to) const;
  105. _FORCE_INLINE_ real_t distance_squared_to(const Vector3 &p_to) const;
  106. _FORCE_INLINE_ Vector3 posmod(const real_t p_mod) const;
  107. _FORCE_INLINE_ Vector3 posmodv(const Vector3 &p_modv) const;
  108. _FORCE_INLINE_ Vector3 project(const Vector3 &p_to) const;
  109. _FORCE_INLINE_ real_t angle_to(const Vector3 &p_to) const;
  110. _FORCE_INLINE_ real_t signed_angle_to(const Vector3 &p_to, const Vector3 &p_axis) const;
  111. _FORCE_INLINE_ Vector3 direction_to(const Vector3 &p_to) const;
  112. _FORCE_INLINE_ Vector3 slide(const Vector3 &p_normal) const;
  113. _FORCE_INLINE_ Vector3 bounce(const Vector3 &p_normal) const;
  114. _FORCE_INLINE_ Vector3 reflect(const Vector3 &p_normal) const;
  115. bool is_equal_approx(const Vector3 &p_v) const;
  116. /* Operators */
  117. _FORCE_INLINE_ Vector3 &operator+=(const Vector3 &p_v);
  118. _FORCE_INLINE_ Vector3 operator+(const Vector3 &p_v) const;
  119. _FORCE_INLINE_ Vector3 &operator-=(const Vector3 &p_v);
  120. _FORCE_INLINE_ Vector3 operator-(const Vector3 &p_v) const;
  121. _FORCE_INLINE_ Vector3 &operator*=(const Vector3 &p_v);
  122. _FORCE_INLINE_ Vector3 operator*(const Vector3 &p_v) const;
  123. _FORCE_INLINE_ Vector3 &operator/=(const Vector3 &p_v);
  124. _FORCE_INLINE_ Vector3 operator/(const Vector3 &p_v) const;
  125. _FORCE_INLINE_ Vector3 &operator*=(const real_t p_scalar);
  126. _FORCE_INLINE_ Vector3 operator*(const real_t p_scalar) const;
  127. _FORCE_INLINE_ Vector3 &operator/=(const real_t p_scalar);
  128. _FORCE_INLINE_ Vector3 operator/(const real_t p_scalar) const;
  129. _FORCE_INLINE_ Vector3 operator-() const;
  130. _FORCE_INLINE_ bool operator==(const Vector3 &p_v) const;
  131. _FORCE_INLINE_ bool operator!=(const Vector3 &p_v) const;
  132. _FORCE_INLINE_ bool operator<(const Vector3 &p_v) const;
  133. _FORCE_INLINE_ bool operator<=(const Vector3 &p_v) const;
  134. _FORCE_INLINE_ bool operator>(const Vector3 &p_v) const;
  135. _FORCE_INLINE_ bool operator>=(const Vector3 &p_v) const;
  136. operator String() const;
  137. operator Vector3i() const;
  138. _FORCE_INLINE_ Vector3() {}
  139. _FORCE_INLINE_ Vector3(const real_t p_x, const real_t p_y, const real_t p_z) {
  140. x = p_x;
  141. y = p_y;
  142. z = p_z;
  143. }
  144. };
  145. Vector3 Vector3::cross(const Vector3 &p_with) const {
  146. Vector3 ret(
  147. (y * p_with.z) - (z * p_with.y),
  148. (z * p_with.x) - (x * p_with.z),
  149. (x * p_with.y) - (y * p_with.x));
  150. return ret;
  151. }
  152. real_t Vector3::dot(const Vector3 &p_with) const {
  153. return x * p_with.x + y * p_with.y + z * p_with.z;
  154. }
  155. Vector3 Vector3::abs() const {
  156. return Vector3(Math::abs(x), Math::abs(y), Math::abs(z));
  157. }
  158. Vector3 Vector3::sign() const {
  159. return Vector3(SIGN(x), SIGN(y), SIGN(z));
  160. }
  161. Vector3 Vector3::floor() const {
  162. return Vector3(Math::floor(x), Math::floor(y), Math::floor(z));
  163. }
  164. Vector3 Vector3::ceil() const {
  165. return Vector3(Math::ceil(x), Math::ceil(y), Math::ceil(z));
  166. }
  167. Vector3 Vector3::round() const {
  168. return Vector3(Math::round(x), Math::round(y), Math::round(z));
  169. }
  170. Vector3 Vector3::lerp(const Vector3 &p_to, const real_t p_weight) const {
  171. return Vector3(
  172. x + (p_weight * (p_to.x - x)),
  173. y + (p_weight * (p_to.y - y)),
  174. z + (p_weight * (p_to.z - z)));
  175. }
  176. Vector3 Vector3::slerp(const Vector3 &p_to, const real_t p_weight) const {
  177. real_t start_length_sq = length_squared();
  178. real_t end_length_sq = p_to.length_squared();
  179. if (unlikely(start_length_sq == 0.0f || end_length_sq == 0.0f)) {
  180. // Zero length vectors have no angle, so the best we can do is either lerp or throw an error.
  181. return lerp(p_to, p_weight);
  182. }
  183. real_t start_length = Math::sqrt(start_length_sq);
  184. real_t result_length = Math::lerp(start_length, Math::sqrt(end_length_sq), p_weight);
  185. real_t angle = angle_to(p_to);
  186. return rotated(cross(p_to).normalized(), angle * p_weight) * (result_length / start_length);
  187. }
  188. Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, const real_t p_weight) const {
  189. Vector3 res = *this;
  190. res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight);
  191. res.y = Math::cubic_interpolate(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight);
  192. res.z = Math::cubic_interpolate(res.z, p_b.z, p_pre_a.z, p_post_b.z, p_weight);
  193. return res;
  194. }
  195. Vector3 Vector3::bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, const real_t p_t) const {
  196. Vector3 res = *this;
  197. /* Formula from Wikipedia article on Bezier curves. */
  198. real_t omt = (1.0 - p_t);
  199. real_t omt2 = omt * omt;
  200. real_t omt3 = omt2 * omt;
  201. real_t t2 = p_t * p_t;
  202. real_t t3 = t2 * p_t;
  203. return res * omt3 + p_control_1 * omt2 * p_t * 3.0 + p_control_2 * omt * t2 * 3.0 + p_end * t3;
  204. }
  205. real_t Vector3::distance_to(const Vector3 &p_to) const {
  206. return (p_to - *this).length();
  207. }
  208. real_t Vector3::distance_squared_to(const Vector3 &p_to) const {
  209. return (p_to - *this).length_squared();
  210. }
  211. Vector3 Vector3::posmod(const real_t p_mod) const {
  212. return Vector3(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod));
  213. }
  214. Vector3 Vector3::posmodv(const Vector3 &p_modv) const {
  215. return Vector3(Math::fposmod(x, p_modv.x), Math::fposmod(y, p_modv.y), Math::fposmod(z, p_modv.z));
  216. }
  217. Vector3 Vector3::project(const Vector3 &p_to) const {
  218. return p_to * (dot(p_to) / p_to.length_squared());
  219. }
  220. real_t Vector3::angle_to(const Vector3 &p_to) const {
  221. return Math::atan2(cross(p_to).length(), dot(p_to));
  222. }
  223. real_t Vector3::signed_angle_to(const Vector3 &p_to, const Vector3 &p_axis) const {
  224. Vector3 cross_to = cross(p_to);
  225. real_t unsigned_angle = Math::atan2(cross_to.length(), dot(p_to));
  226. real_t sign = cross_to.dot(p_axis);
  227. return (sign < 0) ? -unsigned_angle : unsigned_angle;
  228. }
  229. Vector3 Vector3::direction_to(const Vector3 &p_to) const {
  230. Vector3 ret(p_to.x - x, p_to.y - y, p_to.z - z);
  231. ret.normalize();
  232. return ret;
  233. }
  234. /* Operators */
  235. Vector3 &Vector3::operator+=(const Vector3 &p_v) {
  236. x += p_v.x;
  237. y += p_v.y;
  238. z += p_v.z;
  239. return *this;
  240. }
  241. Vector3 Vector3::operator+(const Vector3 &p_v) const {
  242. return Vector3(x + p_v.x, y + p_v.y, z + p_v.z);
  243. }
  244. Vector3 &Vector3::operator-=(const Vector3 &p_v) {
  245. x -= p_v.x;
  246. y -= p_v.y;
  247. z -= p_v.z;
  248. return *this;
  249. }
  250. Vector3 Vector3::operator-(const Vector3 &p_v) const {
  251. return Vector3(x - p_v.x, y - p_v.y, z - p_v.z);
  252. }
  253. Vector3 &Vector3::operator*=(const Vector3 &p_v) {
  254. x *= p_v.x;
  255. y *= p_v.y;
  256. z *= p_v.z;
  257. return *this;
  258. }
  259. Vector3 Vector3::operator*(const Vector3 &p_v) const {
  260. return Vector3(x * p_v.x, y * p_v.y, z * p_v.z);
  261. }
  262. Vector3 &Vector3::operator/=(const Vector3 &p_v) {
  263. x /= p_v.x;
  264. y /= p_v.y;
  265. z /= p_v.z;
  266. return *this;
  267. }
  268. Vector3 Vector3::operator/(const Vector3 &p_v) const {
  269. return Vector3(x / p_v.x, y / p_v.y, z / p_v.z);
  270. }
  271. Vector3 &Vector3::operator*=(const real_t p_scalar) {
  272. x *= p_scalar;
  273. y *= p_scalar;
  274. z *= p_scalar;
  275. return *this;
  276. }
  277. // Multiplication operators required to workaround issues with LLVM using implicit conversion
  278. // to Vector3i instead for integers where it should not.
  279. _FORCE_INLINE_ Vector3 operator*(const float p_scalar, const Vector3 &p_vec) {
  280. return p_vec * p_scalar;
  281. }
  282. _FORCE_INLINE_ Vector3 operator*(const double p_scalar, const Vector3 &p_vec) {
  283. return p_vec * p_scalar;
  284. }
  285. _FORCE_INLINE_ Vector3 operator*(const int32_t p_scalar, const Vector3 &p_vec) {
  286. return p_vec * p_scalar;
  287. }
  288. _FORCE_INLINE_ Vector3 operator*(const int64_t p_scalar, const Vector3 &p_vec) {
  289. return p_vec * p_scalar;
  290. }
  291. Vector3 Vector3::operator*(const real_t p_scalar) const {
  292. return Vector3(x * p_scalar, y * p_scalar, z * p_scalar);
  293. }
  294. Vector3 &Vector3::operator/=(const real_t p_scalar) {
  295. x /= p_scalar;
  296. y /= p_scalar;
  297. z /= p_scalar;
  298. return *this;
  299. }
  300. Vector3 Vector3::operator/(const real_t p_scalar) const {
  301. return Vector3(x / p_scalar, y / p_scalar, z / p_scalar);
  302. }
  303. Vector3 Vector3::operator-() const {
  304. return Vector3(-x, -y, -z);
  305. }
  306. bool Vector3::operator==(const Vector3 &p_v) const {
  307. return x == p_v.x && y == p_v.y && z == p_v.z;
  308. }
  309. bool Vector3::operator!=(const Vector3 &p_v) const {
  310. return x != p_v.x || y != p_v.y || z != p_v.z;
  311. }
  312. bool Vector3::operator<(const Vector3 &p_v) const {
  313. if (x == p_v.x) {
  314. if (y == p_v.y) {
  315. return z < p_v.z;
  316. }
  317. return y < p_v.y;
  318. }
  319. return x < p_v.x;
  320. }
  321. bool Vector3::operator>(const Vector3 &p_v) const {
  322. if (x == p_v.x) {
  323. if (y == p_v.y) {
  324. return z > p_v.z;
  325. }
  326. return y > p_v.y;
  327. }
  328. return x > p_v.x;
  329. }
  330. bool Vector3::operator<=(const Vector3 &p_v) const {
  331. if (x == p_v.x) {
  332. if (y == p_v.y) {
  333. return z <= p_v.z;
  334. }
  335. return y < p_v.y;
  336. }
  337. return x < p_v.x;
  338. }
  339. bool Vector3::operator>=(const Vector3 &p_v) const {
  340. if (x == p_v.x) {
  341. if (y == p_v.y) {
  342. return z >= p_v.z;
  343. }
  344. return y > p_v.y;
  345. }
  346. return x > p_v.x;
  347. }
  348. _FORCE_INLINE_ Vector3 vec3_cross(const Vector3 &p_a, const Vector3 &p_b) {
  349. return p_a.cross(p_b);
  350. }
  351. _FORCE_INLINE_ real_t vec3_dot(const Vector3 &p_a, const Vector3 &p_b) {
  352. return p_a.dot(p_b);
  353. }
  354. real_t Vector3::length() const {
  355. real_t x2 = x * x;
  356. real_t y2 = y * y;
  357. real_t z2 = z * z;
  358. return Math::sqrt(x2 + y2 + z2);
  359. }
  360. real_t Vector3::length_squared() const {
  361. real_t x2 = x * x;
  362. real_t y2 = y * y;
  363. real_t z2 = z * z;
  364. return x2 + y2 + z2;
  365. }
  366. void Vector3::normalize() {
  367. real_t lengthsq = length_squared();
  368. if (lengthsq == 0) {
  369. x = y = z = 0;
  370. } else {
  371. real_t length = Math::sqrt(lengthsq);
  372. x /= length;
  373. y /= length;
  374. z /= length;
  375. }
  376. }
  377. Vector3 Vector3::normalized() const {
  378. Vector3 v = *this;
  379. v.normalize();
  380. return v;
  381. }
  382. bool Vector3::is_normalized() const {
  383. // use length_squared() instead of length() to avoid sqrt(), makes it more stringent.
  384. return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON);
  385. }
  386. Vector3 Vector3::inverse() const {
  387. return Vector3(1.0f / x, 1.0f / y, 1.0f / z);
  388. }
  389. void Vector3::zero() {
  390. x = y = z = 0;
  391. }
  392. // slide returns the component of the vector along the given plane, specified by its normal vector.
  393. Vector3 Vector3::slide(const Vector3 &p_normal) const {
  394. #ifdef MATH_CHECKS
  395. ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 must be normalized.");
  396. #endif
  397. return *this - p_normal * this->dot(p_normal);
  398. }
  399. Vector3 Vector3::bounce(const Vector3 &p_normal) const {
  400. return -reflect(p_normal);
  401. }
  402. Vector3 Vector3::reflect(const Vector3 &p_normal) const {
  403. #ifdef MATH_CHECKS
  404. ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 must be normalized.");
  405. #endif
  406. return 2.0f * p_normal * this->dot(p_normal) - *this;
  407. }
  408. } // namespace godot
  409. #endif // GODOT_VECTOR3_HPP