vector3.h 17 KB

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