vector3.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. class String;
  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[](const int p_axis) const {
  54. DEV_ASSERT((unsigned int)p_axis < 3);
  55. return coord[p_axis];
  56. }
  57. _FORCE_INLINE_ real_t &operator[](const 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 max(const Vector3 &p_vector3) const {
  71. return Vector3(MAX(x, p_vector3.x), MAX(y, p_vector3.y), MAX(z, p_vector3.z));
  72. }
  73. _FORCE_INLINE_ real_t length() const;
  74. _FORCE_INLINE_ real_t length_squared() const;
  75. _FORCE_INLINE_ void normalize();
  76. _FORCE_INLINE_ Vector3 normalized() const;
  77. _FORCE_INLINE_ bool is_normalized() const;
  78. _FORCE_INLINE_ Vector3 inverse() const;
  79. Vector3 limit_length(const real_t p_len = 1.0) const;
  80. _FORCE_INLINE_ void zero();
  81. void snap(const Vector3 p_val);
  82. Vector3 snapped(const Vector3 p_val) const;
  83. void rotate(const Vector3 &p_axis, const real_t p_angle);
  84. Vector3 rotated(const Vector3 &p_axis, const real_t p_angle) const;
  85. /* Static Methods between 2 vector3s */
  86. _FORCE_INLINE_ Vector3 lerp(const Vector3 &p_to, const real_t p_weight) const;
  87. _FORCE_INLINE_ Vector3 slerp(const Vector3 &p_to, const real_t p_weight) const;
  88. _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;
  89. _FORCE_INLINE_ Vector3 cubic_interpolate_in_time(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, const real_t p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const;
  90. _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;
  91. _FORCE_INLINE_ Vector3 bezier_derivative(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. Vector2 octahedron_tangent_encode(const float sign) const;
  96. static Vector3 octahedron_tangent_decode(const Vector2 &p_oct, float *sign);
  97. _FORCE_INLINE_ Vector3 cross(const Vector3 &p_with) const;
  98. _FORCE_INLINE_ real_t dot(const Vector3 &p_with) const;
  99. Basis outer(const Vector3 &p_with) const;
  100. _FORCE_INLINE_ Vector3 abs() const;
  101. _FORCE_INLINE_ Vector3 floor() const;
  102. _FORCE_INLINE_ Vector3 sign() const;
  103. _FORCE_INLINE_ Vector3 ceil() const;
  104. _FORCE_INLINE_ Vector3 round() const;
  105. Vector3 clamp(const Vector3 &p_min, const Vector3 &p_max) const;
  106. _FORCE_INLINE_ real_t distance_to(const Vector3 &p_to) const;
  107. _FORCE_INLINE_ real_t distance_squared_to(const Vector3 &p_to) const;
  108. _FORCE_INLINE_ Vector3 posmod(const real_t p_mod) const;
  109. _FORCE_INLINE_ Vector3 posmodv(const Vector3 &p_modv) const;
  110. _FORCE_INLINE_ Vector3 project(const Vector3 &p_to) const;
  111. _FORCE_INLINE_ real_t angle_to(const Vector3 &p_to) const;
  112. _FORCE_INLINE_ real_t signed_angle_to(const Vector3 &p_to, const Vector3 &p_axis) const;
  113. _FORCE_INLINE_ Vector3 direction_to(const Vector3 &p_to) const;
  114. _FORCE_INLINE_ Vector3 slide(const Vector3 &p_normal) const;
  115. _FORCE_INLINE_ Vector3 bounce(const Vector3 &p_normal) const;
  116. _FORCE_INLINE_ Vector3 reflect(const Vector3 &p_normal) const;
  117. bool is_equal_approx(const Vector3 &p_v) const;
  118. bool is_zero_approx() const;
  119. bool is_finite() const;
  120. /* Operators */
  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 Vector3 &p_v);
  126. _FORCE_INLINE_ Vector3 operator*(const Vector3 &p_v) const;
  127. _FORCE_INLINE_ Vector3 &operator/=(const Vector3 &p_v);
  128. _FORCE_INLINE_ Vector3 operator/(const Vector3 &p_v) const;
  129. _FORCE_INLINE_ Vector3 &operator*=(const real_t p_scalar);
  130. _FORCE_INLINE_ Vector3 operator*(const real_t p_scalar) const;
  131. _FORCE_INLINE_ Vector3 &operator/=(const real_t p_scalar);
  132. _FORCE_INLINE_ Vector3 operator/(const real_t p_scalar) const;
  133. _FORCE_INLINE_ Vector3 operator-() const;
  134. _FORCE_INLINE_ bool operator==(const Vector3 &p_v) const;
  135. _FORCE_INLINE_ bool operator!=(const Vector3 &p_v) const;
  136. _FORCE_INLINE_ bool operator<(const Vector3 &p_v) const;
  137. _FORCE_INLINE_ bool operator<=(const Vector3 &p_v) const;
  138. _FORCE_INLINE_ bool operator>(const Vector3 &p_v) const;
  139. _FORCE_INLINE_ bool operator>=(const Vector3 &p_v) const;
  140. operator String() const;
  141. operator Vector3i() const;
  142. _FORCE_INLINE_ Vector3() {}
  143. _FORCE_INLINE_ Vector3(const real_t p_x, const real_t p_y, const real_t p_z) {
  144. x = p_x;
  145. y = p_y;
  146. z = p_z;
  147. }
  148. };
  149. Vector3 Vector3::cross(const Vector3 &p_with) const {
  150. Vector3 ret(
  151. (y * p_with.z) - (z * p_with.y),
  152. (z * p_with.x) - (x * p_with.z),
  153. (x * p_with.y) - (y * p_with.x));
  154. return ret;
  155. }
  156. real_t Vector3::dot(const Vector3 &p_with) const {
  157. return x * p_with.x + y * p_with.y + z * p_with.z;
  158. }
  159. Vector3 Vector3::abs() const {
  160. return Vector3(Math::abs(x), Math::abs(y), Math::abs(z));
  161. }
  162. Vector3 Vector3::sign() const {
  163. return Vector3(SIGN(x), SIGN(y), SIGN(z));
  164. }
  165. Vector3 Vector3::floor() const {
  166. return Vector3(Math::floor(x), Math::floor(y), Math::floor(z));
  167. }
  168. Vector3 Vector3::ceil() const {
  169. return Vector3(Math::ceil(x), Math::ceil(y), Math::ceil(z));
  170. }
  171. Vector3 Vector3::round() const {
  172. return Vector3(Math::round(x), Math::round(y), Math::round(z));
  173. }
  174. Vector3 Vector3::lerp(const Vector3 &p_to, const real_t p_weight) const {
  175. Vector3 res = *this;
  176. res.x = Math::lerp(res.x, p_to.x, p_weight);
  177. res.y = Math::lerp(res.y, p_to.y, p_weight);
  178. res.z = Math::lerp(res.z, p_to.z, p_weight);
  179. return res;
  180. }
  181. Vector3 Vector3::slerp(const Vector3 &p_to, const real_t p_weight) const {
  182. // This method seems more complicated than it really is, since we write out
  183. // the internals of some methods for efficiency (mainly, checking length).
  184. real_t start_length_sq = length_squared();
  185. real_t end_length_sq = p_to.length_squared();
  186. if (unlikely(start_length_sq == 0.0f || end_length_sq == 0.0f)) {
  187. // Zero length vectors have no angle, so the best we can do is either lerp or throw an error.
  188. return lerp(p_to, p_weight);
  189. }
  190. Vector3 axis = cross(p_to);
  191. real_t axis_length_sq = axis.length_squared();
  192. if (unlikely(axis_length_sq == 0.0f)) {
  193. // Colinear vectors have no rotation axis or angle between them, so the best we can do is lerp.
  194. return lerp(p_to, p_weight);
  195. }
  196. axis /= Math::sqrt(axis_length_sq);
  197. real_t start_length = Math::sqrt(start_length_sq);
  198. real_t result_length = Math::lerp(start_length, Math::sqrt(end_length_sq), p_weight);
  199. real_t angle = angle_to(p_to);
  200. return rotated(axis, angle * p_weight) * (result_length / start_length);
  201. }
  202. Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, const real_t p_weight) const {
  203. Vector3 res = *this;
  204. res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight);
  205. res.y = Math::cubic_interpolate(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight);
  206. res.z = Math::cubic_interpolate(res.z, p_b.z, p_pre_a.z, p_post_b.z, p_weight);
  207. return res;
  208. }
  209. Vector3 Vector3::cubic_interpolate_in_time(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, const real_t p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const {
  210. Vector3 res = *this;
  211. 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);
  212. 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);
  213. 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);
  214. return res;
  215. }
  216. Vector3 Vector3::bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, const real_t p_t) const {
  217. Vector3 res = *this;
  218. res.x = Math::bezier_interpolate(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
  219. res.y = Math::bezier_interpolate(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
  220. res.z = Math::bezier_interpolate(res.z, p_control_1.z, p_control_2.z, p_end.z, p_t);
  221. return res;
  222. }
  223. Vector3 Vector3::bezier_derivative(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, const real_t p_t) const {
  224. Vector3 res = *this;
  225. res.x = Math::bezier_derivative(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
  226. res.y = Math::bezier_derivative(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
  227. res.z = Math::bezier_derivative(res.z, p_control_1.z, p_control_2.z, p_end.z, p_t);
  228. return res;
  229. }
  230. real_t Vector3::distance_to(const Vector3 &p_to) const {
  231. return (p_to - *this).length();
  232. }
  233. real_t Vector3::distance_squared_to(const Vector3 &p_to) const {
  234. return (p_to - *this).length_squared();
  235. }
  236. Vector3 Vector3::posmod(const real_t p_mod) const {
  237. return Vector3(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod));
  238. }
  239. Vector3 Vector3::posmodv(const Vector3 &p_modv) const {
  240. return Vector3(Math::fposmod(x, p_modv.x), Math::fposmod(y, p_modv.y), Math::fposmod(z, p_modv.z));
  241. }
  242. Vector3 Vector3::project(const Vector3 &p_to) const {
  243. return p_to * (dot(p_to) / p_to.length_squared());
  244. }
  245. real_t Vector3::angle_to(const Vector3 &p_to) const {
  246. return Math::atan2(cross(p_to).length(), dot(p_to));
  247. }
  248. real_t Vector3::signed_angle_to(const Vector3 &p_to, const Vector3 &p_axis) const {
  249. Vector3 cross_to = cross(p_to);
  250. real_t unsigned_angle = Math::atan2(cross_to.length(), dot(p_to));
  251. real_t sign = cross_to.dot(p_axis);
  252. return (sign < 0) ? -unsigned_angle : unsigned_angle;
  253. }
  254. Vector3 Vector3::direction_to(const Vector3 &p_to) const {
  255. Vector3 ret(p_to.x - x, p_to.y - y, p_to.z - z);
  256. ret.normalize();
  257. return ret;
  258. }
  259. /* Operators */
  260. Vector3 &Vector3::operator+=(const Vector3 &p_v) {
  261. x += p_v.x;
  262. y += p_v.y;
  263. z += p_v.z;
  264. return *this;
  265. }
  266. Vector3 Vector3::operator+(const Vector3 &p_v) const {
  267. return Vector3(x + p_v.x, y + p_v.y, z + p_v.z);
  268. }
  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 real_t p_scalar) {
  297. x *= p_scalar;
  298. y *= p_scalar;
  299. z *= p_scalar;
  300. return *this;
  301. }
  302. // Multiplication operators required to workaround issues with LLVM using implicit conversion
  303. // to Vector3i instead for integers where it should not.
  304. _FORCE_INLINE_ Vector3 operator*(const float p_scalar, const Vector3 &p_vec) {
  305. return p_vec * p_scalar;
  306. }
  307. _FORCE_INLINE_ Vector3 operator*(const double p_scalar, const Vector3 &p_vec) {
  308. return p_vec * p_scalar;
  309. }
  310. _FORCE_INLINE_ Vector3 operator*(const int32_t p_scalar, const Vector3 &p_vec) {
  311. return p_vec * p_scalar;
  312. }
  313. _FORCE_INLINE_ Vector3 operator*(const int64_t p_scalar, const Vector3 &p_vec) {
  314. return p_vec * p_scalar;
  315. }
  316. Vector3 Vector3::operator*(const real_t p_scalar) const {
  317. return Vector3(x * p_scalar, y * p_scalar, z * p_scalar);
  318. }
  319. Vector3 &Vector3::operator/=(const real_t p_scalar) {
  320. x /= p_scalar;
  321. y /= p_scalar;
  322. z /= p_scalar;
  323. return *this;
  324. }
  325. Vector3 Vector3::operator/(const real_t p_scalar) const {
  326. return Vector3(x / p_scalar, y / p_scalar, z / p_scalar);
  327. }
  328. Vector3 Vector3::operator-() const {
  329. return Vector3(-x, -y, -z);
  330. }
  331. bool Vector3::operator==(const Vector3 &p_v) const {
  332. return x == p_v.x && y == p_v.y && z == p_v.z;
  333. }
  334. bool Vector3::operator!=(const Vector3 &p_v) const {
  335. return x != p_v.x || y != p_v.y || z != p_v.z;
  336. }
  337. bool Vector3::operator<(const Vector3 &p_v) const {
  338. if (x == p_v.x) {
  339. if (y == p_v.y) {
  340. return z < p_v.z;
  341. }
  342. return y < p_v.y;
  343. }
  344. return x < p_v.x;
  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. _FORCE_INLINE_ Vector3 vec3_cross(const Vector3 &p_a, const Vector3 &p_b) {
  374. return p_a.cross(p_b);
  375. }
  376. _FORCE_INLINE_ real_t vec3_dot(const Vector3 &p_a, const Vector3 &p_b) {
  377. return p_a.dot(p_b);
  378. }
  379. real_t Vector3::length() const {
  380. real_t x2 = x * x;
  381. real_t y2 = y * y;
  382. real_t z2 = z * z;
  383. return Math::sqrt(x2 + y2 + z2);
  384. }
  385. real_t Vector3::length_squared() const {
  386. real_t x2 = x * x;
  387. real_t y2 = y * y;
  388. real_t z2 = z * z;
  389. return x2 + y2 + z2;
  390. }
  391. void Vector3::normalize() {
  392. real_t lengthsq = length_squared();
  393. if (lengthsq == 0) {
  394. x = y = z = 0;
  395. } else {
  396. real_t length = Math::sqrt(lengthsq);
  397. x /= length;
  398. y /= length;
  399. z /= length;
  400. }
  401. }
  402. Vector3 Vector3::normalized() const {
  403. Vector3 v = *this;
  404. v.normalize();
  405. return v;
  406. }
  407. bool Vector3::is_normalized() const {
  408. // use length_squared() instead of length() to avoid sqrt(), makes it more stringent.
  409. return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON);
  410. }
  411. Vector3 Vector3::inverse() const {
  412. return Vector3(1.0f / x, 1.0f / y, 1.0f / z);
  413. }
  414. void Vector3::zero() {
  415. x = y = z = 0;
  416. }
  417. // slide returns the component of the vector along the given plane, specified by its normal vector.
  418. Vector3 Vector3::slide(const Vector3 &p_normal) const {
  419. #ifdef MATH_CHECKS
  420. ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 must be normalized.");
  421. #endif
  422. return *this - p_normal * this->dot(p_normal);
  423. }
  424. Vector3 Vector3::bounce(const Vector3 &p_normal) const {
  425. return -reflect(p_normal);
  426. }
  427. Vector3 Vector3::reflect(const Vector3 &p_normal) const {
  428. #ifdef MATH_CHECKS
  429. ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 must be normalized.");
  430. #endif
  431. return 2.0f * p_normal * this->dot(p_normal) - *this;
  432. }
  433. #endif // VECTOR3_H