/
Vector2.ts
333 lines (296 loc) · 8.14 KB
/
Vector2.ts
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/***
* Vector 2D
* @group Math
*/
export class Vector2 {
/**
* @internal
*/
public static HELP_0: Vector2 = new Vector2();
/**
* @internal
*/
public static HELP_1: Vector2 = new Vector2();
/**
* @internal
*/
public static HELP_2: Vector2 = new Vector2();
public static readonly ZERO: Vector2 = new Vector2(0, 0);
public static readonly SAFE_MAX: Vector2 = new Vector2(Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
public static readonly SAFE_MIN: Vector2 = new Vector2(Number.MIN_SAFE_INTEGER, Number.MIN_SAFE_INTEGER);
/**
* The x component of the vector, the default value is 0.
*/
public x: number = 0.0;
/**
* The y component of the vector, the default value is 0.
*/
public y: number = 0.0;
/**
* Create a new Vector2.
* @param x The x component of the vector, which defaults to 0.
* @param y The y component of the vector, which defaults to 0.
*/
constructor(x: number = 0, y: number = 0) {
this.x = x;
this.y = y;
}
/**
* Returns the Angle, in radians, between two vectors.
* @param a Vector a
* @param b Vector b
* @returns result
*/
public static getAngle(a: Vector2, b: Vector2): number {
return Math.atan2(b.y - a.y, b.x - a.x);
}
/**
* Computes linear interpolation between two vectors.
* @param from starting vector
* @param to The vector in which you interpolate
* @param t
*/
public static slerp(from: Vector2, to: Vector2, t: number): Vector2 {
let v = new Vector2();
let dot = from.dot(to);
if (dot < 0) {
to.x = -to.x;
to.y = -to.y;
dot = -dot;
}
if (dot > 0.9995) {
v.x = from.x + t * (to.x - from.x);
v.y = from.y + t * (to.y - from.y);
return v;
}
let theta = Math.acos(dot);
let sinTheta = Math.sin(theta);
let scale0 = Math.sin((1 - t) * theta) / sinTheta;
let scale1 = Math.sin(t * theta) / sinTheta;
v.x = scale0 * from.x + scale1 * to.x;
v.y = scale0 * from.y + scale1 * to.y;
return v;
}
/**
* Linear interpolation between two vectors.
* @param from starting vector
* @param to The vector in which you interpolate
* @param t
* @returns
*/
public static lerp(from: Vector2, to: Vector2, t: number) {
Vector2.HELP_0.copyFrom(from);
Vector2.HELP_1.copyFrom(to);
Vector2.HELP_0.scale(t);
Vector2.HELP_1.scale(1.0 - t);
return new Vector2(Vector2.HELP_0.x + Vector2.HELP_1.x, Vector2.HELP_0.y + Vector2.HELP_1.y);
}
/**
* Sets the x and y components of this vector.
* @param x The x component of the vector, which defaults to 0.
* @param y The y component of the vector, which defaults to 0.
*/
public set(x: number = 0, y: number = 0): this {
this.x = x;
this.y = y;
return this;
}
/**
* Calculate the distance between this vector and the incoming vector.
* @param a Target vector
* @returns
*/
public distance(a: Vector2): number {
return Math.sqrt(Math.pow(this.x - a.x, 2) + Math.pow(this.y - a.y, 2));
}
/**
* Add the vectors.
* @param a
* @param target
* @returns
*/
public add(a: Vector2, target?: Vector2): Vector2 {
target = target || new Vector2();
target.x = this.x + a.x;
target.y = this.y + a.y;
return target;
}
/**
* Vector subtraction
* @param a
* @param target
*
*/
public sub(a: Vector2, target?: Vector2): Vector2 {
target = target || new Vector2();
target.x = this.x - a.x;
target.y = this.y - a.y;
return target;
}
/**
* Let's multiply the x and y values of this vector times v.
* @param v
*/
public scale(v: number): this {
this.x = this.x * v;
this.y = this.y * v;
return this;
}
/**
* Let's multiply the x and y values of this vector by a.
* @param a
* @param target
* @returns
*/
public multiply(a: number, target?: Vector2) {
target = target || new Vector2();
target.x = this.x * a;
target.y = this.y * a;
return target;
}
/**
* Let's multiply the x and y values of this vector by a.
* @param a
* @param target
* @returns
*/
public multiplyScaler(a: number): this {
this.x *= a;
this.y *= a;
return this;
}
/**
* We're going to divide the x and y values of this vector by v.
* @param v
* @param target
* @returns
*/
public divide(v: number, target?: Vector2) {
target = target || new Vector2();
target.x = this.x / v;
target.y = this.y / v;
return target;
}
/**
* Vector inversion
* @param target
* @returns
*/
public neg(target?: Vector2): Vector2 {
if (!target) target = new Vector2();
target.x = -target.x;
target.y = -target.y;
return target;
}
public abs() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
/**
* Length of vector
* @returns
*/
public length(): number {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
/**
* Returns the Angle, in radians, between the current vector and the target vector.
* @param target Target vector
* @returns
*/
public getAngle(target: Vector2): number {
return Math.atan2(target.y - this.y, target.x - this.x);
}
public unt(target?: Vector2): Vector2 {
target = target || new Vector2();
let d = this.abs();
target.x = this.x / d;
target.y = this.y / d;
return target;
}
public angleTo(v: Vector2): number {
let dx = v.x - this.x;
let dy = v.y - this.y;
return Math.atan2(dy, dx);
}
/**
* Whether two vectors are equal
* @param a Vector of comparison
* @returns
*/
public equals(a: Vector2): boolean {
if (Math.abs(this.x - a.x) < 1e-6 && Math.abs(this.y - a.y) < 1e-6) return true;
return false;
}
public pal(a: Vector2): number {
let u1 = this.unt();
let u2 = a.unt();
if (u1.equals(u2)) return 1;
if (u1.equals(u2.neg())) return -1;
return 0;
}
/**
* Returns a new vector that has the same x and y as the current vector.
* @returns
*/
public clone(): Vector2 {
return new Vector2(this.x, this.y);
}
/**
* Copy the x and y properties of the source vector to this vector
* @param v Source vector
* @returns
*/
public copyFrom(v: Vector2): Vector2 {
this.x = v.x;
this.y = v.y;
return this;
}
/**
* Take the dot product of two vectors.
* @param value Target vector
* @returns
*/
public dot(value: Vector2): number {
return this.x * value.x + this.y * value.y;
}
/**
* Convert this vector to a unit vector.
*/
public normalize(): this {
let d = this.abs();
this.x = this.x / d;
this.y = this.y / d;
return this;
}
/**
* Add two vectors
* @param otherVector Additive vector
* @returns
*/
public addInPlace(otherVector: Vector2): this {
this.x += otherVector.x;
this.y += otherVector.y;
return this;
}
/**
* Add the scalar to the x and y of this vector.
* @param s Additive scalar
* @returns
*/
public addScalar(s: number): this {
this.x += s;
this.y += s;
return this;
}
/**
*
* @param minVal Component will be limited to the minimum value of
* @param maxVal The component will be limited to the maximum value of
* @returns
*/
public clampScalar(minVal: number, maxVal: number) {
this.x = Math.max(minVal, Math.min(maxVal, this.x));
this.y = Math.max(minVal, Math.min(maxVal, this.y));
return this;
}
}