mirror of
https://github.com/Relintai/pandemonium_engine.git
synced 2024-12-23 04:16:50 +01:00
309 lines
9.9 KiB
C++
309 lines
9.9 KiB
C++
#ifndef VECTOR2_H
|
|
#define VECTOR2_H
|
|
|
|
/*************************************************************************/
|
|
/* vector2.h */
|
|
/*************************************************************************/
|
|
/* This file is part of: */
|
|
/* PANDEMONIUM ENGINE */
|
|
/* https://github.com/Relintai/pandemonium_engine */
|
|
/*************************************************************************/
|
|
/* Copyright (c) 2022-present Péter Magyar. */
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
/* */
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
/* a copy of this software and associated documentation files (the */
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
/* the following conditions: */
|
|
/* */
|
|
/* The above copyright notice and this permission notice shall be */
|
|
/* included in all copies or substantial portions of the Software. */
|
|
/* */
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
/*************************************************************************/
|
|
|
|
#include "core/math/math_funcs.h"
|
|
|
|
#include "core/error/error_macros.h"
|
|
|
|
class String;
|
|
struct Vector2i;
|
|
|
|
struct _NO_DISCARD_CLASS_ Vector2 {
|
|
static const int AXIS_COUNT = 2;
|
|
|
|
enum Axis {
|
|
AXIS_X,
|
|
AXIS_Y,
|
|
};
|
|
|
|
union {
|
|
struct {
|
|
union {
|
|
real_t x;
|
|
real_t width;
|
|
};
|
|
union {
|
|
real_t y;
|
|
real_t height;
|
|
};
|
|
};
|
|
|
|
real_t coord[2];
|
|
};
|
|
|
|
_FORCE_INLINE_ real_t &operator[](int p_idx) {
|
|
DEV_ASSERT((unsigned int)p_idx < 2);
|
|
return coord[p_idx];
|
|
}
|
|
_FORCE_INLINE_ const real_t &operator[](int p_idx) const {
|
|
DEV_ASSERT((unsigned int)p_idx < 2);
|
|
return coord[p_idx];
|
|
}
|
|
|
|
_FORCE_INLINE_ void set_all(real_t p_value) {
|
|
x = y = p_value;
|
|
}
|
|
|
|
_FORCE_INLINE_ int min_axis() const {
|
|
return x < y ? 0 : 1;
|
|
}
|
|
|
|
_FORCE_INLINE_ int max_axis() const {
|
|
return x < y ? 1 : 0;
|
|
}
|
|
|
|
void normalize();
|
|
Vector2 normalized() const;
|
|
bool is_normalized() const;
|
|
|
|
real_t length() const;
|
|
real_t length_squared() const;
|
|
Vector2 limit_length(const real_t p_len = 1.0) const;
|
|
|
|
Vector2 min(const Vector2 &p_vector2) const {
|
|
return Vector2(MIN(x, p_vector2.x), MIN(y, p_vector2.y));
|
|
}
|
|
|
|
Vector2 max(const Vector2 &p_vector2) const {
|
|
return Vector2(MAX(x, p_vector2.x), MAX(y, p_vector2.y));
|
|
}
|
|
|
|
real_t distance_to(const Vector2 &p_vector2) const;
|
|
real_t distance_squared_to(const Vector2 &p_vector2) const;
|
|
real_t angle_to(const Vector2 &p_vector2) const;
|
|
real_t angle_to_point(const Vector2 &p_vector2) const;
|
|
_FORCE_INLINE_ Vector2 direction_to(const Vector2 &p_to) const;
|
|
|
|
real_t dot(const Vector2 &p_other) const;
|
|
real_t cross(const Vector2 &p_other) const;
|
|
Vector2 posmod(const real_t p_mod) const;
|
|
Vector2 posmodv(const Vector2 &p_modv) const;
|
|
Vector2 project(const Vector2 &p_to) const;
|
|
|
|
Vector2 plane_project(real_t p_d, const Vector2 &p_vec) 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) const;
|
|
_FORCE_INLINE_ Vector2 slerp(const Vector2 &p_to, real_t p_weight) const;
|
|
_FORCE_INLINE_ Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) 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;
|
|
|
|
Vector2 slide(const Vector2 &p_normal) const;
|
|
Vector2 bounce(const Vector2 &p_normal) const;
|
|
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);
|
|
Vector2 operator-(const Vector2 &p_v) const;
|
|
void operator-=(const Vector2 &p_v);
|
|
Vector2 operator*(const Vector2 &p_v1) const;
|
|
|
|
Vector2 operator*(const real_t &rvalue) const;
|
|
void operator*=(const real_t &rvalue);
|
|
void operator*=(const Vector2 &rvalue) { *this = *this * rvalue; }
|
|
|
|
Vector2 operator/(const Vector2 &p_v1) const;
|
|
|
|
Vector2 operator/(const real_t &rvalue) const;
|
|
|
|
void operator/=(const real_t &rvalue);
|
|
void operator/=(const Vector2 &rvalue) { *this = *this / rvalue; }
|
|
|
|
Vector2 operator-() const;
|
|
|
|
bool operator==(const Vector2 &p_vec2) const;
|
|
bool operator!=(const Vector2 &p_vec2) const;
|
|
|
|
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); }
|
|
|
|
real_t angle() const;
|
|
|
|
void set_rotation(real_t p_radians) {
|
|
x = Math::cos(p_radians);
|
|
y = Math::sin(p_radians);
|
|
}
|
|
|
|
_FORCE_INLINE_ Vector2 abs() const {
|
|
return Vector2(Math::abs(x), Math::abs(y));
|
|
}
|
|
|
|
Vector2 rotated(real_t p_by) const;
|
|
_FORCE_INLINE_ Vector2 tangent() const {
|
|
return Vector2(y, -x);
|
|
}
|
|
_FORCE_INLINE_ Vector2 orthogonal() const {
|
|
return Vector2(y, -x);
|
|
}
|
|
|
|
Vector2 sign() const;
|
|
Vector2 floor() const;
|
|
Vector2 ceil() const;
|
|
Vector2 round() const;
|
|
Vector2 snapped(const Vector2 &p_by) const;
|
|
real_t aspect() const { return width / height; }
|
|
|
|
operator String() const;
|
|
operator Vector2i() const;
|
|
|
|
_FORCE_INLINE_ Vector2(real_t p_x, real_t p_y) {
|
|
x = p_x;
|
|
y = p_y;
|
|
}
|
|
_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_ Vector2 operator*(real_t p_scalar, const Vector2 &p_vec) {
|
|
return p_vec * p_scalar;
|
|
}
|
|
|
|
_FORCE_INLINE_ Vector2 Vector2::operator+(const Vector2 &p_v) const {
|
|
return Vector2(x + p_v.x, y + p_v.y);
|
|
}
|
|
_FORCE_INLINE_ void Vector2::operator+=(const Vector2 &p_v) {
|
|
x += p_v.x;
|
|
y += p_v.y;
|
|
}
|
|
_FORCE_INLINE_ Vector2 Vector2::operator-(const Vector2 &p_v) const {
|
|
return Vector2(x - p_v.x, y - p_v.y);
|
|
}
|
|
_FORCE_INLINE_ void Vector2::operator-=(const Vector2 &p_v) {
|
|
x -= p_v.x;
|
|
y -= p_v.y;
|
|
}
|
|
|
|
_FORCE_INLINE_ Vector2 Vector2::operator*(const Vector2 &p_v1) const {
|
|
return Vector2(x * p_v1.x, y * p_v1.y);
|
|
};
|
|
|
|
_FORCE_INLINE_ Vector2 Vector2::operator*(const real_t &rvalue) const {
|
|
return Vector2(x * rvalue, y * rvalue);
|
|
};
|
|
_FORCE_INLINE_ void Vector2::operator*=(const real_t &rvalue) {
|
|
x *= rvalue;
|
|
y *= rvalue;
|
|
};
|
|
|
|
_FORCE_INLINE_ Vector2 Vector2::operator/(const Vector2 &p_v1) const {
|
|
return Vector2(x / p_v1.x, y / p_v1.y);
|
|
};
|
|
|
|
_FORCE_INLINE_ Vector2 Vector2::operator/(const real_t &rvalue) const {
|
|
return Vector2(x / rvalue, y / rvalue);
|
|
};
|
|
|
|
_FORCE_INLINE_ void Vector2::operator/=(const real_t &rvalue) {
|
|
x /= rvalue;
|
|
y /= rvalue;
|
|
};
|
|
|
|
_FORCE_INLINE_ Vector2 Vector2::operator-() const {
|
|
return Vector2(-x, -y);
|
|
}
|
|
|
|
_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));
|
|
|
|
return res;
|
|
}
|
|
|
|
Vector2 Vector2::slerp(const Vector2 &p_to, real_t p_weight) const {
|
|
#ifdef MATH_CHECKS
|
|
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);
|
|
}
|
|
|
|
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;
|
|
|
|
/* 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;
|
|
}
|
|
|
|
Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight) const {
|
|
Vector2 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);
|
|
return res;
|
|
}
|
|
|
|
Vector2 Vector2::direction_to(const Vector2 &p_to) const {
|
|
Vector2 ret(p_to.x - x, p_to.y - y);
|
|
ret.normalize();
|
|
return ret;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
typedef Vector2 Size2;
|
|
typedef Vector2 Point2;
|
|
|
|
#endif // VECTOR2_H
|