More work on vector2.

This commit is contained in:
Relintai 2022-03-13 20:56:32 +01:00
parent 2dee965124
commit d424e12f44
2 changed files with 67 additions and 47 deletions

View File

@ -96,6 +96,50 @@ Vector2 vector2_rotated(const Vector2 *self, real_t p_by) {
return v;
}
Vector2 vector2_posmod(Vector2 *self, const real_t p_mod) {
return vector2_create(math_fposmodf(self->x, p_mod), math_fposmodf(self->y, p_mod));
}
Vector2 vector2_posmodv(Vector2 *self, const Vector2 *p_modv) {
return vector2_create(math_fposmodf(self->x, p_modv->x), math_fposmodf(self->y, p_modv->y));
}
Vector2 vector2_project(Vector2 *self, const Vector2 *p_to) {
return vector2_muls(p_to, (vector2_dot(self, p_to) / vector2_length_squared(p_to)));
}
Vector2 vector2_clamped(Vector2 *self, real_t p_len) {
//WARN_DEPRECATED_MSG("'Vector2.clamped()' is deprecated because it has been renamed to 'limit_length'.");
real_t l = vector2_length(self);
Vector2 v = vector2_createv(self);
if (l > 0 && p_len < l) {
vector2_div_eqs(&v, l);
vector2_mul_eqs(&v, p_len);
}
return v;
}
Vector2 vector2_limit_length(Vector2 *self, const real_t p_len) {
const real_t l = vector2_length(self);
Vector2 v = vector2_createv(self);
if (l > 0 && p_len < l) {
vector2_div_eqs(&v, l);
vector2_mul_eqs(&v, p_len);
}
return v;
}
Vector2 vector2_limit_length1(Vector2 *self) {
const real_t l = vector2_length(self);
Vector2 v = vector2_createv(self);
vector2_div_eqs(&v, 1);
return v;
}
/*
@ -116,17 +160,6 @@ Vector2 vector2_round() {
}
Vector2 vector2_posmod(const real_t p_mod) {
return Vector2(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod));
}
Vector2 vector2_posmodv(const Vector2 &p_modv) {
return Vector2(Math::fposmod(x, p_modv.x), Math::fposmod(y, p_modv.y));
}
Vector2 vector2_project(const Vector2 &p_to) {
return p_to * (dot(p_to) / p_to.length_squared());
}
Vector2 vector2_snapped(const Vector2 &p_by) {
return Vector2(
@ -134,28 +167,6 @@ Vector2 vector2_snapped(const Vector2 &p_by) {
Math::stepify(y, p_by.y));
}
Vector2 vector2_clamped(real_t p_len) {
WARN_DEPRECATED_MSG("'Vector2.clamped()' is deprecated because it has been renamed to 'limit_length'.");
real_t l = length();
Vector2 v = *this;
if (l > 0 && p_len < l) {
v /= l;
v *= p_len;
}
return v;
}
Vector2 vector2_limit_length(const real_t p_len) {
const real_t l = length();
Vector2 v = *this;
if (l > 0 && p_len < l) {
v /= l;
v *= p_len;
}
return v;
}
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;

View File

@ -90,6 +90,15 @@ static _FORCE_INLINE_ Vector2 vector2_create(real_t p_value_x, real_t p_value_y)
return v;
}
static _FORCE_INLINE_ Vector2 vector2_createv(const Vector2 *other) {
Vector2 v;
v.x = other->x;
v.y = other->y;
return v;
}
static _FORCE_INLINE_ int vector2_min_axis(const Vector2 *v) {
return v->x < v->y ? 0 : 1;
}
@ -193,23 +202,26 @@ static _FORCE_INLINE_ Vector2 vector2_abs(const Vector2 *self) {
}
Vector2 vector2_rotated(const Vector2 *self, real_t p_by);
Vector2 vector2_tangent(const Vector2 *self) {
static _FORCE_INLINE_ Vector2 vector2_tangent(const Vector2 *self) {
return vector2_create(self->y, -(self->x));
}
Vector2 vector2_posmod(Vector2 *self, const real_t p_mod);
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);
return vector2_subv(p_vec, &v);
}
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);
/*
Vector2 posmod(const real_t p_mod);
Vector2 posmodv(const Vector2 &p_modv);
Vector2 project(const Vector2 &p_to);
Vector2 plane_project(real_t p_d, const Vector2 &p_vec);
Vector2 clamped(real_t p_len);
Vector2 limit_length(const real_t p_len = 1.0);
_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);
@ -259,9 +271,6 @@ _FORCE_INLINE_ Vector2() {
x = y = 0;
}
_FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const {
return p_vec - *this * (dot(p_vec) - p_d);
}
_FORCE_INLINE_ bool Vector2::operator==(const Vector2 &p_vec2) const {
return x == p_vec2.x && y == p_vec2.y;