From 5b64481c8781496019c978b045a3140df22866f8 Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 14 Mar 2022 09:12:57 +0100 Subject: [PATCH] Finished the rest of vector2. --- core/math/vector2.c | 81 +++++++++++++------------- core/math/vector2.h | 139 ++++++++++++++++++++++---------------------- 2 files changed, 109 insertions(+), 111 deletions(-) diff --git a/core/math/vector2.c b/core/math/vector2.c index 3af7d09..d12078e 100644 --- a/core/math/vector2.c +++ b/core/math/vector2.c @@ -55,7 +55,7 @@ Vector2 vector2_normalized(Vector2 v) { bool vector2_is_normalized(const Vector2 *v) { // use length_squared() instead of length() to avoid sqrt(), makes it more stringent. - return math_is_equal_approxft(length_squared(v), 1, (real_t)UNIT_EPSILON); + return math_is_equal_approxft(vector2_length_squared(v), 1, (real_t)UNIT_EPSILON); } real_t vector2_dot(const Vector2 *self, const Vector2 *p_other) { @@ -139,84 +139,85 @@ Vector2 vector2_limit_length1(Vector2 *self) { return v; } - -/* - - -Vector2 vector2_sign() { - return Vector2(SGN(x), SGN(y)); +Vector2 vector2_sign(Vector2 *self) { + return vector2_create(SGN(self->x), SGN(self->y)); } -Vector2 vector2_floor() { - return Vector2(Math::floor(x), Math::floor(y)); +Vector2 vector2_floor(Vector2 *self) { + return vector2_create(math_floorf(self->x), math_floorf(self->y)); } -Vector2 vector2_ceil() { - return Vector2(Math::ceil(x), Math::ceil(y)); +Vector2 vector2_ceil(Vector2 *self) { + return vector2_create(math_ceilf(self->x), math_ceilf(self->y)); } -Vector2 vector2_round() { - return Vector2(Math::round(x), Math::round(y)); +Vector2 vector2_round(Vector2 *self) { + return vector2_create(math_roundf(self->x), math_roundf(self->y)); } - - -Vector2 vector2_snapped(const Vector2 &p_by) { - return Vector2( - Math::stepify(x, p_by.x), - Math::stepify(y, p_by.y)); +Vector2 vector2_snapped(Vector2 *self, const Vector2 *p_by) { + return vector2_create( + math_stepifyf(self->x, p_by->x), + math_stepifyf(self->y, p_by->y)); } - -Vector2 vector2_cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const { - Vector2 p0 = p_pre_a; - Vector2 p1 = *this; - Vector2 p2 = p_b; - Vector2 p3 = p_post_b; +Vector2 vector2_cubic_interpolate(const Vector2 *self, const Vector2 *p_b, const Vector2 *p_pre_a, const Vector2 *p_post_b, real_t p_weight) { + Vector2 p0 = *p_pre_a; + Vector2 p1 = *self; + Vector2 p2 = *p_b; + Vector2 p3 = *p_post_b; + Vector2 np0 = vector2_neg(&p0); real_t t = p_weight; real_t t2 = t * t; real_t t3 = t2 * t; +/* Vector2 out; out = 0.5f * ((p1 * 2) + (-p0 + p2) * t + (2 * p0 - 5 * p1 + 4 * p2 - p3) * t2 + (-p0 + 3 * p1 - 3 * p2 + p3) * t3); - return out; +*/ + real_t x = 0.5f * ((p1.x * 2) + (np0.x + p2.x) * t + (2 * p0.x - 5 * p1.x + 4 * p2.x - p3.x) * t2 + (np0.x + 3 * p1.x - 3 * p2.x + p3.x) * t3); + real_t y = 0.5f * ((p1.y * 2) + (np0.y + p2.y) * t + (2 * p0.y - 5 * p1.y + 4 * p2.y - p3.y) * t2 + (np0.y + 3 * p1.y - 3 * p2.y + p3.y) * t3); + + return vector2_create(x, y); } -Vector2 vector2_move_toward(const Vector2 &p_to, const real_t p_delta) { - Vector2 v = *this; - Vector2 vd = p_to - v; - real_t len = vd.length(); - return len <= p_delta || len < (real_t)CMP_EPSILON ? p_to : v + vd / len * p_delta; +Vector2 vector2_move_toward(const Vector2 *self, const Vector2 *p_to, const real_t p_delta) { + Vector2 v = *self; + Vector2 vd = vector2_subv(p_to, &v); + real_t len = vector2_length(&vd); + return len <= p_delta || len < (real_t)CMP_EPSILON ? *p_to : vector2_divsc(vector2_addv(&v, &vd), len * p_delta); } // slide returns the component of the vector along the given plane, specified by its normal vector. -Vector2 vector2_slide(const Vector2 &p_normal) { +Vector2 vector2_slide(const Vector2 *self, const Vector2 *p_normal) { #ifdef MATH_CHECKS ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector2(), "The normal Vector2 must be normalized."); #endif - return *this - p_normal * this->dot(p_normal); + return vector2_mulsc(vector2_subv(self, p_normal), vector2_dot(self, p_normal)); } -Vector2 vector2_bounce(const Vector2 &p_normal) { - return -reflect(p_normal); +Vector2 vector2_bounce(const Vector2 *self, const Vector2 *p_normal) { + return vector2_negc(vector2_reflect(self, p_normal)); } -Vector2 vector2_reflect(const Vector2 &p_normal) { +Vector2 vector2_reflect(const Vector2 *self, const Vector2 *p_normal) { #ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector2(), "The normal Vector2 must be normalized."); + //ERR_FAIL_COND_V_MSG(!p_normal.is_normalized(), Vector2(), "The normal Vector2 must be normalized."); #endif - return 2 * p_normal * this->dot(p_normal) - *this; + return vector2_subvc(vector2_mulsc(vector2_muls(p_normal, vector2_dot(self, p_normal)), 2), *self); } -bool vector2_is_equal_approx(const Vector2 &p_v) { - return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y); +bool vector2_is_equal_approx(const Vector2 *self, const Vector2 *p_v) { + return math_is_equal_approxf(self->x, p_v->x) && math_is_equal_approxf(self->y, p_v->y); } +/* + // Vector2i Vector2i Vector2i::operator+(const Vector2i &p_v) { diff --git a/core/math/vector2.h b/core/math/vector2.h index 001abb8..d133f22 100644 --- a/core/math/vector2.h +++ b/core/math/vector2.h @@ -117,6 +117,9 @@ static _FORCE_INLINE_ void vector2_add_eqv(Vector2 *self, const Vector2 *p_v) { static _FORCE_INLINE_ Vector2 vector2_subv(const Vector2 *self, const Vector2 *p_v) { return vector2_create(self->x - p_v->x, self->y - p_v->y); } +static _FORCE_INLINE_ Vector2 vector2_subvc(Vector2 self, Vector2 p_v) { + return vector2_create(self.x - p_v.x, self.y - p_v.y); +} static _FORCE_INLINE_ void vector2_sub_eqv(Vector2 *self, const Vector2 *p_v) { self->x -= p_v->x; self->y -= p_v->y; @@ -124,6 +127,9 @@ static _FORCE_INLINE_ void vector2_sub_eqv(Vector2 *self, const Vector2 *p_v) { static _FORCE_INLINE_ Vector2 vector2_mulv(const Vector2 *self, const Vector2 *p_v1) { return vector2_create(self->x * p_v1->x, self->y * p_v1->y); } +static _FORCE_INLINE_ Vector2 vector2_mulvc(Vector2 self, Vector2 p_v1) { + return vector2_create(self.x * p_v1.x, self.y * p_v1.y); +} static _FORCE_INLINE_ void vector2_mul_eqv(Vector2 *self, const Vector2 *rvalue) { self->x *= rvalue->x; self->y *= rvalue->y; @@ -140,6 +146,9 @@ static _FORCE_INLINE_ void vector2_div_eqv(Vector2 *self, const Vector2 *rvalue) static _FORCE_INLINE_ Vector2 vector2_muls(const Vector2 *self, const real_t rvalue) { return vector2_create(self->x * rvalue, self->y * rvalue); }; +static _FORCE_INLINE_ Vector2 vector2_mulsc(Vector2 self, const real_t rvalue) { + return vector2_create(self.x * rvalue, self.y * rvalue); +}; static _FORCE_INLINE_ void vector2_mul_eqs(Vector2 *self, const real_t rvalue) { self->x *= rvalue; self->y *= rvalue; @@ -148,6 +157,9 @@ static _FORCE_INLINE_ void vector2_mul_eqs(Vector2 *self, const real_t rvalue) { static _FORCE_INLINE_ Vector2 vector2_divs(const Vector2 *self, const real_t rvalue) { return vector2_create(self->x / rvalue, self->y / rvalue); }; +static _FORCE_INLINE_ Vector2 vector2_divsc(Vector2 self, const real_t rvalue) { + return vector2_create(self.x / rvalue, self.y / rvalue); +}; static _FORCE_INLINE_ void vector2_div_eqs(Vector2 *self, const real_t rvalue) { self->x /= rvalue; @@ -164,7 +176,9 @@ static _FORCE_INLINE_ Vector2 operator*(real_t p_scalar, const Vector2 *p_vec) { static _FORCE_INLINE_ Vector2 vector2_neg(const Vector2 *self) { return vector2_create(-(self->x), -(self->y)); } - +static _FORCE_INLINE_ Vector2 vector2_negc(Vector2 self) { + return vector2_create(-(self.x), -(self.y)); +} real_t length(const Vector2 *v); real_t length_squared(const Vector2 *v); @@ -211,7 +225,7 @@ Vector2 vector2_posmodv(Vector2 *self, const Vector2 *p_modv); Vector2 vector2_project(Vector2 *self, const Vector2 *p_to); static _FORCE_INLINE_ Vector2 vector2_plane_project(const Vector2 *self, real_t p_d, const Vector2 *p_vec) { - Vector2 v = vector2_muls(self, vector2_dot(self, p_vec) - p_d); + Vector2 v = vector2_muls(self, vector2_dot(self, p_vec) - p_d); return vector2_subv(p_vec, &v); } @@ -219,97 +233,80 @@ Vector2 vector2_clamped(Vector2 *self, real_t p_len); Vector2 vector2_limit_length(Vector2 *self, const real_t p_len); Vector2 vector2_limit_length1(Vector2 *self); +real_t vector2_aspect(const Vector2 *self) { + return self->width / self->height; +} /* - -_FORCE_INLINE_ static Vector2 linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_weight); -_FORCE_INLINE_ Vector2 linear_interpolate(const Vector2 &p_to, real_t p_weight); -_FORCE_INLINE_ Vector2 slerp(const Vector2 &p_to, real_t p_weight); -Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight); -Vector2 move_toward(const Vector2 &p_to, const real_t p_delta); - -Vector2 slide(const Vector2 &p_normal); -Vector2 bounce(const Vector2 &p_normal); -Vector2 reflect(const Vector2 &p_normal); - -bool is_equal_approx(const Vector2 &p_v); - -bool operator==(const Vector2 &p_vec2); -bool operator!=(const Vector2 &p_vec2); - -bool operator<(const Vector2 &p_vec2) const { - return x == p_vec2.x ? (y < p_vec2.y) : (x < p_vec2.x); -} -bool operator>(const Vector2 &p_vec2) const { - return x == p_vec2.x ? (y > p_vec2.y) : (x > p_vec2.x); -} -bool operator<=(const Vector2 &p_vec2) const { - return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); -} -bool operator>=(const Vector2 &p_vec2) const { - return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); -} - -Vector2 sign(); -Vector2 floor(); -Vector2 ceil(); -Vector2 round(); -Vector2 snapped(const Vector2 &p_by); -real_t aspect() const { - return width / height; -} - -operator String() const { +String vector2_to_string(const Vector2 *self) const { return String::num(x) + ", " + String::num(y); } +*/ -_FORCE_INLINE_ Vector2(real_t p_x, real_t p_y) { - x = p_x; - y = p_y; +//operator== +static _FORCE_INLINE_ bool vector2_eq(const Vector2 *self, const Vector2 *p_vec2) { + return self->x == p_vec2->x && self->y == p_vec2->y; } -_FORCE_INLINE_ Vector2() { - x = y = 0; +//operator!= +static _FORCE_INLINE_ bool vector2_neq(const Vector2 *self, const Vector2 *p_vec2) { + return self->x != p_vec2->x || self->y != p_vec2->y; } +static _FORCE_INLINE_ Vector2 vector2_linear_interpolate(const Vector2 *p_a, const Vector2 *p_b, real_t p_weight) { + Vector2 res = vector2_createv(p_a); -_FORCE_INLINE_ bool Vector2::operator==(const Vector2 &p_vec2) const { - return x == p_vec2.x && y == p_vec2.y; -} -_FORCE_INLINE_ bool Vector2::operator!=(const Vector2 &p_vec2) const { - return x != p_vec2.x || y != p_vec2.y; -} - -Vector2 Vector2::linear_interpolate(const Vector2 &p_to, real_t p_weight) const { - Vector2 res = *this; - - res.x += (p_weight * (p_to.x - x)); - res.y += (p_weight * (p_to.y - y)); + res.x += (p_weight * (p_b->x - p_a->x)); + res.y += (p_weight * (p_b->y - p_a->y)); return res; } -Vector2 Vector2::slerp(const Vector2 &p_to, real_t p_weight) const { +static _FORCE_INLINE_ Vector2 vector2_vector2_slerp(const Vector2 *self, const Vector2 *p_to, real_t p_weight) { #ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(!is_normalized(), Vector2(), "The start Vector2 must be normalized."); + //ERR_FAIL_COND_V_MSG(!is_normalized(), Vector2(), "The start Vector2 must be normalized."); #endif - real_t theta = angle_to(p_to); - return rotated(theta * p_weight); + real_t theta = vector2_angle_to(self, p_to); + return vector2_rotated(self, theta * p_weight); } - - -Vector2 Vector2::linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_weight) { - Vector2 res = p_a; - - res.x += (p_weight * (p_b.x - p_a.x)); - res.y += (p_weight * (p_b.y - p_a.y)); - - return res; +//operator< +static _FORCE_INLINE_ bool vector2_lt(const Vector2 *self, const Vector2 *p_vec2) { + return self->x == p_vec2->x ? (self->y < p_vec2->y) : (self->x < p_vec2->x); } +//operator> +static _FORCE_INLINE_ bool vector2_gt(const Vector2 *self, const Vector2 *p_vec2) { + return self->x == p_vec2->x ? (self->y > p_vec2->y) : (self->x > p_vec2->x); +} +//operator<= +static _FORCE_INLINE_ bool vector2_lte(const Vector2 *self, const Vector2 *p_vec2) { + return self->x == p_vec2->x ? (self->y <= p_vec2->y) : (self->x < p_vec2->x); +} +//operator>= +static _FORCE_INLINE_ bool vector2_gte(const Vector2 *self, const Vector2 *p_vec2) { + return self->x == p_vec2->x ? (self->y >= p_vec2->y) : (self->x > p_vec2->x); +} + +Vector2 vector2_sign(Vector2 *self); +Vector2 vector2_floor(Vector2 *self); +Vector2 vector2_ceil(Vector2 *self); +Vector2 vector2_round(Vector2 *self); +Vector2 vector2_snapped(Vector2 *self, const Vector2 *p_by); + +Vector2 vector2_cubic_interpolate(const Vector2 *self, const Vector2 *p_b, const Vector2 *p_pre_a, const Vector2 *p_post_b, real_t p_weight); +Vector2 vector2_move_toward(const Vector2 *self, const Vector2 *p_to, const real_t p_delta); + +Vector2 vector2_slide(const Vector2 *self, const Vector2 *p_normal); +Vector2 vector2_bounce(const Vector2 *self, const Vector2 *p_normal); +Vector2 vector2_reflect(const Vector2 *self, const Vector2 *p_normal); + +bool vector2_is_equal_approx(const Vector2 *self, const Vector2 *p_v); typedef Vector2 Size2; typedef Vector2 Point2; +/* + + // INTEGER STUFF struct _NO_DISCARD_CLASS_ Vector2i {