2018-05-07 21:33:25 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* vector2.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2018-05-07 21:33:25 +00:00
|
|
|
/* */
|
|
|
|
/* 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
|
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/math/math_funcs.h"
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/string/ustring.h"
|
2018-05-07 21:33:25 +00:00
|
|
|
|
2018-08-11 05:32:45 +00:00
|
|
|
struct Vector2i;
|
|
|
|
|
2018-05-07 21:33:25 +00:00
|
|
|
struct Vector2 {
|
2019-08-05 01:50:28 +00:00
|
|
|
enum Axis {
|
|
|
|
AXIS_X,
|
|
|
|
AXIS_Y,
|
|
|
|
};
|
|
|
|
|
2018-05-07 21:33:25 +00:00
|
|
|
union {
|
2020-05-12 15:01:17 +00:00
|
|
|
real_t x = 0;
|
2018-05-07 21:33:25 +00:00
|
|
|
real_t width;
|
|
|
|
};
|
|
|
|
union {
|
2020-05-12 15:01:17 +00:00
|
|
|
real_t y = 0;
|
2018-05-07 21:33:25 +00:00
|
|
|
real_t height;
|
|
|
|
};
|
|
|
|
|
|
|
|
_FORCE_INLINE_ real_t &operator[](int p_idx) {
|
|
|
|
return p_idx ? y : x;
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ const real_t &operator[](int p_idx) const {
|
|
|
|
return p_idx ? y : x;
|
|
|
|
}
|
|
|
|
|
|
|
|
void normalize();
|
|
|
|
Vector2 normalized() const;
|
|
|
|
bool is_normalized() const;
|
|
|
|
|
|
|
|
real_t length() const;
|
|
|
|
real_t length_squared() const;
|
|
|
|
|
2020-10-08 00:29:49 +00: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 21:33:25 +00: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 08:16:31 +00:00
|
|
|
_FORCE_INLINE_ Vector2 direction_to(const Vector2 &p_to) const;
|
2018-05-07 21:33:25 +00:00
|
|
|
|
|
|
|
real_t dot(const Vector2 &p_other) const;
|
|
|
|
real_t cross(const Vector2 &p_other) const;
|
2019-08-05 01:50:28 +00:00
|
|
|
Vector2 posmod(const real_t p_mod) const;
|
|
|
|
Vector2 posmodv(const Vector2 &p_modv) const;
|
2020-12-07 08:16:31 +00:00
|
|
|
Vector2 project(const Vector2 &p_to) const;
|
2018-05-07 21:33:25 +00:00
|
|
|
|
|
|
|
Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const;
|
|
|
|
|
|
|
|
Vector2 clamped(real_t p_len) const;
|
|
|
|
|
2020-12-07 08:16:31 +00:00
|
|
|
_FORCE_INLINE_ Vector2 lerp(const Vector2 &p_to, real_t p_weight) const;
|
|
|
|
_FORCE_INLINE_ Vector2 slerp(const Vector2 &p_to, real_t p_weight) const;
|
|
|
|
Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_weight) const;
|
2019-04-07 21:40:56 +00:00
|
|
|
Vector2 move_toward(const Vector2 &p_to, const real_t p_delta) const;
|
2018-05-07 21:33:25 +00: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 20:33:45 +00:00
|
|
|
bool is_equal_approx(const Vector2 &p_v) const;
|
|
|
|
|
2018-05-07 21:33:25 +00: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 18:54:33 +00:00
|
|
|
void operator/=(const Vector2 &rvalue) { *this = *this / rvalue; }
|
2018-05-07 21:33:25 +00:00
|
|
|
|
|
|
|
Vector2 operator-() const;
|
|
|
|
|
|
|
|
bool operator==(const Vector2 &p_vec2) const;
|
|
|
|
bool operator!=(const Vector2 &p_vec2) const;
|
|
|
|
|
2020-09-08 22:10:58 +00: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 21:33:25 +00:00
|
|
|
|
|
|
|
real_t angle() const;
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2 abs() const {
|
|
|
|
return Vector2(Math::abs(x), Math::abs(y));
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 rotated(real_t p_by) const;
|
2020-12-06 18:16:06 +00:00
|
|
|
Vector2 orthogonal() const {
|
2018-05-07 21:33:25 +00:00
|
|
|
return Vector2(y, -x);
|
|
|
|
}
|
|
|
|
|
2019-08-05 01:50:28 +00:00
|
|
|
Vector2 sign() const;
|
2018-05-07 21:33:25 +00:00
|
|
|
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 { return String::num(x) + ", " + String::num(y); }
|
|
|
|
|
2020-05-12 15:01:17 +00:00
|
|
|
_FORCE_INLINE_ Vector2() {}
|
2018-05-07 21:33:25 +00:00
|
|
|
_FORCE_INLINE_ Vector2(real_t p_x, real_t p_y) {
|
|
|
|
x = p_x;
|
|
|
|
y = p_y;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const {
|
|
|
|
return p_vec - *this * (dot(p_vec) - p_d);
|
|
|
|
}
|
|
|
|
|
2020-09-17 11:56:36 +00:00
|
|
|
_FORCE_INLINE_ Vector2 operator*(float p_scalar, const Vector2 &p_vec) {
|
|
|
|
return p_vec * p_scalar;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2 operator*(double p_scalar, const Vector2 &p_vec) {
|
|
|
|
return p_vec * p_scalar;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2 operator*(int32_t p_scalar, const Vector2 &p_vec) {
|
|
|
|
return p_vec * p_scalar;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2 operator*(int64_t p_scalar, const Vector2 &p_vec) {
|
2018-05-07 21:33:25 +00:00
|
|
|
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);
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2018-05-07 21:33:25 +00:00
|
|
|
_FORCE_INLINE_ void Vector2::operator+=(const Vector2 &p_v) {
|
|
|
|
x += p_v.x;
|
|
|
|
y += p_v.y;
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2018-05-07 21:33:25 +00:00
|
|
|
_FORCE_INLINE_ Vector2 Vector2::operator-(const Vector2 &p_v) const {
|
|
|
|
return Vector2(x - p_v.x, y - p_v.y);
|
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2018-05-07 21:33:25 +00: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 13:46:49 +00:00
|
|
|
}
|
2018-05-07 21:33:25 +00:00
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2 Vector2::operator*(const real_t &rvalue) const {
|
|
|
|
return Vector2(x * rvalue, y * rvalue);
|
2020-05-19 13:46:49 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 21:33:25 +00:00
|
|
|
_FORCE_INLINE_ void Vector2::operator*=(const real_t &rvalue) {
|
|
|
|
x *= rvalue;
|
|
|
|
y *= rvalue;
|
2020-05-19 13:46:49 +00:00
|
|
|
}
|
2018-05-07 21:33:25 +00:00
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2 Vector2::operator/(const Vector2 &p_v1) const {
|
|
|
|
return Vector2(x / p_v1.x, y / p_v1.y);
|
2020-05-19 13:46:49 +00:00
|
|
|
}
|
2018-05-07 21:33:25 +00:00
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2 Vector2::operator/(const real_t &rvalue) const {
|
|
|
|
return Vector2(x / rvalue, y / rvalue);
|
2020-05-19 13:46:49 +00:00
|
|
|
}
|
2018-05-07 21:33:25 +00:00
|
|
|
|
|
|
|
_FORCE_INLINE_ void Vector2::operator/=(const real_t &rvalue) {
|
|
|
|
x /= rvalue;
|
|
|
|
y /= rvalue;
|
2020-05-19 13:46:49 +00:00
|
|
|
}
|
2018-05-07 21:33:25 +00:00
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2 Vector2::operator-() const {
|
|
|
|
return Vector2(-x, -y);
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ bool Vector2::operator==(const Vector2 &p_vec2) const {
|
2019-10-02 20:35:49 +00:00
|
|
|
return x == p_vec2.x && y == p_vec2.y;
|
2018-05-07 21:33:25 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2018-05-07 21:33:25 +00:00
|
|
|
_FORCE_INLINE_ bool Vector2::operator!=(const Vector2 &p_vec2) const {
|
2019-10-02 20:35:49 +00:00
|
|
|
return x != p_vec2.x || y != p_vec2.y;
|
2018-05-07 21:33:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 08:16:31 +00:00
|
|
|
Vector2 Vector2::lerp(const Vector2 &p_to, real_t p_weight) const {
|
2018-05-07 21:33:25 +00:00
|
|
|
Vector2 res = *this;
|
|
|
|
|
2020-12-07 08:16:31 +00:00
|
|
|
res.x += (p_weight * (p_to.x - x));
|
|
|
|
res.y += (p_weight * (p_to.y - y));
|
2018-05-07 21:33:25 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-12-07 08:16:31 +00:00
|
|
|
Vector2 Vector2::slerp(const Vector2 &p_to, real_t p_weight) const {
|
2018-05-07 21:33:25 +00:00
|
|
|
#ifdef MATH_CHECKS
|
2020-01-24 12:51:25 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(!is_normalized(), Vector2(), "The start Vector2 must be normalized.");
|
2018-05-07 21:33:25 +00:00
|
|
|
#endif
|
2020-12-07 08:16:31 +00:00
|
|
|
real_t theta = angle_to(p_to);
|
|
|
|
return rotated(theta * p_weight);
|
2018-05-07 21:33:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 08:16:31 +00:00
|
|
|
Vector2 Vector2::direction_to(const Vector2 &p_to) const {
|
|
|
|
Vector2 ret(p_to.x - x, p_to.y - y);
|
2019-03-27 10:51:05 +00:00
|
|
|
ret.normalize();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-05-07 21:33:25 +00:00
|
|
|
typedef Vector2 Size2;
|
|
|
|
typedef Vector2 Point2;
|
|
|
|
|
2018-08-11 05:32:45 +00:00
|
|
|
/* INTEGER STUFF */
|
|
|
|
|
|
|
|
struct Vector2i {
|
2019-08-05 01:50:28 +00:00
|
|
|
enum Axis {
|
|
|
|
AXIS_X,
|
|
|
|
AXIS_Y,
|
|
|
|
};
|
|
|
|
|
2018-08-11 05:32:45 +00:00
|
|
|
union {
|
2021-01-22 10:36:48 +00:00
|
|
|
int32_t x = 0;
|
|
|
|
int32_t width;
|
2018-08-11 05:32:45 +00:00
|
|
|
};
|
|
|
|
union {
|
2021-01-22 10:36:48 +00:00
|
|
|
int32_t y = 0;
|
|
|
|
int32_t height;
|
2018-08-11 05:32:45 +00:00
|
|
|
};
|
|
|
|
|
2021-01-22 10:36:48 +00:00
|
|
|
_FORCE_INLINE_ int32_t &operator[](int p_idx) {
|
2018-08-11 05:32:45 +00:00
|
|
|
return p_idx ? y : x;
|
|
|
|
}
|
2021-01-22 10:36:48 +00:00
|
|
|
_FORCE_INLINE_ const int32_t &operator[](int p_idx) const {
|
2018-08-11 05:32:45 +00:00
|
|
|
return p_idx ? y : x;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2i operator+(const Vector2i &p_v) const;
|
|
|
|
void operator+=(const Vector2i &p_v);
|
|
|
|
Vector2i operator-(const Vector2i &p_v) const;
|
|
|
|
void operator-=(const Vector2i &p_v);
|
|
|
|
Vector2i operator*(const Vector2i &p_v1) const;
|
|
|
|
|
2021-01-22 10:36:48 +00:00
|
|
|
Vector2i operator*(const int32_t &rvalue) const;
|
|
|
|
void operator*=(const int32_t &rvalue);
|
2018-08-11 05:32:45 +00:00
|
|
|
|
|
|
|
Vector2i operator/(const Vector2i &p_v1) const;
|
2021-01-22 10:36:48 +00:00
|
|
|
Vector2i operator/(const int32_t &rvalue) const;
|
|
|
|
void operator/=(const int32_t &rvalue);
|
2018-08-11 05:32:45 +00:00
|
|
|
|
2020-11-05 02:01:55 +00:00
|
|
|
Vector2i operator%(const Vector2i &p_v1) const;
|
2021-01-22 10:36:48 +00:00
|
|
|
Vector2i operator%(const int32_t &rvalue) const;
|
|
|
|
void operator%=(const int32_t &rvalue);
|
2020-11-05 02:01:55 +00:00
|
|
|
|
2018-08-11 05:32:45 +00:00
|
|
|
Vector2i operator-() const;
|
|
|
|
bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
|
|
|
|
bool operator>(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); }
|
|
|
|
|
2020-02-22 03:26:41 +00:00
|
|
|
bool operator<=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); }
|
|
|
|
bool operator>=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); }
|
|
|
|
|
2018-08-11 05:32:45 +00:00
|
|
|
bool operator==(const Vector2i &p_vec2) const;
|
|
|
|
bool operator!=(const Vector2i &p_vec2) const;
|
|
|
|
|
2020-02-22 03:26:41 +00:00
|
|
|
real_t aspect() const { return width / (real_t)height; }
|
|
|
|
Vector2i sign() const { return Vector2i(SGN(x), SGN(y)); }
|
|
|
|
Vector2i abs() const { return Vector2i(ABS(x), ABS(y)); }
|
2018-08-11 05:32:45 +00:00
|
|
|
|
|
|
|
operator String() const { return String::num(x) + ", " + String::num(y); }
|
|
|
|
|
|
|
|
operator Vector2() const { return Vector2(x, y); }
|
2020-05-12 15:01:17 +00:00
|
|
|
|
|
|
|
inline Vector2i() {}
|
2018-08-11 05:32:45 +00:00
|
|
|
inline Vector2i(const Vector2 &p_vec2) {
|
2021-01-22 10:36:48 +00:00
|
|
|
x = (int32_t)p_vec2.x;
|
|
|
|
y = (int32_t)p_vec2.y;
|
2018-08-11 05:32:45 +00:00
|
|
|
}
|
2021-01-22 10:36:48 +00:00
|
|
|
inline Vector2i(int32_t p_x, int32_t p_y) {
|
2018-08-11 05:32:45 +00:00
|
|
|
x = p_x;
|
|
|
|
y = p_y;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-17 11:56:36 +00:00
|
|
|
_FORCE_INLINE_ Vector2i operator*(const int32_t &p_scalar, const Vector2i &p_vector) {
|
|
|
|
return p_vector * p_scalar;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2i operator*(const int64_t &p_scalar, const Vector2i &p_vector) {
|
|
|
|
return p_vector * p_scalar;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2i operator*(const float &p_scalar, const Vector2i &p_vector) {
|
|
|
|
return p_vector * p_scalar;
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Vector2i operator*(const double &p_scalar, const Vector2i &p_vector) {
|
|
|
|
return p_vector * p_scalar;
|
|
|
|
}
|
|
|
|
|
2018-08-11 05:32:45 +00:00
|
|
|
typedef Vector2i Size2i;
|
|
|
|
typedef Vector2i Point2i;
|
|
|
|
|
2018-05-07 21:33:25 +00:00
|
|
|
#endif // VECTOR2_H
|