vector3.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /**************************************************************************/
  2. /* vector3.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #pragma once
  31. #include "core/error/error_macros.h"
  32. #include "core/math/math_funcs.h"
  33. #include "core/string/ustring.h"
  34. struct Basis;
  35. struct Vector2;
  36. struct Vector3i;
  37. struct [[nodiscard]] Vector3 {
  38. static const Vector3 LEFT;
  39. static const Vector3 RIGHT;
  40. static const Vector3 UP;
  41. static const Vector3 DOWN;
  42. static const Vector3 FORWARD;
  43. static const Vector3 BACK;
  44. static const Vector3 MODEL_LEFT;
  45. static const Vector3 MODEL_RIGHT;
  46. static const Vector3 MODEL_TOP;
  47. static const Vector3 MODEL_BOTTOM;
  48. static const Vector3 MODEL_FRONT;
  49. static const Vector3 MODEL_REAR;
  50. static constexpr int AXIS_COUNT = 3;
  51. enum Axis {
  52. AXIS_X,
  53. AXIS_Y,
  54. AXIS_Z,
  55. };
  56. union {
  57. // NOLINTBEGIN(modernize-use-default-member-init)
  58. struct {
  59. real_t x;
  60. real_t y;
  61. real_t z;
  62. };
  63. real_t coord[3] = { 0 };
  64. // NOLINTEND(modernize-use-default-member-init)
  65. };
  66. _FORCE_INLINE_ const real_t &operator[](int p_axis) const {
  67. DEV_ASSERT((unsigned int)p_axis < 3);
  68. return coord[p_axis];
  69. }
  70. _FORCE_INLINE_ real_t &operator[](int p_axis) {
  71. DEV_ASSERT((unsigned int)p_axis < 3);
  72. return coord[p_axis];
  73. }
  74. _FORCE_INLINE_ Vector3::Axis min_axis_index() const {
  75. return x < y ? (x < z ? Vector3::AXIS_X : Vector3::AXIS_Z) : (y < z ? Vector3::AXIS_Y : Vector3::AXIS_Z);
  76. }
  77. _FORCE_INLINE_ Vector3::Axis max_axis_index() const {
  78. return x < y ? (y < z ? Vector3::AXIS_Z : Vector3::AXIS_Y) : (x < z ? Vector3::AXIS_Z : Vector3::AXIS_X);
  79. }
  80. Vector3 min(const Vector3 &p_vector3) const {
  81. return Vector3(MIN(x, p_vector3.x), MIN(y, p_vector3.y), MIN(z, p_vector3.z));
  82. }
  83. Vector3 minf(real_t p_scalar) const {
  84. return Vector3(MIN(x, p_scalar), MIN(y, p_scalar), MIN(z, p_scalar));
  85. }
  86. Vector3 max(const Vector3 &p_vector3) const {
  87. return Vector3(MAX(x, p_vector3.x), MAX(y, p_vector3.y), MAX(z, p_vector3.z));
  88. }
  89. Vector3 maxf(real_t p_scalar) const {
  90. return Vector3(MAX(x, p_scalar), MAX(y, p_scalar), MAX(z, p_scalar));
  91. }
  92. Vector3 clamp(const Vector3 &p_min, const Vector3 &p_max) const {
  93. return Vector3(
  94. CLAMP(x, p_min.x, p_max.x),
  95. CLAMP(y, p_min.y, p_max.y),
  96. CLAMP(z, p_min.z, p_max.z));
  97. }
  98. Vector3 clampf(real_t p_min, real_t p_max) const {
  99. return Vector3(
  100. CLAMP(x, p_min, p_max),
  101. CLAMP(y, p_min, p_max),
  102. CLAMP(z, p_min, p_max));
  103. }
  104. _FORCE_INLINE_ real_t length() const;
  105. _FORCE_INLINE_ real_t length_squared() const;
  106. _FORCE_INLINE_ void normalize();
  107. _FORCE_INLINE_ Vector3 normalized() const;
  108. _FORCE_INLINE_ bool is_normalized() const;
  109. _FORCE_INLINE_ Vector3 inverse() const;
  110. Vector3 limit_length(real_t p_len = 1.0) const;
  111. _FORCE_INLINE_ void zero();
  112. void snap(const Vector3 &p_step);
  113. void snapf(real_t p_step);
  114. Vector3 snapped(const Vector3 &p_step) const;
  115. Vector3 snappedf(real_t p_step) const;
  116. void rotate(const Vector3 &p_axis, real_t p_angle);
  117. Vector3 rotated(const Vector3 &p_axis, real_t p_angle) const;
  118. /* Static Methods between 2 vector3s */
  119. _FORCE_INLINE_ Vector3 lerp(const Vector3 &p_to, real_t p_weight) const;
  120. _FORCE_INLINE_ Vector3 slerp(const Vector3 &p_to, real_t p_weight) const;
  121. _FORCE_INLINE_ Vector3 cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const;
  122. _FORCE_INLINE_ Vector3 cubic_interpolate_in_time(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight, real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const;
  123. _FORCE_INLINE_ Vector3 bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, real_t p_t) const;
  124. _FORCE_INLINE_ Vector3 bezier_derivative(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, real_t p_t) const;
  125. Vector3 move_toward(const Vector3 &p_to, real_t p_delta) const;
  126. Vector2 octahedron_encode() const;
  127. static Vector3 octahedron_decode(const Vector2 &p_oct);
  128. Vector2 octahedron_tangent_encode(float p_sign) const;
  129. static Vector3 octahedron_tangent_decode(const Vector2 &p_oct, float *r_sign);
  130. _FORCE_INLINE_ Vector3 cross(const Vector3 &p_with) const;
  131. _FORCE_INLINE_ real_t dot(const Vector3 &p_with) const;
  132. Basis outer(const Vector3 &p_with) const;
  133. _FORCE_INLINE_ Vector3 get_any_perpendicular() const;
  134. _FORCE_INLINE_ Vector3 abs() const;
  135. _FORCE_INLINE_ Vector3 floor() const;
  136. _FORCE_INLINE_ Vector3 sign() const;
  137. _FORCE_INLINE_ Vector3 ceil() const;
  138. _FORCE_INLINE_ Vector3 round() const;
  139. _FORCE_INLINE_ real_t distance_to(const Vector3 &p_to) const;
  140. _FORCE_INLINE_ real_t distance_squared_to(const Vector3 &p_to) const;
  141. _FORCE_INLINE_ Vector3 posmod(real_t p_mod) const;
  142. _FORCE_INLINE_ Vector3 posmodv(const Vector3 &p_modv) const;
  143. _FORCE_INLINE_ Vector3 project(const Vector3 &p_to) const;
  144. _FORCE_INLINE_ real_t angle_to(const Vector3 &p_to) const;
  145. _FORCE_INLINE_ real_t signed_angle_to(const Vector3 &p_to, const Vector3 &p_axis) const;
  146. _FORCE_INLINE_ Vector3 direction_to(const Vector3 &p_to) const;
  147. _FORCE_INLINE_ Vector3 slide(const Vector3 &p_normal) const;
  148. _FORCE_INLINE_ Vector3 bounce(const Vector3 &p_normal) const;
  149. _FORCE_INLINE_ Vector3 reflect(const Vector3 &p_normal) const;
  150. bool is_equal_approx(const Vector3 &p_v) const;
  151. bool is_same(const Vector3 &p_v) const;
  152. bool is_zero_approx() const;
  153. bool is_finite() const;
  154. /* Operators */
  155. constexpr Vector3 &operator+=(const Vector3 &p_v);
  156. constexpr Vector3 operator+(const Vector3 &p_v) const;
  157. constexpr Vector3 &operator-=(const Vector3 &p_v);
  158. constexpr Vector3 operator-(const Vector3 &p_v) const;
  159. constexpr Vector3 &operator*=(const Vector3 &p_v);
  160. constexpr Vector3 operator*(const Vector3 &p_v) const;
  161. constexpr Vector3 &operator/=(const Vector3 &p_v);
  162. constexpr Vector3 operator/(const Vector3 &p_v) const;
  163. constexpr Vector3 &operator*=(real_t p_scalar);
  164. constexpr Vector3 operator*(real_t p_scalar) const;
  165. constexpr Vector3 &operator/=(real_t p_scalar);
  166. constexpr Vector3 operator/(real_t p_scalar) const;
  167. constexpr Vector3 operator-() const;
  168. constexpr bool operator==(const Vector3 &p_v) const;
  169. constexpr bool operator!=(const Vector3 &p_v) const;
  170. constexpr bool operator<(const Vector3 &p_v) const;
  171. constexpr bool operator<=(const Vector3 &p_v) const;
  172. constexpr bool operator>(const Vector3 &p_v) const;
  173. constexpr bool operator>=(const Vector3 &p_v) const;
  174. explicit operator String() const;
  175. operator Vector3i() const;
  176. uint32_t hash() const {
  177. uint32_t h = hash_murmur3_one_real(x);
  178. h = hash_murmur3_one_real(y, h);
  179. h = hash_murmur3_one_real(z, h);
  180. return hash_fmix32(h);
  181. }
  182. constexpr Vector3() :
  183. x(0), y(0), z(0) {}
  184. constexpr Vector3(real_t p_x, real_t p_y, real_t p_z) :
  185. x(p_x), y(p_y), z(p_z) {}
  186. };
  187. inline constexpr Vector3 Vector3::LEFT = { -1, 0, 0 };
  188. inline constexpr Vector3 Vector3::RIGHT = { 1, 0, 0 };
  189. inline constexpr Vector3 Vector3::UP = { 0, 1, 0 };
  190. inline constexpr Vector3 Vector3::DOWN = { 0, -1, 0 };
  191. inline constexpr Vector3 Vector3::FORWARD = { 0, 0, -1 };
  192. inline constexpr Vector3 Vector3::BACK = { 0, 0, 1 };
  193. inline constexpr Vector3 Vector3::MODEL_LEFT = { 1, 0, 0 };
  194. inline constexpr Vector3 Vector3::MODEL_RIGHT = { -1, 0, 0 };
  195. inline constexpr Vector3 Vector3::MODEL_TOP = { 0, 1, 0 };
  196. inline constexpr Vector3 Vector3::MODEL_BOTTOM = { 0, -1, 0 };
  197. inline constexpr Vector3 Vector3::MODEL_FRONT = { 0, 0, 1 };
  198. inline constexpr Vector3 Vector3::MODEL_REAR = { 0, 0, -1 };
  199. Vector3 Vector3::cross(const Vector3 &p_with) const {
  200. Vector3 ret(
  201. (y * p_with.z) - (z * p_with.y),
  202. (z * p_with.x) - (x * p_with.z),
  203. (x * p_with.y) - (y * p_with.x));
  204. return ret;
  205. }
  206. real_t Vector3::dot(const Vector3 &p_with) const {
  207. return x * p_with.x + y * p_with.y + z * p_with.z;
  208. }
  209. Vector3 Vector3::abs() const {
  210. return Vector3(Math::abs(x), Math::abs(y), Math::abs(z));
  211. }
  212. Vector3 Vector3::sign() const {
  213. return Vector3(SIGN(x), SIGN(y), SIGN(z));
  214. }
  215. Vector3 Vector3::floor() const {
  216. return Vector3(Math::floor(x), Math::floor(y), Math::floor(z));
  217. }
  218. Vector3 Vector3::ceil() const {
  219. return Vector3(Math::ceil(x), Math::ceil(y), Math::ceil(z));
  220. }
  221. Vector3 Vector3::round() const {
  222. return Vector3(Math::round(x), Math::round(y), Math::round(z));
  223. }
  224. Vector3 Vector3::lerp(const Vector3 &p_to, real_t p_weight) const {
  225. Vector3 res = *this;
  226. res.x = Math::lerp(res.x, p_to.x, p_weight);
  227. res.y = Math::lerp(res.y, p_to.y, p_weight);
  228. res.z = Math::lerp(res.z, p_to.z, p_weight);
  229. return res;
  230. }
  231. Vector3 Vector3::slerp(const Vector3 &p_to, real_t p_weight) const {
  232. // This method seems more complicated than it really is, since we write out
  233. // the internals of some methods for efficiency (mainly, checking length).
  234. real_t start_length_sq = length_squared();
  235. real_t end_length_sq = p_to.length_squared();
  236. if (unlikely(start_length_sq == 0.0f || end_length_sq == 0.0f)) {
  237. // Zero length vectors have no angle, so the best we can do is either lerp or throw an error.
  238. return lerp(p_to, p_weight);
  239. }
  240. Vector3 axis = cross(p_to);
  241. real_t axis_length_sq = axis.length_squared();
  242. if (unlikely(axis_length_sq == 0.0f)) {
  243. // Colinear vectors have no rotation axis or angle between them, so the best we can do is lerp.
  244. return lerp(p_to, p_weight);
  245. }
  246. axis /= Math::sqrt(axis_length_sq);
  247. real_t start_length = Math::sqrt(start_length_sq);
  248. real_t result_length = Math::lerp(start_length, Math::sqrt(end_length_sq), p_weight);
  249. real_t angle = angle_to(p_to);
  250. return rotated(axis, angle * p_weight) * (result_length / start_length);
  251. }
  252. Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const {
  253. Vector3 res = *this;
  254. res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight);
  255. res.y = Math::cubic_interpolate(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight);
  256. res.z = Math::cubic_interpolate(res.z, p_b.z, p_pre_a.z, p_post_b.z, p_weight);
  257. return res;
  258. }
  259. Vector3 Vector3::cubic_interpolate_in_time(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight, real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const {
  260. Vector3 res = *this;
  261. res.x = Math::cubic_interpolate_in_time(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
  262. res.y = Math::cubic_interpolate_in_time(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
  263. res.z = Math::cubic_interpolate_in_time(res.z, p_b.z, p_pre_a.z, p_post_b.z, p_weight, p_b_t, p_pre_a_t, p_post_b_t);
  264. return res;
  265. }
  266. Vector3 Vector3::bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, real_t p_t) const {
  267. Vector3 res = *this;
  268. res.x = Math::bezier_interpolate(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
  269. res.y = Math::bezier_interpolate(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
  270. res.z = Math::bezier_interpolate(res.z, p_control_1.z, p_control_2.z, p_end.z, p_t);
  271. return res;
  272. }
  273. Vector3 Vector3::bezier_derivative(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, real_t p_t) const {
  274. Vector3 res = *this;
  275. res.x = Math::bezier_derivative(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
  276. res.y = Math::bezier_derivative(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
  277. res.z = Math::bezier_derivative(res.z, p_control_1.z, p_control_2.z, p_end.z, p_t);
  278. return res;
  279. }
  280. real_t Vector3::distance_to(const Vector3 &p_to) const {
  281. return (p_to - *this).length();
  282. }
  283. real_t Vector3::distance_squared_to(const Vector3 &p_to) const {
  284. return (p_to - *this).length_squared();
  285. }
  286. Vector3 Vector3::posmod(real_t p_mod) const {
  287. return Vector3(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod));
  288. }
  289. Vector3 Vector3::posmodv(const Vector3 &p_modv) const {
  290. return Vector3(Math::fposmod(x, p_modv.x), Math::fposmod(y, p_modv.y), Math::fposmod(z, p_modv.z));
  291. }
  292. Vector3 Vector3::project(const Vector3 &p_to) const {
  293. return p_to * (dot(p_to) / p_to.length_squared());
  294. }
  295. real_t Vector3::angle_to(const Vector3 &p_to) const {
  296. return Math::atan2(cross(p_to).length(), dot(p_to));
  297. }
  298. real_t Vector3::signed_angle_to(const Vector3 &p_to, const Vector3 &p_axis) const {
  299. Vector3 cross_to = cross(p_to);
  300. real_t unsigned_angle = Math::atan2(cross_to.length(), dot(p_to));
  301. real_t sign = cross_to.dot(p_axis);
  302. return (sign < 0) ? -unsigned_angle : unsigned_angle;
  303. }
  304. Vector3 Vector3::direction_to(const Vector3 &p_to) const {
  305. Vector3 ret(p_to.x - x, p_to.y - y, p_to.z - z);
  306. ret.normalize();
  307. return ret;
  308. }
  309. Vector3 Vector3::get_any_perpendicular() const {
  310. // Return the any perpendicular vector by cross product with the Vector3.RIGHT or Vector3.UP,
  311. // whichever has the greater angle to the current vector with the sign of each element positive.
  312. // The only essence is "to avoid being parallel to the current vector", and there is no mathematical basis for using Vector3.RIGHT and Vector3.UP,
  313. // since it could be a different vector depending on the prior branching code Math::abs(x) <= Math::abs(y) && Math::abs(x) <= Math::abs(z).
  314. // However, it would be reasonable to use any of the axes of the basis, as it is simpler to calculate.
  315. ERR_FAIL_COND_V_MSG(is_zero_approx(), Vector3(0, 0, 0), "The Vector3 must not be zero.");
  316. return cross((Math::abs(x) <= Math::abs(y) && Math::abs(x) <= Math::abs(z)) ? Vector3::RIGHT : Vector3::UP).normalized();
  317. }
  318. /* Operators */
  319. constexpr Vector3 &Vector3::operator+=(const Vector3 &p_v) {
  320. x += p_v.x;
  321. y += p_v.y;
  322. z += p_v.z;
  323. return *this;
  324. }
  325. constexpr Vector3 Vector3::operator+(const Vector3 &p_v) const {
  326. return Vector3(x + p_v.x, y + p_v.y, z + p_v.z);
  327. }
  328. constexpr Vector3 &Vector3::operator-=(const Vector3 &p_v) {
  329. x -= p_v.x;
  330. y -= p_v.y;
  331. z -= p_v.z;
  332. return *this;
  333. }
  334. constexpr Vector3 Vector3::operator-(const Vector3 &p_v) const {
  335. return Vector3(x - p_v.x, y - p_v.y, z - p_v.z);
  336. }
  337. constexpr Vector3 &Vector3::operator*=(const Vector3 &p_v) {
  338. x *= p_v.x;
  339. y *= p_v.y;
  340. z *= p_v.z;
  341. return *this;
  342. }
  343. constexpr Vector3 Vector3::operator*(const Vector3 &p_v) const {
  344. return Vector3(x * p_v.x, y * p_v.y, z * p_v.z);
  345. }
  346. constexpr Vector3 &Vector3::operator/=(const Vector3 &p_v) {
  347. x /= p_v.x;
  348. y /= p_v.y;
  349. z /= p_v.z;
  350. return *this;
  351. }
  352. constexpr Vector3 Vector3::operator/(const Vector3 &p_v) const {
  353. return Vector3(x / p_v.x, y / p_v.y, z / p_v.z);
  354. }
  355. constexpr Vector3 &Vector3::operator*=(real_t p_scalar) {
  356. x *= p_scalar;
  357. y *= p_scalar;
  358. z *= p_scalar;
  359. return *this;
  360. }
  361. // Multiplication operators required to workaround issues with LLVM using implicit conversion
  362. // to Vector3i instead for integers where it should not.
  363. constexpr Vector3 operator*(float p_scalar, const Vector3 &p_vec) {
  364. return p_vec * p_scalar;
  365. }
  366. constexpr Vector3 operator*(double p_scalar, const Vector3 &p_vec) {
  367. return p_vec * p_scalar;
  368. }
  369. constexpr Vector3 operator*(int32_t p_scalar, const Vector3 &p_vec) {
  370. return p_vec * p_scalar;
  371. }
  372. constexpr Vector3 operator*(int64_t p_scalar, const Vector3 &p_vec) {
  373. return p_vec * p_scalar;
  374. }
  375. constexpr Vector3 Vector3::operator*(real_t p_scalar) const {
  376. return Vector3(x * p_scalar, y * p_scalar, z * p_scalar);
  377. }
  378. constexpr Vector3 &Vector3::operator/=(real_t p_scalar) {
  379. x /= p_scalar;
  380. y /= p_scalar;
  381. z /= p_scalar;
  382. return *this;
  383. }
  384. constexpr Vector3 Vector3::operator/(real_t p_scalar) const {
  385. return Vector3(x / p_scalar, y / p_scalar, z / p_scalar);
  386. }
  387. constexpr Vector3 Vector3::operator-() const {
  388. return Vector3(-x, -y, -z);
  389. }
  390. constexpr bool Vector3::operator==(const Vector3 &p_v) const {
  391. return x == p_v.x && y == p_v.y && z == p_v.z;
  392. }
  393. constexpr bool Vector3::operator!=(const Vector3 &p_v) const {
  394. return x != p_v.x || y != p_v.y || z != p_v.z;
  395. }
  396. constexpr bool Vector3::operator<(const Vector3 &p_v) const {
  397. if (x == p_v.x) {
  398. if (y == p_v.y) {
  399. return z < p_v.z;
  400. }
  401. return y < p_v.y;
  402. }
  403. return x < p_v.x;
  404. }
  405. constexpr bool Vector3::operator>(const Vector3 &p_v) const {
  406. if (x == p_v.x) {
  407. if (y == p_v.y) {
  408. return z > p_v.z;
  409. }
  410. return y > p_v.y;
  411. }
  412. return x > p_v.x;
  413. }
  414. constexpr bool Vector3::operator<=(const Vector3 &p_v) const {
  415. if (x == p_v.x) {
  416. if (y == p_v.y) {
  417. return z <= p_v.z;
  418. }
  419. return y < p_v.y;
  420. }
  421. return x < p_v.x;
  422. }
  423. constexpr bool Vector3::operator>=(const Vector3 &p_v) const {
  424. if (x == p_v.x) {
  425. if (y == p_v.y) {
  426. return z >= p_v.z;
  427. }
  428. return y > p_v.y;
  429. }
  430. return x > p_v.x;
  431. }
  432. _FORCE_INLINE_ Vector3 vec3_cross(const Vector3 &p_a, const Vector3 &p_b) {
  433. return p_a.cross(p_b);
  434. }
  435. _FORCE_INLINE_ real_t vec3_dot(const Vector3 &p_a, const Vector3 &p_b) {
  436. return p_a.dot(p_b);
  437. }
  438. real_t Vector3::length() const {
  439. real_t x2 = x * x;
  440. real_t y2 = y * y;
  441. real_t z2 = z * z;
  442. return Math::sqrt(x2 + y2 + z2);
  443. }
  444. real_t Vector3::length_squared() const {
  445. real_t x2 = x * x;
  446. real_t y2 = y * y;
  447. real_t z2 = z * z;
  448. return x2 + y2 + z2;
  449. }
  450. void Vector3::normalize() {
  451. real_t lengthsq = length_squared();
  452. if (lengthsq == 0) {
  453. x = y = z = 0;
  454. } else {
  455. real_t length = Math::sqrt(lengthsq);
  456. x /= length;
  457. y /= length;
  458. z /= length;
  459. }
  460. }
  461. Vector3 Vector3::normalized() const {
  462. Vector3 v = *this;
  463. v.normalize();
  464. return v;
  465. }
  466. bool Vector3::is_normalized() const {
  467. // use length_squared() instead of length() to avoid sqrt(), makes it more stringent.
  468. return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON);
  469. }
  470. Vector3 Vector3::inverse() const {
  471. return Vector3(1.0f / x, 1.0f / y, 1.0f / z);
  472. }
  473. void Vector3::zero() {
  474. x = y = z = 0;
  475. }
  476. // slide returns the component of the vector along the given plane, specified by its normal vector.
  477. Vector3 Vector3::slide(const Vector3 &p_normal) const {
  478. #ifdef MATH_CHECKS
  479. ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 " + p_normal.operator String() + " must be normalized.");
  480. #endif
  481. return *this - p_normal * dot(p_normal);
  482. }
  483. Vector3 Vector3::bounce(const Vector3 &p_normal) const {
  484. return -reflect(p_normal);
  485. }
  486. Vector3 Vector3::reflect(const Vector3 &p_normal) const {
  487. #ifdef MATH_CHECKS
  488. ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 " + p_normal.operator String() + " must be normalized.");
  489. #endif
  490. return 2.0f * p_normal * dot(p_normal) - *this;
  491. }
  492. template <>
  493. struct is_zero_constructible<Vector3> : std::true_type {};