mirror of
https://github.com/godotengine/godot-cpp.git
synced 2026-01-03 18:09:13 +03:00
Synchronize most shared variant code with Godot 4.4
(cherry picked from commit 075b517d96)
This commit is contained in:
@@ -59,12 +59,12 @@ struct [[nodiscard]] Vector3 {
|
||||
real_t coord[3] = { 0 };
|
||||
};
|
||||
|
||||
_FORCE_INLINE_ const real_t &operator[](const int p_axis) const {
|
||||
_FORCE_INLINE_ const real_t &operator[](int p_axis) const {
|
||||
DEV_ASSERT((unsigned int)p_axis < 3);
|
||||
return coord[p_axis];
|
||||
}
|
||||
|
||||
_FORCE_INLINE_ real_t &operator[](const int p_axis) {
|
||||
_FORCE_INLINE_ real_t &operator[](int p_axis) {
|
||||
DEV_ASSERT((unsigned int)p_axis < 3);
|
||||
return coord[p_axis];
|
||||
}
|
||||
@@ -100,36 +100,38 @@ struct [[nodiscard]] Vector3 {
|
||||
_FORCE_INLINE_ Vector3 normalized() const;
|
||||
_FORCE_INLINE_ bool is_normalized() const;
|
||||
_FORCE_INLINE_ Vector3 inverse() const;
|
||||
Vector3 limit_length(const real_t p_len = 1.0) const;
|
||||
Vector3 limit_length(real_t p_len = 1.0) const;
|
||||
|
||||
_FORCE_INLINE_ void zero();
|
||||
|
||||
void snap(const Vector3 p_val);
|
||||
void snapf(real_t p_val);
|
||||
Vector3 snapped(const Vector3 p_val) const;
|
||||
Vector3 snappedf(real_t p_val) const;
|
||||
void snap(const Vector3 &p_step);
|
||||
void snapf(real_t p_step);
|
||||
Vector3 snapped(const Vector3 &p_step) const;
|
||||
Vector3 snappedf(real_t p_step) const;
|
||||
|
||||
void rotate(const Vector3 &p_axis, const real_t p_angle);
|
||||
Vector3 rotated(const Vector3 &p_axis, const real_t p_angle) const;
|
||||
void rotate(const Vector3 &p_axis, real_t p_angle);
|
||||
Vector3 rotated(const Vector3 &p_axis, real_t p_angle) const;
|
||||
|
||||
/* Static Methods between 2 vector3s */
|
||||
|
||||
_FORCE_INLINE_ Vector3 lerp(const Vector3 &p_to, const real_t p_weight) const;
|
||||
_FORCE_INLINE_ Vector3 slerp(const Vector3 &p_to, const real_t p_weight) const;
|
||||
_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;
|
||||
_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;
|
||||
_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;
|
||||
_FORCE_INLINE_ Vector3 lerp(const Vector3 &p_to, real_t p_weight) const;
|
||||
_FORCE_INLINE_ Vector3 slerp(const Vector3 &p_to, real_t p_weight) const;
|
||||
_FORCE_INLINE_ Vector3 cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const;
|
||||
_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;
|
||||
_FORCE_INLINE_ Vector3 bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, real_t p_t) const;
|
||||
_FORCE_INLINE_ Vector3 bezier_derivative(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, real_t p_t) const;
|
||||
|
||||
Vector3 move_toward(const Vector3 &p_to, const real_t p_delta) const;
|
||||
Vector3 move_toward(const Vector3 &p_to, real_t p_delta) const;
|
||||
|
||||
Vector2 octahedron_encode() const;
|
||||
static Vector3 octahedron_decode(const Vector2 &p_oct);
|
||||
Vector2 octahedron_tangent_encode(const float sign) const;
|
||||
static Vector3 octahedron_tangent_decode(const Vector2 &p_oct, float *sign);
|
||||
Vector2 octahedron_tangent_encode(float p_sign) const;
|
||||
static Vector3 octahedron_tangent_decode(const Vector2 &p_oct, float *r_sign);
|
||||
|
||||
_FORCE_INLINE_ Vector3 cross(const Vector3 &p_with) const;
|
||||
_FORCE_INLINE_ real_t dot(const Vector3 &p_with) const;
|
||||
Basis outer(const Vector3 &p_with) const;
|
||||
_FORCE_INLINE_ Vector3 get_any_perpendicular() const;
|
||||
|
||||
_FORCE_INLINE_ Vector3 abs() const;
|
||||
_FORCE_INLINE_ Vector3 floor() const;
|
||||
@@ -142,7 +144,7 @@ struct [[nodiscard]] Vector3 {
|
||||
_FORCE_INLINE_ real_t distance_to(const Vector3 &p_to) const;
|
||||
_FORCE_INLINE_ real_t distance_squared_to(const Vector3 &p_to) const;
|
||||
|
||||
_FORCE_INLINE_ Vector3 posmod(const real_t p_mod) const;
|
||||
_FORCE_INLINE_ Vector3 posmod(real_t p_mod) const;
|
||||
_FORCE_INLINE_ Vector3 posmodv(const Vector3 &p_modv) const;
|
||||
_FORCE_INLINE_ Vector3 project(const Vector3 &p_to) const;
|
||||
|
||||
@@ -169,10 +171,10 @@ struct [[nodiscard]] Vector3 {
|
||||
_FORCE_INLINE_ Vector3 &operator/=(const Vector3 &p_v);
|
||||
_FORCE_INLINE_ Vector3 operator/(const Vector3 &p_v) const;
|
||||
|
||||
_FORCE_INLINE_ Vector3 &operator*=(const real_t p_scalar);
|
||||
_FORCE_INLINE_ Vector3 operator*(const real_t p_scalar) const;
|
||||
_FORCE_INLINE_ Vector3 &operator/=(const real_t p_scalar);
|
||||
_FORCE_INLINE_ Vector3 operator/(const real_t p_scalar) const;
|
||||
_FORCE_INLINE_ Vector3 &operator*=(real_t p_scalar);
|
||||
_FORCE_INLINE_ Vector3 operator*(real_t p_scalar) const;
|
||||
_FORCE_INLINE_ Vector3 &operator/=(real_t p_scalar);
|
||||
_FORCE_INLINE_ Vector3 operator/(real_t p_scalar) const;
|
||||
|
||||
_FORCE_INLINE_ Vector3 operator-() const;
|
||||
|
||||
@@ -187,7 +189,7 @@ struct [[nodiscard]] Vector3 {
|
||||
operator Vector3i() const;
|
||||
|
||||
_FORCE_INLINE_ Vector3() {}
|
||||
_FORCE_INLINE_ Vector3(const real_t p_x, const real_t p_y, const real_t p_z) {
|
||||
_FORCE_INLINE_ Vector3(real_t p_x, real_t p_y, real_t p_z) {
|
||||
x = p_x;
|
||||
y = p_y;
|
||||
z = p_z;
|
||||
@@ -227,14 +229,15 @@ Vector3 Vector3::round() const {
|
||||
return Vector3(Math::round(x), Math::round(y), Math::round(z));
|
||||
}
|
||||
|
||||
Vector3 Vector3::lerp(const Vector3 &p_to, const real_t p_weight) const {
|
||||
return Vector3(
|
||||
x + (p_weight * (p_to.x - x)),
|
||||
y + (p_weight * (p_to.y - y)),
|
||||
z + (p_weight * (p_to.z - z)));
|
||||
Vector3 Vector3::lerp(const Vector3 &p_to, real_t p_weight) const {
|
||||
Vector3 res = *this;
|
||||
res.x = Math::lerp(res.x, p_to.x, p_weight);
|
||||
res.y = Math::lerp(res.y, p_to.y, p_weight);
|
||||
res.z = Math::lerp(res.z, p_to.z, p_weight);
|
||||
return res;
|
||||
}
|
||||
|
||||
Vector3 Vector3::slerp(const Vector3 &p_to, const real_t p_weight) const {
|
||||
Vector3 Vector3::slerp(const Vector3 &p_to, real_t p_weight) const {
|
||||
// This method seems more complicated than it really is, since we write out
|
||||
// the internals of some methods for efficiency (mainly, checking length).
|
||||
real_t start_length_sq = length_squared();
|
||||
@@ -256,7 +259,7 @@ Vector3 Vector3::slerp(const Vector3 &p_to, const real_t p_weight) const {
|
||||
return rotated(axis, angle * p_weight) * (result_length / start_length);
|
||||
}
|
||||
|
||||
Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, const real_t p_weight) const {
|
||||
Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, const Vector3 &p_post_b, real_t p_weight) const {
|
||||
Vector3 res = *this;
|
||||
res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight);
|
||||
res.y = Math::cubic_interpolate(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight);
|
||||
@@ -264,7 +267,7 @@ Vector3 Vector3::cubic_interpolate(const Vector3 &p_b, const Vector3 &p_pre_a, c
|
||||
return res;
|
||||
}
|
||||
|
||||
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 {
|
||||
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 {
|
||||
Vector3 res = *this;
|
||||
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);
|
||||
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);
|
||||
@@ -272,17 +275,20 @@ Vector3 Vector3::cubic_interpolate_in_time(const Vector3 &p_b, const Vector3 &p_
|
||||
return res;
|
||||
}
|
||||
|
||||
Vector3 Vector3::bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, const real_t p_t) const {
|
||||
Vector3 Vector3::bezier_interpolate(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, real_t p_t) const {
|
||||
Vector3 res = *this;
|
||||
res.x = Math::bezier_interpolate(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
|
||||
res.y = Math::bezier_interpolate(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
|
||||
res.z = Math::bezier_interpolate(res.z, p_control_1.z, p_control_2.z, p_end.z, p_t);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Formula from Wikipedia article on Bezier curves. */
|
||||
real_t omt = (1.0 - p_t);
|
||||
real_t omt2 = omt * omt;
|
||||
real_t omt3 = omt2 * omt;
|
||||
real_t t2 = p_t * p_t;
|
||||
real_t t3 = t2 * p_t;
|
||||
|
||||
return res * omt3 + p_control_1 * omt2 * p_t * 3.0 + p_control_2 * omt * t2 * 3.0 + p_end * t3;
|
||||
Vector3 Vector3::bezier_derivative(const Vector3 &p_control_1, const Vector3 &p_control_2, const Vector3 &p_end, real_t p_t) const {
|
||||
Vector3 res = *this;
|
||||
res.x = Math::bezier_derivative(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
|
||||
res.y = Math::bezier_derivative(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
|
||||
res.z = Math::bezier_derivative(res.z, p_control_1.z, p_control_2.z, p_end.z, p_t);
|
||||
return res;
|
||||
}
|
||||
|
||||
real_t Vector3::distance_to(const Vector3 &p_to) const {
|
||||
@@ -293,7 +299,7 @@ real_t Vector3::distance_squared_to(const Vector3 &p_to) const {
|
||||
return (p_to - *this).length_squared();
|
||||
}
|
||||
|
||||
Vector3 Vector3::posmod(const real_t p_mod) const {
|
||||
Vector3 Vector3::posmod(real_t p_mod) const {
|
||||
return Vector3(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod));
|
||||
}
|
||||
|
||||
@@ -322,6 +328,16 @@ Vector3 Vector3::direction_to(const Vector3 &p_to) const {
|
||||
return ret;
|
||||
}
|
||||
|
||||
Vector3 Vector3::get_any_perpendicular() const {
|
||||
// Return the any perpendicular vector by cross product with the Vector3.RIGHT or Vector3.UP,
|
||||
// whichever has the greater angle to the current vector with the sign of each element positive.
|
||||
// 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,
|
||||
// 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).
|
||||
// However, it would be reasonable to use any of the axes of the basis, as it is simpler to calculate.
|
||||
ERR_FAIL_COND_V_MSG(is_zero_approx(), Vector3(0, 0, 0), "The Vector3 must not be zero.");
|
||||
return cross((Math::abs(x) <= Math::abs(y) && Math::abs(x) <= Math::abs(z)) ? Vector3(1, 0, 0) : Vector3(0, 1, 0)).normalized();
|
||||
}
|
||||
|
||||
/* Operators */
|
||||
|
||||
Vector3 &Vector3::operator+=(const Vector3 &p_v) {
|
||||
@@ -368,7 +384,7 @@ Vector3 Vector3::operator/(const Vector3 &p_v) const {
|
||||
return Vector3(x / p_v.x, y / p_v.y, z / p_v.z);
|
||||
}
|
||||
|
||||
Vector3 &Vector3::operator*=(const real_t p_scalar) {
|
||||
Vector3 &Vector3::operator*=(real_t p_scalar) {
|
||||
x *= p_scalar;
|
||||
y *= p_scalar;
|
||||
z *= p_scalar;
|
||||
@@ -378,34 +394,34 @@ Vector3 &Vector3::operator*=(const real_t p_scalar) {
|
||||
// Multiplication operators required to workaround issues with LLVM using implicit conversion
|
||||
// to Vector3i instead for integers where it should not.
|
||||
|
||||
_FORCE_INLINE_ Vector3 operator*(const float p_scalar, const Vector3 &p_vec) {
|
||||
_FORCE_INLINE_ Vector3 operator*(float p_scalar, const Vector3 &p_vec) {
|
||||
return p_vec * p_scalar;
|
||||
}
|
||||
|
||||
_FORCE_INLINE_ Vector3 operator*(const double p_scalar, const Vector3 &p_vec) {
|
||||
_FORCE_INLINE_ Vector3 operator*(double p_scalar, const Vector3 &p_vec) {
|
||||
return p_vec * p_scalar;
|
||||
}
|
||||
|
||||
_FORCE_INLINE_ Vector3 operator*(const int32_t p_scalar, const Vector3 &p_vec) {
|
||||
_FORCE_INLINE_ Vector3 operator*(int32_t p_scalar, const Vector3 &p_vec) {
|
||||
return p_vec * p_scalar;
|
||||
}
|
||||
|
||||
_FORCE_INLINE_ Vector3 operator*(const int64_t p_scalar, const Vector3 &p_vec) {
|
||||
_FORCE_INLINE_ Vector3 operator*(int64_t p_scalar, const Vector3 &p_vec) {
|
||||
return p_vec * p_scalar;
|
||||
}
|
||||
|
||||
Vector3 Vector3::operator*(const real_t p_scalar) const {
|
||||
Vector3 Vector3::operator*(real_t p_scalar) const {
|
||||
return Vector3(x * p_scalar, y * p_scalar, z * p_scalar);
|
||||
}
|
||||
|
||||
Vector3 &Vector3::operator/=(const real_t p_scalar) {
|
||||
Vector3 &Vector3::operator/=(real_t p_scalar) {
|
||||
x /= p_scalar;
|
||||
y /= p_scalar;
|
||||
z /= p_scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector3 Vector3::operator/(const real_t p_scalar) const {
|
||||
Vector3 Vector3::operator/(real_t p_scalar) const {
|
||||
return Vector3(x / p_scalar, y / p_scalar, z / p_scalar);
|
||||
}
|
||||
|
||||
@@ -519,9 +535,9 @@ void Vector3::zero() {
|
||||
// slide returns the component of the vector along the given plane, specified by its normal vector.
|
||||
Vector3 Vector3::slide(const Vector3 &p_normal) const {
|
||||
#ifdef MATH_CHECKS
|
||||
ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 must be normalized.");
|
||||
ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 " + p_normal.operator String() + " must be normalized.");
|
||||
#endif
|
||||
return *this - p_normal * this->dot(p_normal);
|
||||
return *this - p_normal * dot(p_normal);
|
||||
}
|
||||
|
||||
Vector3 Vector3::bounce(const Vector3 &p_normal) const {
|
||||
@@ -530,9 +546,9 @@ Vector3 Vector3::bounce(const Vector3 &p_normal) const {
|
||||
|
||||
Vector3 Vector3::reflect(const Vector3 &p_normal) const {
|
||||
#ifdef MATH_CHECKS
|
||||
ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 must be normalized.");
|
||||
ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector3(), "The normal Vector3 " + p_normal.operator String() + " must be normalized.");
|
||||
#endif
|
||||
return 2.0f * p_normal * this->dot(p_normal) - *this;
|
||||
return 2.0f * p_normal * dot(p_normal) - *this;
|
||||
}
|
||||
|
||||
} // namespace godot
|
||||
|
||||
Reference in New Issue
Block a user