| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/error/error_macros.h" |
| 34 |
#include "core/math/math_funcs.h" |
| 35 |
#include "core/templates/hashfuncs.h" |
| 36 |
|
| 37 |
class String; |
| 38 |
struct Vector2i; |
| 39 |
|
| 40 |
struct [[nodiscard]] Vector2 { |
| 41 |
static const Vector2 LEFT; |
| 42 |
static const Vector2 RIGHT; |
| 43 |
static const Vector2 UP; |
| 44 |
static const Vector2 DOWN; |
| 45 |
|
| 46 |
static constexpr int AXIS_COUNT = 2; |
| 47 |
|
| 48 |
enum Axis { |
| 49 |
AXIS_X, |
| 50 |
AXIS_Y, |
| 51 |
}; |
| 52 |
|
| 53 |
union { |
| 54 |
|
| 55 |
struct { |
| 56 |
real_t x; |
| 57 |
real_t y; |
| 58 |
}; |
| 59 |
|
| 60 |
struct { |
| 61 |
real_t width; |
| 62 |
real_t height; |
| 63 |
}; |
| 64 |
|
| 65 |
real_t coord[2] = { 0 }; |
| 66 |
|
| 67 |
}; |
| 68 |
|
| 69 |
_FORCE_INLINE_ real_t &operator[](int p_axis) { |
| 70 |
DEV_ASSERT((unsigned int)p_axis < 2); |
| 71 |
return coord[p_axis]; |
| 72 |
} |
| 73 |
_FORCE_INLINE_ const real_t &operator[](int p_axis) const { |
| 74 |
DEV_ASSERT((unsigned int)p_axis < 2); |
| 75 |
return coord[p_axis]; |
| 76 |
} |
| 77 |
|
| 78 |
_FORCE_INLINE_ Vector2::Axis min_axis_index() const { |
| 79 |
return x < y ? Vector2::AXIS_X : Vector2::AXIS_Y; |
| 80 |
} |
| 81 |
|
| 82 |
_FORCE_INLINE_ Vector2::Axis max_axis_index() const { |
| 83 |
return x < y ? Vector2::AXIS_Y : Vector2::AXIS_X; |
| 84 |
} |
| 85 |
|
| 86 |
void normalize(); |
| 87 |
Vector2 normalized() const; |
| 88 |
bool is_normalized() const; |
| 89 |
|
| 90 |
real_t length() const; |
| 91 |
real_t length_squared() const; |
| 92 |
Vector2 limit_length(real_t p_len = 1.0) const; |
| 93 |
|
| 94 |
Vector2 min(const Vector2 &p_vector2) const { |
| 95 |
return Vector2(MIN(x, p_vector2.x), MIN(y, p_vector2.y)); |
| 96 |
} |
| 97 |
|
| 98 |
Vector2 minf(real_t p_scalar) const { |
| 99 |
return Vector2(MIN(x, p_scalar), MIN(y, p_scalar)); |
| 100 |
} |
| 101 |
|
| 102 |
Vector2 max(const Vector2 &p_vector2) const { |
| 103 |
return Vector2(MAX(x, p_vector2.x), MAX(y, p_vector2.y)); |
| 104 |
} |
| 105 |
|
| 106 |
Vector2 maxf(real_t p_scalar) const { |
| 107 |
return Vector2(MAX(x, p_scalar), MAX(y, p_scalar)); |
| 108 |
} |
| 109 |
|
| 110 |
real_t distance_to(const Vector2 &p_vector2) const; |
| 111 |
real_t distance_squared_to(const Vector2 &p_vector2) const; |
| 112 |
real_t angle_to(const Vector2 &p_vector2) const; |
| 113 |
real_t angle_to_point(const Vector2 &p_vector2) const; |
| 114 |
_FORCE_INLINE_ Vector2 direction_to(const Vector2 &p_to) const; |
| 115 |
|
| 116 |
real_t dot(const Vector2 &p_other) const; |
| 117 |
real_t cross(const Vector2 &p_other) const; |
| 118 |
Vector2 posmod(real_t p_mod) const; |
| 119 |
Vector2 posmodv(const Vector2 &p_modv) const; |
| 120 |
Vector2 project(const Vector2 &p_to) const; |
| 121 |
|
| 122 |
Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const; |
| 123 |
|
| 124 |
_FORCE_INLINE_ Vector2 lerp(const Vector2 &p_to, real_t p_weight) const; |
| 125 |
_FORCE_INLINE_ Vector2 slerp(const Vector2 &p_to, real_t p_weight) const; |
| 126 |
_FORCE_INLINE_ Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const; |
| 127 |
_FORCE_INLINE_ Vector2 cubic_interpolate_in_time(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight, real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const; |
| 128 |
_FORCE_INLINE_ Vector2 bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, real_t p_t) const; |
| 129 |
_FORCE_INLINE_ Vector2 bezier_derivative(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, real_t p_t) const; |
| 130 |
|
| 131 |
Vector2 move_toward(const Vector2 &p_to, real_t p_delta) const; |
| 132 |
|
| 133 |
Vector2 slide(const Vector2 &p_normal) const; |
| 134 |
Vector2 bounce(const Vector2 &p_normal) const; |
| 135 |
Vector2 reflect(const Vector2 &p_normal) const; |
| 136 |
|
| 137 |
bool is_equal_approx(const Vector2 &p_v) const; |
| 138 |
bool is_same(const Vector2 &p_v) const; |
| 139 |
bool is_zero_approx() const; |
| 140 |
bool is_finite() const; |
| 141 |
|
| 142 |
constexpr Vector2 operator+(const Vector2 &p_v) const; |
| 143 |
constexpr void operator+=(const Vector2 &p_v); |
| 144 |
constexpr Vector2 operator-(const Vector2 &p_v) const; |
| 145 |
constexpr void operator-=(const Vector2 &p_v); |
| 146 |
constexpr Vector2 operator*(const Vector2 &p_v1) const; |
| 147 |
|
| 148 |
constexpr Vector2 operator*(real_t p_rvalue) const; |
| 149 |
constexpr void operator*=(real_t p_rvalue); |
| 150 |
constexpr void operator*=(const Vector2 &p_rvalue) { *this = *this * p_rvalue; } |
| 151 |
|
| 152 |
constexpr Vector2 operator/(const Vector2 &p_v1) const; |
| 153 |
|
| 154 |
constexpr Vector2 operator/(real_t p_rvalue) const; |
| 155 |
|
| 156 |
constexpr void operator/=(real_t p_rvalue); |
| 157 |
constexpr void operator/=(const Vector2 &p_rvalue) { *this = *this / p_rvalue; } |
| 158 |
|
| 159 |
constexpr Vector2 operator-() const; |
| 160 |
|
| 161 |
constexpr bool operator==(const Vector2 &p_vec2) const; |
| 162 |
constexpr bool operator!=(const Vector2 &p_vec2) const; |
| 163 |
|
| 164 |
constexpr bool operator<(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y < p_vec2.y) : (x < p_vec2.x); } |
| 165 |
constexpr bool operator>(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y > p_vec2.y) : (x > p_vec2.x); } |
| 166 |
constexpr bool operator<=(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); } |
| 167 |
constexpr bool operator>=(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); } |
| 168 |
|
| 169 |
real_t angle() const; |
| 170 |
static Vector2 from_angle(real_t p_angle); |
| 171 |
|
| 172 |
_FORCE_INLINE_ Vector2 abs() const { |
| 173 |
return Vector2(Math::abs(x), Math::abs(y)); |
| 174 |
} |
| 175 |
|
| 176 |
Vector2 rotated(real_t p_by) const; |
| 177 |
Vector2 orthogonal() const { |
| 178 |
return Vector2(y, -x); |
| 179 |
} |
| 180 |
|
| 181 |
Vector2 sign() const; |
| 182 |
Vector2 floor() const; |
| 183 |
Vector2 ceil() const; |
| 184 |
Vector2 round() const; |
| 185 |
Vector2 snapped(const Vector2 &p_by) const; |
| 186 |
Vector2 snappedf(real_t p_by) const; |
| 187 |
Vector2 clamp(const Vector2 &p_min, const Vector2 &p_max) const; |
| 188 |
Vector2 clampf(real_t p_min, real_t p_max) const; |
| 189 |
real_t aspect() const { return width / height; } |
| 190 |
|
| 191 |
explicit operator String() const; |
| 192 |
operator Vector2i() const; |
| 193 |
|
| 194 |
uint32_t hash() const { |
| 195 |
uint32_t h = hash_murmur3_one_real(x); |
| 196 |
h = hash_murmur3_one_real(y, h); |
| 197 |
return hash_fmix32(h); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
constexpr Vector2() : |
| 202 |
x(0), y(0) {} |
| 203 |
constexpr Vector2(real_t p_x, real_t p_y) : |
| 204 |
x(p_x), y(p_y) {} |
| 205 |
|
| 206 |
}; |
| 207 |
|
| 208 |
inline constexpr Vector2 Vector2::LEFT = { -1, 0 }; |
| 209 |
inline constexpr Vector2 Vector2::RIGHT = { 1, 0 }; |
| 210 |
inline constexpr Vector2 Vector2::UP = { 0, -1 }; |
| 211 |
inline constexpr Vector2 Vector2::DOWN = { 0, 1 }; |
| 212 |
|
| 213 |
_FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const { |
| 214 |
return p_vec - *this * (dot(p_vec) - p_d); |
| 215 |
} |
| 216 |
|
| 217 |
constexpr Vector2 Vector2::operator+(const Vector2 &p_v) const { |
| 218 |
return Vector2(x + p_v.x, y + p_v.y); |
| 219 |
} |
| 220 |
|
| 221 |
constexpr void Vector2::operator+=(const Vector2 &p_v) { |
| 222 |
x += p_v.x; |
| 223 |
y += p_v.y; |
| 224 |
} |
| 225 |
|
| 226 |
constexpr Vector2 Vector2::operator-(const Vector2 &p_v) const { |
| 227 |
return Vector2(x - p_v.x, y - p_v.y); |
| 228 |
} |
| 229 |
|
| 230 |
constexpr void Vector2::operator-=(const Vector2 &p_v) { |
| 231 |
x -= p_v.x; |
| 232 |
y -= p_v.y; |
| 233 |
} |
| 234 |
|
| 235 |
constexpr Vector2 Vector2::operator*(const Vector2 &p_v1) const { |
| 236 |
return Vector2(x * p_v1.x, y * p_v1.y); |
| 237 |
} |
| 238 |
|
| 239 |
constexpr Vector2 Vector2::operator*(real_t p_rvalue) const { |
| 240 |
return Vector2(x * p_rvalue, y * p_rvalue); |
| 241 |
} |
| 242 |
|
| 243 |
constexpr void Vector2::operator*=(real_t p_rvalue) { |
| 244 |
x *= p_rvalue; |
| 245 |
y *= p_rvalue; |
| 246 |
} |
| 247 |
|
| 248 |
constexpr Vector2 Vector2::operator/(const Vector2 &p_v1) const { |
| 249 |
return Vector2(x / p_v1.x, y / p_v1.y); |
| 250 |
} |
| 251 |
|
| 252 |
constexpr Vector2 Vector2::operator/(real_t p_rvalue) const { |
| 253 |
return Vector2(x / p_rvalue, y / p_rvalue); |
| 254 |
} |
| 255 |
|
| 256 |
constexpr void Vector2::operator/=(real_t p_rvalue) { |
| 257 |
x /= p_rvalue; |
| 258 |
y /= p_rvalue; |
| 259 |
} |
| 260 |
|
| 261 |
constexpr Vector2 Vector2::operator-() const { |
| 262 |
return Vector2(-x, -y); |
| 263 |
} |
| 264 |
|
| 265 |
constexpr bool Vector2::operator==(const Vector2 &p_vec2) const { |
| 266 |
return x == p_vec2.x && y == p_vec2.y; |
| 267 |
} |
| 268 |
|
| 269 |
constexpr bool Vector2::operator!=(const Vector2 &p_vec2) const { |
| 270 |
return x != p_vec2.x || y != p_vec2.y; |
| 271 |
} |
| 272 |
|
| 273 |
Vector2 Vector2::lerp(const Vector2 &p_to, real_t p_weight) const { |
| 274 |
Vector2 res = *this; |
| 275 |
res.x = Math::lerp(res.x, p_to.x, p_weight); |
| 276 |
res.y = Math::lerp(res.y, p_to.y, p_weight); |
| 277 |
return res; |
| 278 |
} |
| 279 |
|
| 280 |
Vector2 Vector2::slerp(const Vector2 &p_to, real_t p_weight) const { |
| 281 |
real_t start_length_sq = length_squared(); |
| 282 |
real_t end_length_sq = p_to.length_squared(); |
| 283 |
if (unlikely(start_length_sq == 0.0f || end_length_sq == 0.0f)) { |
| 284 |
|
| 285 |
return lerp(p_to, p_weight); |
| 286 |
} |
| 287 |
real_t start_length = Math::sqrt(start_length_sq); |
| 288 |
real_t result_length = Math::lerp(start_length, Math::sqrt(end_length_sq), p_weight); |
| 289 |
real_t angle = angle_to(p_to); |
| 290 |
return rotated(angle * p_weight) * (result_length / start_length); |
| 291 |
} |
| 292 |
|
| 293 |
Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const { |
| 294 |
Vector2 res = *this; |
| 295 |
res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight); |
| 296 |
res.y = Math::cubic_interpolate(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight); |
| 297 |
return res; |
| 298 |
} |
| 299 |
|
| 300 |
Vector2 Vector2::cubic_interpolate_in_time(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight, real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const { |
| 301 |
Vector2 res = *this; |
| 302 |
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); |
| 303 |
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); |
| 304 |
return res; |
| 305 |
} |
| 306 |
|
| 307 |
Vector2 Vector2::bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, real_t p_t) const { |
| 308 |
Vector2 res = *this; |
| 309 |
res.x = Math::bezier_interpolate(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t); |
| 310 |
res.y = Math::bezier_interpolate(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t); |
| 311 |
return res; |
| 312 |
} |
| 313 |
|
| 314 |
Vector2 Vector2::bezier_derivative(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, real_t p_t) const { |
| 315 |
Vector2 res = *this; |
| 316 |
res.x = Math::bezier_derivative(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t); |
| 317 |
res.y = Math::bezier_derivative(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t); |
| 318 |
return res; |
| 319 |
} |
| 320 |
|
| 321 |
Vector2 Vector2::direction_to(const Vector2 &p_to) const { |
| 322 |
Vector2 ret(p_to.x - x, p_to.y - y); |
| 323 |
ret.normalize(); |
| 324 |
return ret; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
constexpr Vector2 operator*(float p_scalar, const Vector2 &p_vec) { |
| 331 |
return p_vec * p_scalar; |
| 332 |
} |
| 333 |
|
| 334 |
constexpr Vector2 operator*(double p_scalar, const Vector2 &p_vec) { |
| 335 |
return p_vec * p_scalar; |
| 336 |
} |
| 337 |
|
| 338 |
constexpr Vector2 operator*(int32_t p_scalar, const Vector2 &p_vec) { |
| 339 |
return p_vec * p_scalar; |
| 340 |
} |
| 341 |
|
| 342 |
constexpr Vector2 operator*(int64_t p_scalar, const Vector2 &p_vec) { |
| 343 |
return p_vec * p_scalar; |
| 344 |
} |
| 345 |
|
| 346 |
typedef Vector2 Size2; |
| 347 |
typedef Vector2 Point2; |
| 348 |
|
| 349 |
template <> |
| 350 |
struct is_zero_constructible<Vector2> : std::true_type {}; |