Debian Codegraph @

core/math/vector3.h

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