Finished the rest of vector2.

This commit is contained in:
Relintai 2022-03-14 09:12:57 +01:00
parent 7f7591b6a2
commit 5b64481c87
2 changed files with 109 additions and 111 deletions

View File

@ -55,7 +55,7 @@ Vector2 vector2_normalized(Vector2 v) {
bool vector2_is_normalized(const Vector2 *v) { bool vector2_is_normalized(const Vector2 *v) {
// use length_squared() instead of length() to avoid sqrt(), makes it more stringent. // 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) { real_t vector2_dot(const Vector2 *self, const Vector2 *p_other) {
@ -139,84 +139,85 @@ Vector2 vector2_limit_length1(Vector2 *self) {
return v; return v;
} }
Vector2 vector2_sign(Vector2 *self) {
/* return vector2_create(SGN(self->x), SGN(self->y));
Vector2 vector2_sign() {
return Vector2(SGN(x), SGN(y));
} }
Vector2 vector2_floor() { Vector2 vector2_floor(Vector2 *self) {
return Vector2(Math::floor(x), Math::floor(y)); return vector2_create(math_floorf(self->x), math_floorf(self->y));
} }
Vector2 vector2_ceil() { Vector2 vector2_ceil(Vector2 *self) {
return Vector2(Math::ceil(x), Math::ceil(y)); return vector2_create(math_ceilf(self->x), math_ceilf(self->y));
} }
Vector2 vector2_round() { Vector2 vector2_round(Vector2 *self) {
return Vector2(Math::round(x), Math::round(y)); return vector2_create(math_roundf(self->x), math_roundf(self->y));
} }
Vector2 vector2_snapped(Vector2 *self, const Vector2 *p_by) {
return vector2_create(
Vector2 vector2_snapped(const Vector2 &p_by) { math_stepifyf(self->x, p_by->x),
return Vector2( math_stepifyf(self->y, p_by->y));
Math::stepify(x, p_by.x),
Math::stepify(y, p_by.y));
} }
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_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 p0 = p_pre_a; Vector2 p1 = *self;
Vector2 p1 = *this; Vector2 p2 = *p_b;
Vector2 p2 = p_b; Vector2 p3 = *p_post_b;
Vector2 p3 = p_post_b; Vector2 np0 = vector2_neg(&p0);
real_t t = p_weight; real_t t = p_weight;
real_t t2 = t * t; real_t t2 = t * t;
real_t t3 = t2 * t; real_t t3 = t2 * t;
/*
Vector2 out; Vector2 out;
out = 0.5f * out = 0.5f *
((p1 * 2) + ((p1 * 2) +
(-p0 + p2) * t + (-p0 + p2) * t +
(2 * p0 - 5 * p1 + 4 * p2 - p3) * t2 + (2 * p0 - 5 * p1 + 4 * p2 - p3) * t2 +
(-p0 + 3 * p1 - 3 * p2 + p3) * t3); (-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 vector2_move_toward(const Vector2 *self, const Vector2 *p_to, const real_t p_delta) {
Vector2 v = *this; Vector2 v = *self;
Vector2 vd = p_to - v; Vector2 vd = vector2_subv(p_to, &v);
real_t len = vd.length(); real_t len = vector2_length(&vd);
return len <= p_delta || len < (real_t)CMP_EPSILON ? p_to : v + vd / len * p_delta; 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. // 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 #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 #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) { Vector2 vector2_bounce(const Vector2 *self, const Vector2 *p_normal) {
return -reflect(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 #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 #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) { bool vector2_is_equal_approx(const Vector2 *self, const Vector2 *p_v) {
return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y); return math_is_equal_approxf(self->x, p_v->x) && math_is_equal_approxf(self->y, p_v->y);
} }
/*
// Vector2i // Vector2i
Vector2i Vector2i::operator+(const Vector2i &p_v) { Vector2i Vector2i::operator+(const Vector2i &p_v) {

View File

@ -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) { 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); 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) { static _FORCE_INLINE_ void vector2_sub_eqv(Vector2 *self, const Vector2 *p_v) {
self->x -= p_v->x; self->x -= p_v->x;
self->y -= p_v->y; 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) { 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); 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) { static _FORCE_INLINE_ void vector2_mul_eqv(Vector2 *self, const Vector2 *rvalue) {
self->x *= rvalue->x; self->x *= rvalue->x;
self->y *= rvalue->y; 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) { static _FORCE_INLINE_ Vector2 vector2_muls(const Vector2 *self, const real_t rvalue) {
return vector2_create(self->x * rvalue, self->y * 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) { static _FORCE_INLINE_ void vector2_mul_eqs(Vector2 *self, const real_t rvalue) {
self->x *= rvalue; self->x *= rvalue;
self->y *= 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) { static _FORCE_INLINE_ Vector2 vector2_divs(const Vector2 *self, const real_t rvalue) {
return vector2_create(self->x / rvalue, self->y / 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) { static _FORCE_INLINE_ void vector2_div_eqs(Vector2 *self, const real_t rvalue) {
self->x /= 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) { static _FORCE_INLINE_ Vector2 vector2_neg(const Vector2 *self) {
return vector2_create(-(self->x), -(self->y)); 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(const Vector2 *v);
real_t length_squared(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); 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) { 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); 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_length(Vector2 *self, const real_t p_len);
Vector2 vector2_limit_length1(Vector2 *self); Vector2 vector2_limit_length1(Vector2 *self);
real_t vector2_aspect(const Vector2 *self) {
return self->width / self->height;
}
/* /*
String vector2_to_string(const Vector2 *self) const {
_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 {
return String::num(x) + ", " + String::num(y); return String::num(x) + ", " + String::num(y);
} }
*/
_FORCE_INLINE_ Vector2(real_t p_x, real_t p_y) { //operator==
x = p_x; static _FORCE_INLINE_ bool vector2_eq(const Vector2 *self, const Vector2 *p_vec2) {
y = p_y; return self->x == p_vec2->x && self->y == p_vec2->y;
} }
_FORCE_INLINE_ Vector2() { //operator!=
x = y = 0; 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 { res.x += (p_weight * (p_b->x - p_a->x));
return x == p_vec2.x && y == p_vec2.y; res.y += (p_weight * (p_b->y - p_a->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));
return res; 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 #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 #endif
real_t theta = angle_to(p_to); real_t theta = vector2_angle_to(self, p_to);
return rotated(theta * p_weight); return vector2_rotated(self, theta * p_weight);
} }
//operator<
static _FORCE_INLINE_ bool vector2_lt(const Vector2 *self, const Vector2 *p_vec2) {
Vector2 Vector2::linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_weight) { return self->x == p_vec2->x ? (self->y < p_vec2->y) : (self->x < p_vec2->x);
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_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 Size2;
typedef Vector2 Point2; typedef Vector2 Point2;
/*
// INTEGER STUFF // INTEGER STUFF
struct _NO_DISCARD_CLASS_ Vector2i { struct _NO_DISCARD_CLASS_ Vector2i {