vector3.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /**************************************************************************/
  2. /* vector3.hpp */
  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 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 String;
  36. struct Basis;
  37. struct Vector2;
  38. struct Vector3i;
  39. struct _NO_DISCARD_ Vector3 {
  40. static const int AXIS_COUNT = 3;
  41. enum Axis {
  42. AXIS_X,
  43. AXIS_Y,
  44. AXIS_Z,
  45. };
  46. union {
  47. struct {
  48. real_t x;
  49. real_t y;
  50. real_t z;
  51. };
  52. real_t coord[3] = { 0 };
  53. };
  54. _FORCE_INLINE_ const real_t &operator[](const int p_axis) const {
  55. DEV_ASSERT((unsigned int)p_axis < 3);
  56. return coord[p_axis];
  57. }
  58. _FORCE_INLINE_ real_t &operator[](const 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(const real_t p_len = 1.0) const;
  87. _FORCE_INLINE_ void zero();
  88. void snap(const Vector3 p_val);
  89. void snapf(real_t p_val);
  90. Vector3 snapped(const Vector3 p_val) const;
  91. Vector3 snappedf(real_t p_val) const;
  92. void rotate(const Vector3 &p_axis, const real_t p_angle);
  93. Vector3 rotated(const Vector3 &p_axis, const real_t p_angle) const;
  94. /* Static Methods between 2 vector3s */
  95. _FORCE_INLINE_ Vector3 lerp(const Vector3 &p_to, const real_t p_weight) const;
  96. _FORCE_INLINE_ Vector3 slerp(const Vector3 &p_to, const 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, const 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, 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;
  99. _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;
  100. Vector3 move_toward(const Vector3 &p_to, const real_t p_delta) const;
  101. Vector2 octahedron_encode() const;
  102. static Vector3 octahedron_decode(const Vector2 &p_oct);
  103. Vector2 octahedron_tangent_encode(const float sign) const;
  104. static Vector3 octahedron_tangent_decode(const Vector2 &p_oct, float *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(const 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*=(const real_t p_scalar);
  139. _FORCE_INLINE_ Vector3 operator*(const real_t p_scalar) const;
  140. _FORCE_INLINE_ Vector3 &operator/=(const real_t p_scalar);
  141. _FORCE_INLINE_ Vector3 operator/(const 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(const real_t p_x, const real_t p_y, const 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, const real_t p_weight) const {
  184. return Vector3(
  185. x + (p_weight * (p_to.x - x)),
  186. y + (p_weight * (p_to.y - y)),
  187. z + (p_weight * (p_to.z - z)));
  188. }
  189. Vector3 Vector3::slerp(const Vector3 &p_to, const real_t p_weight) const {
  190. // This method seems more complicated than it really is, since we write out
  191. // the internals of some methods for efficiency (mainly, checking length).
  192. real_t start_length_sq = length_squared();
  193. real_t end_length_sq = p_to.length_squared();
  194. if (unlikely(start_length_sq == 0.0f || end_length_sq == 0.0f)) {
  195. // Zero length vectors have no angle, so the best we can do is either lerp or throw an error.
  196. return lerp(p_to, p_weight);
  197. }
  198. Vector3 axis = cross(p_to);
  199. real_t axis_length_sq = axis.length_squared();
  200. if (unlikely(axis_length_sq == 0.0f)) {
  201. // Colinear vectors have no rotation axis or angle between them, so the best we can do is lerp.
  202. return lerp(p_to, p_weight);
  203. }
  204. axis /= Math::sqrt(axis_length_sq);
  205. real_t start_length = Math::sqrt(start_length_sq);
  206. real_t result_length = Math::lerp(start_length, Math::sqrt(end_length_sq), p_weight);
  207. real_t angle = angle_to(p_to);
  208. return rotated(axis, angle * p_weight) * (result_length / start_length);
  209. }
  210. Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, const real_t p_weight) const {
  211. Vector3 res = *this;
  212. res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight);
  213. res.y = Math::cubic_interpolate(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight);
  214. res.z = Math::cubic_interpolate(res.z, p_b.z, p_pre_a.z, p_post_b.z, p_weight);
  215. return res;
  216. }
  217. 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 {
  218. Vector3 res = *this;
  219. 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);
  220. 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);
  221. 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);
  222. return res;
  223. }
  224. Vector3 Vector3::bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, const real_t p_t) const {
  225. Vector3 res = *this;
  226. /* Formula from Wikipedia article on Bezier curves. */
  227. real_t omt = (1.0 - p_t);
  228. real_t omt2 = omt * omt;
  229. real_t omt3 = omt2 * omt;
  230. real_t t2 = p_t * p_t;
  231. real_t t3 = t2 * p_t;
  232. return res * omt3 + p_control_1 * omt2 * p_t * 3.0 + p_control_2 * omt * t2 * 3.0 + p_end * t3;
  233. }
  234. real_t Vector3::distance_to(const Vector3 &p_to) const {
  235. return (p_to - *this).length();
  236. }
  237. real_t Vector3::distance_squared_to(const Vector3 &p_to) const {
  238. return (p_to - *this).length_squared();
  239. }
  240. Vector3 Vector3::posmod(const real_t p_mod) const {
  241. return Vector3(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod));
  242. }
  243. Vector3 Vector3::posmodv(const Vector3 &p_modv) const {
  244. return Vector3(Math::fposmod(x, p_modv.x), Math::fposmod(y, p_modv.y), Math::fposmod(z, p_modv.z));
  245. }
  246. Vector3 Vector3::project(const Vector3 &p_to) const {
  247. return p_to * (dot(p_to) / p_to.length_squared());
  248. }
  249. real_t Vector3::angle_to(const Vector3 &p_to) const {
  250. return Math::atan2(cross(p_to).length(), dot(p_to));
  251. }
  252. real_t Vector3::signed_angle_to(const Vector3 &p_to, const Vector3 &p_axis) const {
  253. Vector3 cross_to = cross(p_to);
  254. real_t unsigned_angle = Math::atan2(cross_to.length(), dot(p_to));
  255. real_t sign = cross_to.dot(p_axis);
  256. return (sign < 0) ? -unsigned_angle : unsigned_angle;
  257. }
  258. Vector3 Vector3::direction_to(const Vector3 &p_to) const {
  259. Vector3 ret(p_to.x - x, p_to.y - y, p_to.z - z);
  260. ret.normalize();
  261. return ret;
  262. }
  263. /* Operators */
  264. Vector3 &Vector3::operator+=(const Vector3 &p_v) {
  265. x += p_v.x;
  266. y += p_v.y;
  267. z += p_v.z;
  268. return *this;
  269. }
  270. Vector3 Vector3::operator+(const Vector3 &p_v) const {
  271. return Vector3(x + p_v.x, y + p_v.y, z + p_v.z);
  272. }
  273. Vector3 &Vector3::operator-=(const Vector3 &p_v) {
  274. x -= p_v.x;
  275. y -= p_v.y;
  276. z -= p_v.z;
  277. return *this;
  278. }
  279. Vector3 Vector3::operator-(const Vector3 &p_v) const {
  280. return Vector3(x - p_v.x, y - p_v.y, z - p_v.z);
  281. }
  282. Vector3 &Vector3::operator*=(const Vector3 &p_v) {
  283. x *= p_v.x;
  284. y *= p_v.y;
  285. z *= p_v.z;
  286. return *this;
  287. }
  288. Vector3 Vector3::operator*(const Vector3 &p_v) const {
  289. return Vector3(x * p_v.x, y * p_v.y, z * p_v.z);
  290. }
  291. Vector3 &Vector3::operator/=(const Vector3 &p_v) {
  292. x /= p_v.x;
  293. y /= p_v.y;
  294. z /= p_v.z;
  295. return *this;
  296. }
  297. Vector3 Vector3::operator/(const Vector3 &p_v) const {
  298. return Vector3(x / p_v.x, y / p_v.y, z / p_v.z);
  299. }
  300. Vector3 &Vector3::operator*=(const real_t p_scalar) {
  301. x *= p_scalar;
  302. y *= p_scalar;
  303. z *= p_scalar;
  304. return *this;
  305. }
  306. // Multiplication operators required to workaround issues with LLVM using implicit conversion
  307. // to Vector3i instead for integers where it should not.
  308. _FORCE_INLINE_ Vector3 operator*(const float p_scalar, const Vector3 &p_vec) {
  309. return p_vec * p_scalar;
  310. }
  311. _FORCE_INLINE_ Vector3 operator*(const double p_scalar, const Vector3 &p_vec) {
  312. return p_vec * p_scalar;
  313. }
  314. _FORCE_INLINE_ Vector3 operator*(const int32_t p_scalar, const Vector3 &p_vec) {
  315. return p_vec * p_scalar;
  316. }
  317. _FORCE_INLINE_ Vector3 operator*(const int64_t p_scalar, const Vector3 &p_vec) {
  318. return p_vec * p_scalar;
  319. }
  320. Vector3 Vector3::operator*(const real_t p_scalar) const {
  321. return Vector3(x * p_scalar, y * p_scalar, z * p_scalar);
  322. }
  323. Vector3 &Vector3::operator/=(const real_t p_scalar) {
  324. x /= p_scalar;
  325. y /= p_scalar;
  326. z /= p_scalar;
  327. return *this;
  328. }
  329. Vector3 Vector3::operator/(const real_t p_scalar) const {
  330. return Vector3(x / p_scalar, y / p_scalar, z / p_scalar);
  331. }
  332. Vector3 Vector3::operator-() const {
  333. return Vector3(-x, -y, -z);
  334. }
  335. bool Vector3::operator==(const Vector3 &p_v) const {
  336. return x == p_v.x && y == p_v.y && z == p_v.z;
  337. }
  338. bool Vector3::operator!=(const Vector3 &p_v) const {
  339. return x != p_v.x || y != p_v.y || z != p_v.z;
  340. }
  341. bool Vector3::operator<(const Vector3 &p_v) const {
  342. if (x == p_v.x) {
  343. if (y == p_v.y) {
  344. return z < p_v.z;
  345. }
  346. return y < p_v.y;
  347. }
  348. return x < p_v.x;
  349. }
  350. bool Vector3::operator>(const Vector3 &p_v) const {
  351. if (x == p_v.x) {
  352. if (y == p_v.y) {
  353. return z > p_v.z;
  354. }
  355. return y > p_v.y;
  356. }
  357. return x > p_v.x;
  358. }
  359. bool Vector3::operator<=(const Vector3 &p_v) const {
  360. if (x == p_v.x) {
  361. if (y == p_v.y) {
  362. return z <= p_v.z;
  363. }
  364. return y < p_v.y;
  365. }
  366. return x < p_v.x;
  367. }
  368. bool Vector3::operator>=(const Vector3 &p_v) const {
  369. if (x == p_v.x) {
  370. if (y == p_v.y) {
  371. return z >= p_v.z;
  372. }
  373. return y > p_v.y;
  374. }
  375. return x > p_v.x;
  376. }
  377. _FORCE_INLINE_ Vector3 vec3_cross(const Vector3 &p_a, const Vector3 &p_b) {
  378. return p_a.cross(p_b);
  379. }
  380. _FORCE_INLINE_ real_t vec3_dot(const Vector3 &p_a, const Vector3 &p_b) {
  381. return p_a.dot(p_b);
  382. }
  383. real_t Vector3::length() const {
  384. real_t x2 = x * x;
  385. real_t y2 = y * y;
  386. real_t z2 = z * z;
  387. return Math::sqrt(x2 + y2 + z2);
  388. }
  389. real_t Vector3::length_squared() const {
  390. real_t x2 = x * x;
  391. real_t y2 = y * y;
  392. real_t z2 = z * z;
  393. return x2 + y2 + z2;
  394. }
  395. void Vector3::normalize() {
  396. real_t lengthsq = length_squared();
  397. if (lengthsq == 0) {
  398. x = y = z = 0;
  399. } else {
  400. real_t length = Math::sqrt(lengthsq);
  401. x /= length;
  402. y /= length;
  403. z /= length;
  404. }
  405. }
  406. Vector3 Vector3::normalized() const {
  407. Vector3 v = *this;
  408. v.normalize();
  409. return v;
  410. }
  411. bool Vector3::is_normalized() const {
  412. // use length_squared() instead of length() to avoid sqrt(), makes it more stringent.
  413. return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON);
  414. }
  415. Vector3 Vector3::inverse() const {
  416. return Vector3(1.0f / x, 1.0f / y, 1.0f / z);
  417. }
  418. void Vector3::zero() {
  419. x = y = z = 0;
  420. }
  421. // slide returns the component of the vector along the given plane, specified by its normal vector.
  422. Vector3 Vector3::slide(const Vector3 &p_normal) const {
  423. #ifdef MATH_CHECKS
  424. ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 must be normalized.");
  425. #endif
  426. return *this - p_normal * this->dot(p_normal);
  427. }
  428. Vector3 Vector3::bounce(const Vector3 &p_normal) const {
  429. return -reflect(p_normal);
  430. }
  431. Vector3 Vector3::reflect(const Vector3 &p_normal) const {
  432. #ifdef MATH_CHECKS
  433. ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 must be normalized.");
  434. #endif
  435. return 2.0f * p_normal * this->dot(p_normal) - *this;
  436. }
  437. } // namespace godot
  438. #endif // GODOT_VECTOR3_HPP