2018-05-07 23:33:25 +02:00
|
|
|
/**************************************************************************/
|
|
|
|
/* vector2.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 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. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
|
|
|
#ifndef VECTOR2_H
|
|
|
|
#define VECTOR2_H
|
|
|
|
|
2022-03-07 12:15:45 +01:00
|
|
|
#include "core/error/error_macros.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/math/math_funcs.h"
|
2018-05-07 23:33:25 +02:00
|
|
|
|
2022-02-04 15:35:14 +01:00
|
|
|
class String;
|
2018-08-11 07:32:45 +02:00
|
|
|
struct Vector2i;
|
|
|
|
|
2022-01-19 20:46:17 +01:00
|
|
|
struct _NO_DISCARD_ Vector2 {
|
2021-05-10 23:43:13 +02:00
|
|
|
static const int AXIS_COUNT = 2;
|
|
|
|
|
2019-08-05 03:50:28 +02:00
|
|
|
enum Axis {
|
|
|
|
AXIS_X,
|
|
|
|
AXIS_Y,
|
|
|
|
};
|
|
|
|
|
2018-05-07 23:33:25 +02:00
|
|
|
union {
|
2021-05-10 23:43:13 +02:00
|
|
|
struct {
|
|
|
|
union {
|
|
|
|
real_t x;
|
|
|
|
real_t width;
|
|
|
|
};
|
|
|
|
union {
|
|
|
|
real_t y;
|
|
|
|
real_t height;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
real_t coord[2] = { 0 };
|
2018-05-07 23:33:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
_FORCE_INLINE_ real_t &operator[](int p_idx) {
|
2022-03-07 12:15:45 +01:00
|
|
|
DEV_ASSERT((unsigned int)p_idx < 2);
|
2022-02-09 09:17:17 +01:00
|
|
|
return coord[p_idx];
|
2018-05-07 23:33:25 +02:00
|
|
|
}
|
|
|
|
_FORCE_INLINE_ const real_t &operator[](int p_idx) const {
|
2022-03-07 12:15:45 +01:00
|
|
|
DEV_ASSERT((unsigned int)p_idx < 2);
|
2022-02-09 09:17:17 +01:00
|
|
|
return coord[p_idx];
|
2018-05-07 23:33:25 +02:00
|
|
|
}
|
|
|
|
|
2019-11-28 09:48:51 +01:00
|
|
|
_FORCE_INLINE_ Vector2::Axis min_axis_index() const {
|
|
|
|
return x < y ? Vector2::AXIS_X : Vector2::AXIS_Y;
|
2021-05-10 23:43:13 +02:00
|
|
|
}
|
|
|
|
|
2019-11-28 09:48:51 +01:00
|
|
|
_FORCE_INLINE_ Vector2::Axis max_axis_index() const {
|
|
|
|
return x < y ? Vector2::AXIS_Y : Vector2::AXIS_X;
|
2021-05-10 23:43:13 +02:00
|
|
|
}
|
|
|
|
|
2018-05-07 23:33:25 +02:00
|
|
|
void normalize();
|
|
|
|
Vector2 normalized() const;
|
|
|
|
bool is_normalized() const;
|
|
|
|
|
|
|
|
real_t length() const;
|
|
|
|
real_t length_squared() const;
|
2021-02-01 06:42:00 +01:00
|
|
|
Vector2 limit_length(const real_t p_len = 1.0) const;
|
2018-05-07 23:33:25 +02:00
|
|
|
|
2020-10-08 02:29:49 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2018-05-07 23:33:25 +02:00
|
|
|
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;
|
2020-12-07 09:16:31 +01:00
|
|
|
_FORCE_INLINE_ Vector2 direction_to(const Vector2 &p_to) const;
|
2018-05-07 23:33:25 +02:00
|
|
|
|
|
|
|
real_t dot(const Vector2 &p_other) const;
|
|
|
|
real_t cross(const Vector2 &p_other) const;
|
2019-08-05 03:50:28 +02:00
|
|
|
Vector2 posmod(const real_t p_mod) const;
|
|
|
|
Vector2 posmodv(const Vector2 &p_modv) const;
|
2020-12-07 09:16:31 +01:00
|
|
|
Vector2 project(const Vector2 &p_to) const;
|
2018-05-07 23:33:25 +02:00
|
|
|
|
2021-08-15 16:45:37 +02:00
|
|
|
Vector2 plane_project(const real_t p_d, const Vector2 &p_vec) const;
|
2018-05-07 23:33:25 +02:00
|
|
|
|
2021-08-15 16:45:37 +02:00
|
|
|
_FORCE_INLINE_ Vector2 lerp(const Vector2 &p_to, const real_t p_weight) const;
|
|
|
|
_FORCE_INLINE_ Vector2 slerp(const Vector2 &p_to, const real_t p_weight) const;
|
2022-06-27 19:41:32 +02:00
|
|
|
_FORCE_INLINE_ Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight) const;
|
2022-07-28 21:55:10 +02:00
|
|
|
_FORCE_INLINE_ Vector2 cubic_interpolate_in_time(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const;
|
2022-06-27 19:41:32 +02:00
|
|
|
_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;
|
2022-11-23 04:11:58 +01:00
|
|
|
_FORCE_INLINE_ Vector2 bezier_derivative(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t) const;
|
2022-06-27 19:41:32 +02:00
|
|
|
|
2019-04-07 23:40:56 +02:00
|
|
|
Vector2 move_toward(const Vector2 &p_to, const real_t p_delta) const;
|
2018-05-07 23:33:25 +02:00
|
|
|
|
|
|
|
Vector2 slide(const Vector2 &p_normal) const;
|
|
|
|
Vector2 bounce(const Vector2 &p_normal) const;
|
|
|
|
Vector2 reflect(const Vector2 &p_normal) const;
|
|
|
|
|
2019-10-14 22:33:45 +02:00
|
|
|
bool is_equal_approx(const Vector2 &p_v) const;
|
2022-09-02 02:32:33 +02:00
|
|
|
bool is_zero_approx() const;
|
2022-08-11 10:12:27 +02:00
|
|
|
bool is_finite() const;
|
2019-10-14 22:33:45 +02:00
|
|
|
|
2018-05-07 23:33:25 +02:00
|
|
|
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);
|
2019-04-19 20:54:33 +02:00
|
|
|
void operator/=(const Vector2 &rvalue) { *this = *this / rvalue; }
|
2018-05-07 23:33:25 +02:00
|
|
|
|
|
|
|
Vector2 operator-() const;
|
|
|
|
|
|
|
|
bool operator==(const Vector2 &p_vec2) const;
|
|
|
|
bool operator!=(const Vector2 &p_vec2) const;
|
|
|
|
|
2020-09-09 00:10:58 +02:00
|
|
|
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); }
|
2018-05-07 23:33:25 +02:00
|
|
|
|
|
|
|
real_t angle() const;
|
2021-04-27 15:53:04 +02:00
|
|
|
static Vector2 from_angle(const real_t p_angle);
|
2018-05-07 23:33:25 +02:00
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2 abs() const {
|
|
|
|
return Vector2(Math::abs(x), Math::abs(y));
|
|
|
|
}
|
|
|
|
|
2021-08-15 16:45:37 +02:00
|
|
|
Vector2 rotated(const real_t p_by) const;
|
2020-12-06 19:16:06 +01:00
|
|
|
Vector2 orthogonal() const {
|
2018-05-07 23:33:25 +02:00
|
|
|
return Vector2(y, -x);
|
|
|
|
}
|
|
|
|
|
2019-08-05 03:50:28 +02:00
|
|
|
Vector2 sign() const;
|
2018-05-07 23:33:25 +02:00
|
|
|
Vector2 floor() const;
|
|
|
|
Vector2 ceil() const;
|
|
|
|
Vector2 round() const;
|
|
|
|
Vector2 snapped(const Vector2 &p_by) const;
|
2021-02-01 06:10:52 +01:00
|
|
|
Vector2 clamp(const Vector2 &p_min, const Vector2 &p_max) const;
|
2018-05-07 23:33:25 +02:00
|
|
|
real_t aspect() const { return width / height; }
|
|
|
|
|
2021-02-25 15:54:50 +01:00
|
|
|
operator String() const;
|
2022-02-04 15:35:14 +01:00
|
|
|
operator Vector2i() const;
|
2018-05-07 23:33:25 +02:00
|
|
|
|
2020-05-12 17:01:17 +02:00
|
|
|
_FORCE_INLINE_ Vector2() {}
|
2021-08-15 16:45:37 +02:00
|
|
|
_FORCE_INLINE_ Vector2(const real_t p_x, const real_t p_y) {
|
2018-05-07 23:33:25 +02:00
|
|
|
x = p_x;
|
|
|
|
y = p_y;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-15 16:45:37 +02:00
|
|
|
_FORCE_INLINE_ Vector2 Vector2::plane_project(const real_t p_d, const Vector2 &p_vec) const {
|
2018-05-07 23:33:25 +02:00
|
|
|
return p_vec - *this * (dot(p_vec) - p_d);
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2 Vector2::operator+(const Vector2 &p_v) const {
|
|
|
|
return Vector2(x + p_v.x, y + p_v.y);
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2018-05-07 23:33:25 +02:00
|
|
|
_FORCE_INLINE_ void Vector2::operator+=(const Vector2 &p_v) {
|
|
|
|
x += p_v.x;
|
|
|
|
y += p_v.y;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2018-05-07 23:33:25 +02:00
|
|
|
_FORCE_INLINE_ Vector2 Vector2::operator-(const Vector2 &p_v) const {
|
|
|
|
return Vector2(x - p_v.x, y - p_v.y);
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2018-05-07 23:33:25 +02:00
|
|
|
_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);
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2018-05-07 23:33:25 +02:00
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2 Vector2::operator*(const real_t &rvalue) const {
|
|
|
|
return Vector2(x * rvalue, y * rvalue);
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
|
|
|
|
2018-05-07 23:33:25 +02:00
|
|
|
_FORCE_INLINE_ void Vector2::operator*=(const real_t &rvalue) {
|
|
|
|
x *= rvalue;
|
|
|
|
y *= rvalue;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2018-05-07 23:33:25 +02:00
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2 Vector2::operator/(const Vector2 &p_v1) const {
|
|
|
|
return Vector2(x / p_v1.x, y / p_v1.y);
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2018-05-07 23:33:25 +02:00
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2 Vector2::operator/(const real_t &rvalue) const {
|
|
|
|
return Vector2(x / rvalue, y / rvalue);
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2018-05-07 23:33:25 +02:00
|
|
|
|
|
|
|
_FORCE_INLINE_ void Vector2::operator/=(const real_t &rvalue) {
|
|
|
|
x /= rvalue;
|
|
|
|
y /= rvalue;
|
2020-05-19 15:46:49 +02:00
|
|
|
}
|
2018-05-07 23:33:25 +02:00
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2 Vector2::operator-() const {
|
|
|
|
return Vector2(-x, -y);
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ bool Vector2::operator==(const Vector2 &p_vec2) const {
|
2019-10-02 22:35:49 +02:00
|
|
|
return x == p_vec2.x && y == p_vec2.y;
|
2018-05-07 23:33:25 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2018-05-07 23:33:25 +02:00
|
|
|
_FORCE_INLINE_ bool Vector2::operator!=(const Vector2 &p_vec2) const {
|
2019-10-02 22:35:49 +02:00
|
|
|
return x != p_vec2.x || y != p_vec2.y;
|
2018-05-07 23:33:25 +02:00
|
|
|
}
|
|
|
|
|
2021-08-15 16:45:37 +02:00
|
|
|
Vector2 Vector2::lerp(const Vector2 &p_to, const real_t p_weight) const {
|
2018-05-07 23:33:25 +02:00
|
|
|
Vector2 res = *this;
|
2022-11-24 12:09:34 +01:00
|
|
|
res.x = Math::lerp(res.x, p_to.x, p_weight);
|
|
|
|
res.y = Math::lerp(res.y, p_to.y, p_weight);
|
2018-05-07 23:33:25 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-08-15 16:45:37 +02:00
|
|
|
Vector2 Vector2::slerp(const Vector2 &p_to, const real_t p_weight) const {
|
2021-12-12 21:19:47 +01:00
|
|
|
real_t start_length_sq = length_squared();
|
|
|
|
real_t end_length_sq = p_to.length_squared();
|
2022-02-06 12:14:58 +01:00
|
|
|
if (unlikely(start_length_sq == 0.0f || end_length_sq == 0.0f)) {
|
2021-12-12 21:19:47 +01:00
|
|
|
// Zero length vectors have no angle, so the best we can do is either lerp or throw an error.
|
|
|
|
return lerp(p_to, p_weight);
|
|
|
|
}
|
|
|
|
real_t start_length = Math::sqrt(start_length_sq);
|
|
|
|
real_t result_length = Math::lerp(start_length, Math::sqrt(end_length_sq), p_weight);
|
|
|
|
real_t angle = angle_to(p_to);
|
|
|
|
return rotated(angle * p_weight) * (result_length / start_length);
|
2018-05-07 23:33:25 +02:00
|
|
|
}
|
|
|
|
|
2022-06-27 19:41:32 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-07-28 21:55:10 +02:00
|
|
|
Vector2 Vector2::cubic_interpolate_in_time(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight, const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const {
|
|
|
|
Vector2 res = *this;
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-06-27 19:41:32 +02:00
|
|
|
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;
|
2022-11-24 12:09:34 +01:00
|
|
|
res.x = Math::bezier_interpolate(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
|
|
|
|
res.y = Math::bezier_interpolate(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
|
|
|
|
return res;
|
2022-06-27 19:41:32 +02:00
|
|
|
}
|
|
|
|
|
2022-11-23 04:11:58 +01:00
|
|
|
Vector2 Vector2::bezier_derivative(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t) const {
|
|
|
|
Vector2 res = *this;
|
2022-11-24 12:09:34 +01:00
|
|
|
res.x = Math::bezier_derivative(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t);
|
|
|
|
res.y = Math::bezier_derivative(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t);
|
|
|
|
return res;
|
2022-11-23 04:11:58 +01:00
|
|
|
}
|
|
|
|
|
2020-12-07 09:16:31 +01:00
|
|
|
Vector2 Vector2::direction_to(const Vector2 &p_to) const {
|
|
|
|
Vector2 ret(p_to.x - x, p_to.y - y);
|
2019-03-27 11:51:05 +01:00
|
|
|
ret.normalize();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-02-03 22:16:58 +01:00
|
|
|
// Multiplication operators required to workaround issues with LLVM using implicit conversion
|
|
|
|
// to Vector2i instead for integers where it should not.
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2 operator*(const float p_scalar, const Vector2 &p_vec) {
|
|
|
|
return p_vec * p_scalar;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2 operator*(const double p_scalar, const Vector2 &p_vec) {
|
|
|
|
return p_vec * p_scalar;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2 operator*(const int32_t p_scalar, const Vector2 &p_vec) {
|
|
|
|
return p_vec * p_scalar;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2 operator*(const int64_t p_scalar, const Vector2 &p_vec) {
|
|
|
|
return p_vec * p_scalar;
|
|
|
|
}
|
|
|
|
|
2018-05-07 23:33:25 +02:00
|
|
|
typedef Vector2 Size2;
|
|
|
|
typedef Vector2 Point2;
|
|
|
|
|
|
|
|
#endif // VECTOR2_H
|