1 | /**************************************************************************/ |
2 | /* vector2.h */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #ifndef VECTOR2_H |
32 | #define VECTOR2_H |
33 | |
34 | #include "core/error/error_macros.h" |
35 | #include "core/math/math_funcs.h" |
36 | |
37 | class String; |
38 | struct Vector2i; |
39 | |
40 | struct _NO_DISCARD_ Vector2 { |
41 | static const int AXIS_COUNT = 2; |
42 | |
43 | enum Axis { |
44 | AXIS_X, |
45 | AXIS_Y, |
46 | }; |
47 | |
48 | union { |
49 | struct { |
50 | union { |
51 | real_t x; |
52 | real_t width; |
53 | }; |
54 | union { |
55 | real_t y; |
56 | real_t height; |
57 | }; |
58 | }; |
59 | |
60 | real_t coord[2] = { 0 }; |
61 | }; |
62 | |
63 | _FORCE_INLINE_ real_t &operator[](int p_idx) { |
64 | DEV_ASSERT((unsigned int)p_idx < 2); |
65 | return coord[p_idx]; |
66 | } |
67 | _FORCE_INLINE_ const real_t &operator[](int p_idx) const { |
68 | DEV_ASSERT((unsigned int)p_idx < 2); |
69 | return coord[p_idx]; |
70 | } |
71 | |
72 | _FORCE_INLINE_ Vector2::Axis min_axis_index() const { |
73 | return x < y ? Vector2::AXIS_X : Vector2::AXIS_Y; |
74 | } |
75 | |
76 | _FORCE_INLINE_ Vector2::Axis max_axis_index() const { |
77 | return x < y ? Vector2::AXIS_Y : Vector2::AXIS_X; |
78 | } |
79 | |
80 | void normalize(); |
81 | Vector2 normalized() const; |
82 | bool is_normalized() const; |
83 | |
84 | real_t length() const; |
85 | real_t length_squared() const; |
86 | Vector2 limit_length(const real_t p_len = 1.0) const; |
87 | |
88 | Vector2 min(const Vector2 &p_vector2) const { |
89 | return Vector2(MIN(x, p_vector2.x), MIN(y, p_vector2.y)); |
90 | } |
91 | |
92 | Vector2 max(const Vector2 &p_vector2) const { |
93 | return Vector2(MAX(x, p_vector2.x), MAX(y, p_vector2.y)); |
94 | } |
95 | |
96 | real_t distance_to(const Vector2 &p_vector2) const; |
97 | real_t distance_squared_to(const Vector2 &p_vector2) const; |
98 | real_t angle_to(const Vector2 &p_vector2) const; |
99 | real_t angle_to_point(const Vector2 &p_vector2) const; |
100 | _FORCE_INLINE_ Vector2 direction_to(const Vector2 &p_to) const; |
101 | |
102 | real_t dot(const Vector2 &p_other) const; |
103 | real_t cross(const Vector2 &p_other) const; |
104 | Vector2 posmod(const real_t p_mod) const; |
105 | Vector2 posmodv(const Vector2 &p_modv) const; |
106 | Vector2 project(const Vector2 &p_to) const; |
107 | |
108 | Vector2 plane_project(const real_t p_d, const Vector2 &p_vec) const; |
109 | |
110 | _FORCE_INLINE_ Vector2 lerp(const Vector2 &p_to, const real_t p_weight) const; |
111 | _FORCE_INLINE_ Vector2 slerp(const Vector2 &p_to, const real_t p_weight) const; |
112 | _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; |
113 | _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; |
114 | _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; |
115 | _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; |
116 | |
117 | Vector2 move_toward(const Vector2 &p_to, const real_t p_delta) const; |
118 | |
119 | Vector2 slide(const Vector2 &p_normal) const; |
120 | Vector2 bounce(const Vector2 &p_normal) const; |
121 | Vector2 reflect(const Vector2 &p_normal) const; |
122 | |
123 | bool is_equal_approx(const Vector2 &p_v) const; |
124 | bool is_zero_approx() const; |
125 | bool is_finite() const; |
126 | |
127 | Vector2 operator+(const Vector2 &p_v) const; |
128 | void operator+=(const Vector2 &p_v); |
129 | Vector2 operator-(const Vector2 &p_v) const; |
130 | void operator-=(const Vector2 &p_v); |
131 | Vector2 operator*(const Vector2 &p_v1) const; |
132 | |
133 | Vector2 operator*(const real_t &rvalue) const; |
134 | void operator*=(const real_t &rvalue); |
135 | void operator*=(const Vector2 &rvalue) { *this = *this * rvalue; } |
136 | |
137 | Vector2 operator/(const Vector2 &p_v1) const; |
138 | |
139 | Vector2 operator/(const real_t &rvalue) const; |
140 | |
141 | void operator/=(const real_t &rvalue); |
142 | void operator/=(const Vector2 &rvalue) { *this = *this / rvalue; } |
143 | |
144 | Vector2 operator-() const; |
145 | |
146 | bool operator==(const Vector2 &p_vec2) const; |
147 | bool operator!=(const Vector2 &p_vec2) const; |
148 | |
149 | bool operator<(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y < p_vec2.y) : (x < p_vec2.x); } |
150 | bool operator>(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y > p_vec2.y) : (x > p_vec2.x); } |
151 | bool operator<=(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); } |
152 | bool operator>=(const Vector2 &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); } |
153 | |
154 | real_t angle() const; |
155 | static Vector2 from_angle(const real_t p_angle); |
156 | |
157 | _FORCE_INLINE_ Vector2 abs() const { |
158 | return Vector2(Math::abs(x), Math::abs(y)); |
159 | } |
160 | |
161 | Vector2 rotated(const real_t p_by) const; |
162 | Vector2 orthogonal() const { |
163 | return Vector2(y, -x); |
164 | } |
165 | |
166 | Vector2 sign() const; |
167 | Vector2 floor() const; |
168 | Vector2 ceil() const; |
169 | Vector2 round() const; |
170 | Vector2 snapped(const Vector2 &p_by) const; |
171 | Vector2 clamp(const Vector2 &p_min, const Vector2 &p_max) const; |
172 | real_t aspect() const { return width / height; } |
173 | |
174 | operator String() const; |
175 | operator Vector2i() const; |
176 | |
177 | _FORCE_INLINE_ Vector2() {} |
178 | _FORCE_INLINE_ Vector2(const real_t p_x, const real_t p_y) { |
179 | x = p_x; |
180 | y = p_y; |
181 | } |
182 | }; |
183 | |
184 | _FORCE_INLINE_ Vector2 Vector2::plane_project(const real_t p_d, const Vector2 &p_vec) const { |
185 | return p_vec - *this * (dot(p_vec) - p_d); |
186 | } |
187 | |
188 | _FORCE_INLINE_ Vector2 Vector2::operator+(const Vector2 &p_v) const { |
189 | return Vector2(x + p_v.x, y + p_v.y); |
190 | } |
191 | |
192 | _FORCE_INLINE_ void Vector2::operator+=(const Vector2 &p_v) { |
193 | x += p_v.x; |
194 | y += p_v.y; |
195 | } |
196 | |
197 | _FORCE_INLINE_ Vector2 Vector2::operator-(const Vector2 &p_v) const { |
198 | return Vector2(x - p_v.x, y - p_v.y); |
199 | } |
200 | |
201 | _FORCE_INLINE_ void Vector2::operator-=(const Vector2 &p_v) { |
202 | x -= p_v.x; |
203 | y -= p_v.y; |
204 | } |
205 | |
206 | _FORCE_INLINE_ Vector2 Vector2::operator*(const Vector2 &p_v1) const { |
207 | return Vector2(x * p_v1.x, y * p_v1.y); |
208 | } |
209 | |
210 | _FORCE_INLINE_ Vector2 Vector2::operator*(const real_t &rvalue) const { |
211 | return Vector2(x * rvalue, y * rvalue); |
212 | } |
213 | |
214 | _FORCE_INLINE_ void Vector2::operator*=(const real_t &rvalue) { |
215 | x *= rvalue; |
216 | y *= rvalue; |
217 | } |
218 | |
219 | _FORCE_INLINE_ Vector2 Vector2::operator/(const Vector2 &p_v1) const { |
220 | return Vector2(x / p_v1.x, y / p_v1.y); |
221 | } |
222 | |
223 | _FORCE_INLINE_ Vector2 Vector2::operator/(const real_t &rvalue) const { |
224 | return Vector2(x / rvalue, y / rvalue); |
225 | } |
226 | |
227 | _FORCE_INLINE_ void Vector2::operator/=(const real_t &rvalue) { |
228 | x /= rvalue; |
229 | y /= rvalue; |
230 | } |
231 | |
232 | _FORCE_INLINE_ Vector2 Vector2::operator-() const { |
233 | return Vector2(-x, -y); |
234 | } |
235 | |
236 | _FORCE_INLINE_ bool Vector2::operator==(const Vector2 &p_vec2) const { |
237 | return x == p_vec2.x && y == p_vec2.y; |
238 | } |
239 | |
240 | _FORCE_INLINE_ bool Vector2::operator!=(const Vector2 &p_vec2) const { |
241 | return x != p_vec2.x || y != p_vec2.y; |
242 | } |
243 | |
244 | Vector2 Vector2::lerp(const Vector2 &p_to, const real_t p_weight) const { |
245 | Vector2 res = *this; |
246 | res.x = Math::lerp(res.x, p_to.x, p_weight); |
247 | res.y = Math::lerp(res.y, p_to.y, p_weight); |
248 | return res; |
249 | } |
250 | |
251 | Vector2 Vector2::slerp(const Vector2 &p_to, const real_t p_weight) const { |
252 | real_t start_length_sq = length_squared(); |
253 | real_t end_length_sq = p_to.length_squared(); |
254 | if (unlikely(start_length_sq == 0.0f || end_length_sq == 0.0f)) { |
255 | // Zero length vectors have no angle, so the best we can do is either lerp or throw an error. |
256 | return lerp(p_to, p_weight); |
257 | } |
258 | real_t start_length = Math::sqrt(start_length_sq); |
259 | real_t result_length = Math::lerp(start_length, Math::sqrt(end_length_sq), p_weight); |
260 | real_t angle = angle_to(p_to); |
261 | return rotated(angle * p_weight) * (result_length / start_length); |
262 | } |
263 | |
264 | Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, const real_t p_weight) const { |
265 | Vector2 res = *this; |
266 | res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight); |
267 | res.y = Math::cubic_interpolate(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight); |
268 | return res; |
269 | } |
270 | |
271 | 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 { |
272 | Vector2 res = *this; |
273 | 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); |
274 | 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); |
275 | return res; |
276 | } |
277 | |
278 | Vector2 Vector2::bezier_interpolate(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t) const { |
279 | Vector2 res = *this; |
280 | res.x = Math::bezier_interpolate(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t); |
281 | res.y = Math::bezier_interpolate(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t); |
282 | return res; |
283 | } |
284 | |
285 | Vector2 Vector2::bezier_derivative(const Vector2 &p_control_1, const Vector2 &p_control_2, const Vector2 &p_end, const real_t p_t) const { |
286 | Vector2 res = *this; |
287 | res.x = Math::bezier_derivative(res.x, p_control_1.x, p_control_2.x, p_end.x, p_t); |
288 | res.y = Math::bezier_derivative(res.y, p_control_1.y, p_control_2.y, p_end.y, p_t); |
289 | return res; |
290 | } |
291 | |
292 | Vector2 Vector2::direction_to(const Vector2 &p_to) const { |
293 | Vector2 ret(p_to.x - x, p_to.y - y); |
294 | ret.normalize(); |
295 | return ret; |
296 | } |
297 | |
298 | // Multiplication operators required to workaround issues with LLVM using implicit conversion |
299 | // to Vector2i instead for integers where it should not. |
300 | |
301 | _FORCE_INLINE_ Vector2 operator*(const float p_scalar, const Vector2 &p_vec) { |
302 | return p_vec * p_scalar; |
303 | } |
304 | |
305 | _FORCE_INLINE_ Vector2 operator*(const double p_scalar, const Vector2 &p_vec) { |
306 | return p_vec * p_scalar; |
307 | } |
308 | |
309 | _FORCE_INLINE_ Vector2 operator*(const int32_t p_scalar, const Vector2 &p_vec) { |
310 | return p_vec * p_scalar; |
311 | } |
312 | |
313 | _FORCE_INLINE_ Vector2 operator*(const int64_t p_scalar, const Vector2 &p_vec) { |
314 | return p_vec * p_scalar; |
315 | } |
316 | |
317 | typedef Vector2 Size2; |
318 | typedef Vector2 Point2; |
319 | |
320 | #endif // VECTOR2_H |
321 | |