Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Vector2/2i/3/3i/4/4i to match the engine #857

Merged
merged 2 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions include/godot_cpp/variant/vector2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Vector2 {
friend class Variant;

public:
static const int AXIS_COUNT = 2;

enum Axis {
AXIS_X,
AXIS_Y,
Expand Down Expand Up @@ -74,10 +76,6 @@ class Vector2 {
return coord[p_idx];
}

_FORCE_INLINE_ void set_all(const real_t p_value) {
x = y = p_value;
}

_FORCE_INLINE_ Vector2::Axis min_axis_index() const {
return x < y ? Vector2::AXIS_X : Vector2::AXIS_Y;
}
Expand Down Expand Up @@ -119,6 +117,7 @@ class Vector2 {
_FORCE_INLINE_ Vector2 lerp(const Vector2 &p_to, const real_t p_weight) const;
_FORCE_INLINE_ Vector2 slerp(const Vector2 &p_to, const real_t p_weight) const;
_FORCE_INLINE_ Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight) const;
_FORCE_INLINE_ Vector2 cubic_interpolate_in_time(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &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_ Vector2 bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t) const;

Vector2 move_toward(const Vector2 &p_to, const real_t p_delta) const;
Expand All @@ -128,6 +127,7 @@ class Vector2 {
Vector2 reflect(const Vector2 &p_normal) const;

bool is_equal_approx(const Vector2 &p_v) const;
bool is_zero_approx() const;

Vector2 operator+(const Vector2 &p_v) const;
void operator+=(const Vector2 &p_v);
Expand Down Expand Up @@ -275,6 +275,13 @@ Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, c
return res;
}

Vector2 Vector2::cubic_interpolate_in_time(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &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 {
Vector2 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);
return res;
}

Vector2 Vector2::bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t) const {
Vector2 res = *this;

Expand Down
4 changes: 3 additions & 1 deletion include/godot_cpp/variant/vector2i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Vector2i {
friend class Variant;

public:
static const int AXIS_COUNT = 2;

enum Axis {
AXIS_X,
AXIS_Y,
Expand Down Expand Up @@ -122,7 +124,7 @@ class Vector2i {

real_t aspect() const { return width / (real_t)height; }
Vector2i sign() const { return Vector2i(SIGN(x), SIGN(y)); }
Vector2i abs() const { return Vector2i(ABS(x), ABS(y)); }
Vector2i abs() const { return Vector2i(Math::abs(x), Math::abs(y)); }
Vector2i clamp(const Vector2i &p_min, const Vector2i &p_max) const;

operator String() const;
Expand Down
32 changes: 24 additions & 8 deletions include/godot_cpp/variant/vector3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class Vector3 {
friend class Variant;

public:
static const int AXIS_COUNT = 3;

enum Axis {
AXIS_X,
AXIS_Y,
Expand All @@ -73,13 +75,6 @@ class Vector3 {
return coord[p_axis];
}

void set_axis(const int p_axis, const real_t p_value);
real_t get_axis(const int p_axis) const;

_FORCE_INLINE_ void set_all(const real_t p_value) {
x = y = z = p_value;
}

_FORCE_INLINE_ Vector3::Axis min_axis_index() const {
return x < y ? (x < z ? Vector3::AXIS_X : Vector3::AXIS_Z) : (y < z ? Vector3::AXIS_Y : Vector3::AXIS_Z);
}
Expand Down Expand Up @@ -110,12 +105,15 @@ class Vector3 {
_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;

Vector3 move_toward(const Vector3 &p_to, const 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);

_FORCE_INLINE_ Vector3 cross(const Vector3 &p_with) const;
_FORCE_INLINE_ real_t dot(const Vector3 &p_with) const;
Expand Down Expand Up @@ -144,6 +142,7 @@ class Vector3 {
_FORCE_INLINE_ Vector3 reflect(const Vector3 &p_normal) const;

bool is_equal_approx(const Vector3 &p_v) const;
bool is_zero_approx() const;

/* Operators */

Expand Down Expand Up @@ -222,16 +221,25 @@ Vector3 Vector3::lerp(const Vector3 &p_to, const real_t p_weight) const {
}

Vector3 Vector3::slerp(const Vector3 &p_to, const 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();
real_t end_length_sq = p_to.length_squared();
if (unlikely(start_length_sq == 0.0f || end_length_sq == 0.0f)) {
// Zero length vectors have no angle, so the best we can do is either lerp or throw an error.
return lerp(p_to, p_weight);
}
Vector3 axis = cross(p_to);
real_t axis_length_sq = axis.length_squared();
if (unlikely(axis_length_sq == 0.0f)) {
// Colinear vectors have no rotation axis or angle between them, so the best we can do is lerp.
return lerp(p_to, p_weight);
}
axis /= Math::sqrt(axis_length_sq);
real_t start_length = Math::sqrt(start_length_sq);
real_t result_length = Math::lerp(start_length, Math::sqrt(end_length_sq), p_weight);
real_t angle = angle_to(p_to);
return rotated(cross(p_to).normalized(), angle * p_weight) * (result_length / start_length);
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 {
Expand All @@ -242,6 +250,14 @@ 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 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);
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);
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 res = *this;

Expand Down
7 changes: 3 additions & 4 deletions include/godot_cpp/variant/vector3i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Vector3i {
friend class Variant;

public:
static const int AXIS_COUNT = 3;

enum Axis {
AXIS_X,
AXIS_Y,
Expand All @@ -71,9 +73,6 @@ class Vector3i {
return coord[p_axis];
}

void set_axis(const int p_axis, const int32_t p_value);
int32_t get_axis(const int p_axis) const;

Vector3i::Axis min_axis_index() const;
Vector3i::Axis max_axis_index() const;

Expand Down Expand Up @@ -135,7 +134,7 @@ double Vector3i::length() const {
}

Vector3i Vector3i::abs() const {
return Vector3i(ABS(x), ABS(y), ABS(z));
return Vector3i(Math::abs(x), Math::abs(y), Math::abs(z));
}

Vector3i Vector3i::sign() const {
Expand Down
72 changes: 39 additions & 33 deletions include/godot_cpp/variant/vector4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class Vector4 {
friend class Variant;

public:
static const int AXIS_COUNT = 4;

enum Axis {
AXIS_X,
AXIS_Y,
Expand All @@ -61,30 +63,46 @@ class Vector4 {
real_t components[4] = { 0, 0, 0, 0 };
};

_FORCE_INLINE_ real_t &operator[](int idx) {
return components[idx];
_FORCE_INLINE_ real_t &operator[](const int p_axis) {
DEV_ASSERT((unsigned int)p_axis < 4);
return components[p_axis];
}
_FORCE_INLINE_ const real_t &operator[](int idx) const {
return components[idx];
_FORCE_INLINE_ const real_t &operator[](const int p_axis) const {
DEV_ASSERT((unsigned int)p_axis < 4);
return components[p_axis];
}

Vector4::Axis min_axis_index() const;
Vector4::Axis max_axis_index() const;

_FORCE_INLINE_ real_t length_squared() const;
bool is_equal_approx(const Vector4 &p_vec4) const;
bool is_zero_approx() const;
real_t length() const;
void normalize();
Vector4 normalized() const;
bool is_normalized() const;

real_t distance_to(const Vector4 &p_to) const;
real_t distance_squared_to(const Vector4 &p_to) const;
Vector4 direction_to(const Vector4 &p_to) const;

Vector4 abs() const;
Vector4 sign() const;
Vector4 floor() const;
Vector4 ceil() const;
Vector4 round() const;
Vector4 lerp(const Vector4 &p_to, const real_t p_weight) const;
Vector4 cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, const real_t p_weight) const;
Vector4 cubic_interpolate_in_time(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &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;

Vector4::Axis min_axis_index() const;
Vector4::Axis max_axis_index() const;
Vector4 posmod(const real_t p_mod) const;
Vector4 posmodv(const Vector4 &p_modv) const;
void snap(const Vector4 &p_step);
Vector4 snapped(const Vector4 &p_step) const;
Vector4 clamp(const Vector4 &p_min, const Vector4 &p_max) const;

Vector4 inverse() const;
Vector4 lerp(const Vector4 &p_to, const real_t p_weight) const;
_FORCE_INLINE_ real_t dot(const Vector4 &p_vec4) const;

_FORCE_INLINE_ void operator+=(const Vector4 &p_vec4);
Expand Down Expand Up @@ -197,7 +215,7 @@ Vector4 Vector4::operator/(const Vector4 &p_vec4) const {
}

Vector4 Vector4::operator-() const {
return Vector4(x, y, z, w);
return Vector4(-x, -y, -z, -w);
}

Vector4 Vector4::operator*(const real_t &s) const {
Expand All @@ -221,63 +239,51 @@ bool Vector4::operator<(const Vector4 &p_v) const {
if (y == p_v.y) {
if (z == p_v.z) {
return w < p_v.w;
} else {
return z < p_v.z;
}
} else {
return y < p_v.y;
return z < p_v.z;
}
} else {
return x < p_v.x;
return y < p_v.y;
}
return x < p_v.x;
}

bool Vector4::operator>(const Vector4 &p_v) const {
if (x == p_v.x) {
if (y == p_v.y) {
if (z == p_v.z) {
return w > p_v.w;
} else {
return z > p_v.z;
}
} else {
return y > p_v.y;
return z > p_v.z;
}
} else {
return x > p_v.x;
return y > p_v.y;
}
return x > p_v.x;
}

bool Vector4::operator<=(const Vector4 &p_v) const {
if (x == p_v.x) {
if (y == p_v.y) {
if (z == p_v.z) {
return w <= p_v.w;
} else {
return z < p_v.z;
}
} else {
return y < p_v.y;
return z < p_v.z;
}
} else {
return x < p_v.x;
return y < p_v.y;
}
return x < p_v.x;
}

bool Vector4::operator>=(const Vector4 &p_v) const {
if (x == p_v.x) {
if (y == p_v.y) {
if (z == p_v.z) {
return w >= p_v.w;
} else {
return z > p_v.z;
}
} else {
return y > p_v.y;
return z > p_v.z;
}
} else {
return x > p_v.x;
return y > p_v.y;
}
return x > p_v.x;
}

_FORCE_INLINE_ Vector4 operator*(const float p_scalar, const Vector4 &p_vec) {
Expand All @@ -298,4 +304,4 @@ _FORCE_INLINE_ Vector4 operator*(const int64_t p_scalar, const Vector4 &p_vec) {

} // namespace godot

#endif // GODOT_VECTOR3_HPP
#endif // GODOT_VECTOR4_HPP
8 changes: 6 additions & 2 deletions include/godot_cpp/variant/vector4i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Vector4i {
friend class Variant;

public:
static const int AXIS_COUNT = 4;

enum Axis {
AXIS_X,
AXIS_Y,
Expand All @@ -64,10 +66,12 @@ class Vector4i {
};

_FORCE_INLINE_ const int32_t &operator[](const int p_axis) const {
DEV_ASSERT((unsigned int)p_axis < 4);
return coord[p_axis];
}

_FORCE_INLINE_ int32_t &operator[](const int p_axis) {
DEV_ASSERT((unsigned int)p_axis < 4);
return coord[p_axis];
}

Expand Down Expand Up @@ -137,11 +141,11 @@ double Vector4i::length() const {
}

Vector4i Vector4i::abs() const {
return Vector4i(ABS(x), ABS(y), ABS(z), ABS(w));
return Vector4i(Math::abs(x), Math::abs(y), Math::abs(z), Math::abs(w));
}

Vector4i Vector4i::sign() const {
return Vector4i(SIGN(x), SIGN(y), SIGN(z), SIGN(w));
return Vector4i(Math::sign(x), Math::sign(y), Math::sign(z), Math::sign(w));
}

/* Operators */
Expand Down
4 changes: 4 additions & 0 deletions src/variant/vector2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ bool Vector2::is_equal_approx(const Vector2 &p_v) const {
return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y);
}

bool Vector2::is_zero_approx() const {
return Math::is_zero_approx(x) && Math::is_zero_approx(y);
}

Vector2::operator String() const {
return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ")";
}
Expand Down
Loading