3
3
* License, v. 2.0. If a copy of the MPL was not distributed with this
4
4
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
5
*/
6
- use std:: ops:: * ;
7
6
use std:: fmt;
7
+ use std:: ops:: * ;
8
8
9
9
use godot_ffi as sys;
10
10
use sys:: { ffi_methods, GodotFfi } ;
11
11
12
+ use crate :: builtin:: math:: * ;
12
13
use crate :: builtin:: { inner, Vector2i } ;
13
14
14
15
/// Vector used for 2D math using floating point coordinates.
@@ -80,10 +81,6 @@ impl Vector2 {
80
81
glam:: Vec2 :: new ( self . x , self . y )
81
82
}
82
83
83
- pub fn abs ( self ) -> Self {
84
- Self ( self . to_glam ( ) . abs ( ) )
85
- }
86
-
87
84
pub fn angle ( self ) -> f32 {
88
85
self . y . atan2 ( self . x )
89
86
}
@@ -100,91 +97,44 @@ impl Vector2 {
100
97
self . x / self . y
101
98
}
102
99
103
- pub fn bezier_derivative (
104
- self ,
105
- control_1 : Self ,
106
- control_2 : Self ,
107
- end : Self ,
108
- t : f32 ,
109
- ) -> Self {
110
- let x = bezier_derivative (
111
- self . x ,
112
- control_1. x ,
113
- control_2. x ,
114
- end. x ,
115
- t,
116
- ) ;
117
- let y = bezier_derivative (
118
- self . y ,
119
- control_1. y ,
120
- control_2. y ,
121
- end. y ,
122
- t,
123
- ) ;
100
+ pub fn bezier_derivative ( self , control_1 : Self , control_2 : Self , end : Self , t : f32 ) -> Self {
101
+ let x = bezier_derivative ( self . x , control_1. x , control_2. x , end. x , t) ;
102
+ let y = bezier_derivative ( self . y , control_1. y , control_2. y , end. y , t) ;
124
103
125
- Self :: new ( x, y)
104
+ Self :: new ( x, y)
126
105
}
127
106
128
- pub fn bezier_interpolate (
129
- self ,
130
- control_1 : Self ,
131
- control_2 : Self ,
132
- end : Self ,
133
- t : f32 ,
134
- ) -> Self {
135
- let x = bezier_interpolate (
136
- self . x ,
137
- control_1. x ,
138
- control_2. x ,
139
- end. x ,
140
- t,
141
- ) ;
142
- let y = bezier_interpolate (
143
- self . y ,
144
- control_1. y ,
145
- control_2. y ,
146
- end. y ,
147
- t,
148
- ) ;
107
+ pub fn bezier_interpolate ( self , control_1 : Self , control_2 : Self , end : Self , t : f32 ) -> Self {
108
+ let x = bezier_interpolate ( self . x , control_1. x , control_2. x , end. x , t) ;
109
+ let y = bezier_interpolate ( self . y , control_1. y , control_2. y , end. y , t) ;
149
110
150
- Self :: new ( x, y)
111
+ Self :: new ( x, y)
151
112
}
152
113
153
114
pub fn bounce ( self , normal : Self ) -> Self {
154
115
-self . reflect ( normal)
155
116
}
156
117
157
118
pub fn ceil ( self ) -> Self {
158
- Self ( self . to_glam ( ) . ceil ( ) )
119
+ Self :: from_glam ( self . to_glam ( ) . ceil ( ) )
159
120
}
160
121
161
122
pub fn clamp ( self , min : Self , max : Self ) -> Self {
162
- Self ( self . to_glam ( ) . clamp ( min. to_glam ( ) , max. to_glam ( ) ) )
123
+ Self :: from_glam ( self . to_glam ( ) . clamp ( min. to_glam ( ) , max. to_glam ( ) ) )
163
124
}
164
125
165
126
pub fn cross ( self , with : Self ) -> f32 {
166
127
self . to_glam ( ) . perp_dot ( with. to_glam ( ) )
167
128
}
168
129
169
130
pub fn cubic_interpolate ( self , b : Self , pre_a : Self , post_b : Self , weight : f32 ) -> Self {
170
- let x = cubic_interpolate (
171
- self . x ,
172
- b. x ,
173
- pre_a. x ,
174
- post_b. x ,
175
- weight,
176
- ) ;
177
- let y = cubic_interpolate (
178
- self . y ,
179
- b. y ,
180
- pre_a. y ,
181
- post_b. y ,
182
- weight,
183
- ) ;
131
+ let x = cubic_interpolate ( self . x , b. x , pre_a. x , post_b. x , weight) ;
132
+ let y = cubic_interpolate ( self . y , b. y , pre_a. y , post_b. y , weight) ;
184
133
185
- Self :: new ( x, y)
134
+ Self :: new ( x, y)
186
135
}
187
136
137
+ #[ allow( clippy:: too_many_arguments) ]
188
138
pub fn cubic_interpolate_in_time (
189
139
self ,
190
140
b : Self ,
@@ -196,27 +146,13 @@ impl Vector2 {
196
146
post_b_t : f32 ,
197
147
) -> Self {
198
148
let x = cubic_interpolate_in_time (
199
- self . x ,
200
- b. x ,
201
- pre_a. x ,
202
- post_b. x ,
203
- weight,
204
- b_t,
205
- pre_a_t,
206
- post_b_t,
149
+ self . x , b. x , pre_a. x , post_b. x , weight, b_t, pre_a_t, post_b_t,
207
150
) ;
208
151
let y = cubic_interpolate_in_time (
209
- self . y ,
210
- b. y ,
211
- pre_a. y ,
212
- post_b. y ,
213
- weight,
214
- b_t,
215
- pre_a_t,
216
- post_b_t,
152
+ self . y , b. y , pre_a. y , post_b. y , weight, b_t, pre_a_t, post_b_t,
217
153
) ;
218
154
219
- Self :: new ( x, y)
155
+ Self :: new ( x, y)
220
156
}
221
157
222
158
pub fn direction_to ( self , to : Self ) -> Self {
@@ -264,37 +200,31 @@ impl Vector2 {
264
200
}
265
201
266
202
pub fn lerp ( self , to : Self , weight : f32 ) -> Self {
267
- Self ( self . to_glam ( ) . lerp ( to. to_glam ( ) , weight) )
203
+ Self :: from_glam ( self . to_glam ( ) . lerp ( to. to_glam ( ) , weight) )
268
204
}
269
205
270
206
pub fn limit_length ( self , length : Option < f32 > ) -> Self {
271
- Self ( self . to_glam ( ) . clamp_length_max ( length. unwrap_or ( 1.0 ) ) )
207
+ Self :: from_glam ( self . to_glam ( ) . clamp_length_max ( length. unwrap_or ( 1.0 ) ) )
272
208
}
273
209
274
210
pub fn max_axis_index ( self ) -> i32 {
275
- if self . to_glam ( ) . max_element ( ) == self . x {
276
- 0
277
- } else {
278
- 1
279
- }
211
+ let me = self . to_glam ( ) . max_element ( ) ;
212
+ ( me == self . y ) as i32
280
213
}
281
214
282
215
pub fn min_axis_index ( self ) -> i32 {
283
- if self . to_glam ( ) . min_element ( ) == self . x {
284
- 0
285
- } else {
286
- 1
287
- }
216
+ let me = self . to_glam ( ) . min_element ( ) ;
217
+ ( me == self . y ) as i32
288
218
}
289
219
290
220
pub fn move_toward ( self , to : Self , delta : f32 ) -> Self {
291
221
let vd = to - self ;
292
222
let len = vd. length ( ) ;
293
223
if len <= delta || len < CMP_EPSILON {
294
- return to ;
224
+ to
295
225
} else {
296
- return self + vd / len * delta;
297
- } ;
226
+ self + vd / len * delta
227
+ }
298
228
}
299
229
300
230
pub fn orthogonal ( self ) -> Self {
@@ -306,10 +236,7 @@ impl Vector2 {
306
236
}
307
237
308
238
pub fn posmodv ( self , modv : Self ) -> Self {
309
- Self :: new (
310
- fposmod ( self . x , modv. x ) ,
311
- fposmod ( self . y , modv. y ) ,
312
- )
239
+ Self :: new ( fposmod ( self . x , modv. x ) , fposmod ( self . y , modv. y ) )
313
240
}
314
241
315
242
pub fn project ( self , b : Self ) -> Self {
@@ -345,17 +272,14 @@ impl Vector2 {
345
272
}
346
273
347
274
pub fn snapped ( self , step : Self ) -> Self {
348
- Self :: new (
349
- snapped ( self . x , step. x ) ,
350
- snapped ( self . y , step. y ) ,
351
- )
275
+ Self :: new ( snapped ( self . x , step. x ) , snapped ( self . y , step. y ) )
352
276
}
353
277
354
278
/// Returns the result of rotating this vector by `angle` (in radians).
355
279
pub fn rotated ( self , angle : f32 ) -> Self {
356
280
Self :: from_glam ( glam:: Affine2 :: from_angle ( angle) . transform_vector2 ( self . to_glam ( ) ) )
357
281
}
358
-
282
+
359
283
#[ cfg( not( any( gdext_test, doctest) ) ) ]
360
284
#[ doc( hidden) ]
361
285
pub fn as_inner ( & self ) -> inner:: InnerVector2 {
0 commit comments