vector3.h 18 KB

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