forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector4.js
More file actions
535 lines (479 loc) · 13.2 KB
/
Copy pathVector4.js
File metadata and controls
535 lines (479 loc) · 13.2 KB
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
// Adapted from [gl-matrix](https://github.com/toji/gl-matrix) by toji
// and [vecmath](https://github.com/mattdesl/vecmath) by mattdesl
var Class = require('../utils/Class');
/**
* @classdesc
* A representation of a vector in 4D space.
*
* A four-component vector.
*
* @class Vector4
* @memberof Phaser.Math
* @constructor
* @since 3.0.0
*
* @param {number} [x] - The x component.
* @param {number} [y] - The y component.
* @param {number} [z] - The z component.
* @param {number} [w] - The w component.
*/
var Vector4 = new Class({
initialize:
function Vector4 (x, y, z, w)
{
/**
* The x component of this Vector.
*
* @name Phaser.Math.Vector4#x
* @type {number}
* @default 0
* @since 3.0.0
*/
this.x = 0;
/**
* The y component of this Vector.
*
* @name Phaser.Math.Vector4#y
* @type {number}
* @default 0
* @since 3.0.0
*/
this.y = 0;
/**
* The z component of this Vector.
*
* @name Phaser.Math.Vector4#z
* @type {number}
* @default 0
* @since 3.0.0
*/
this.z = 0;
/**
* The w component of this Vector.
*
* @name Phaser.Math.Vector4#w
* @type {number}
* @default 0
* @since 3.0.0
*/
this.w = 0;
if (typeof x === 'object')
{
this.x = x.x || 0;
this.y = x.y || 0;
this.z = x.z || 0;
this.w = x.w || 0;
}
else
{
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
this.w = w || 0;
}
},
/**
* Make a clone of this Vector4.
*
* @method Phaser.Math.Vector4#clone
* @since 3.0.0
*
* @return {Phaser.Math.Vector4} A clone of this Vector4.
*/
clone: function ()
{
return new Vector4(this.x, this.y, this.z, this.w);
},
/**
* Copy the components of a given Vector into this Vector.
*
* @method Phaser.Math.Vector4#copy
* @since 3.0.0
*
* @param {Phaser.Math.Vector4} src - The Vector to copy the components from.
*
* @return {Phaser.Math.Vector4} This Vector4.
*/
copy: function (src)
{
this.x = src.x;
this.y = src.y;
this.z = src.z || 0;
this.w = src.w || 0;
return this;
},
/**
* Check whether this Vector is equal to a given Vector.
*
* Performs a strict quality check against each Vector's components.
*
* @method Phaser.Math.Vector4#equals
* @since 3.0.0
*
* @param {Phaser.Math.Vector4} v - The vector to check equality with.
*
* @return {boolean} A boolean indicating whether the two Vectors are equal or not.
*/
equals: function (v)
{
return ((this.x === v.x) && (this.y === v.y) && (this.z === v.z) && (this.w === v.w));
},
/**
* Set the `x`, `y`, `z` and `w` components of the this Vector to the given `x`, `y`, `z` and `w` values.
*
* @method Phaser.Math.Vector4#set
* @since 3.0.0
*
* @param {(number|object)} x - The x value to set for this Vector, or an object containing x, y, z and w components.
* @param {number} y - The y value to set for this Vector.
* @param {number} z - The z value to set for this Vector.
* @param {number} w - The z value to set for this Vector.
*
* @return {Phaser.Math.Vector4} This Vector4.
*/
set: function (x, y, z, w)
{
if (typeof x === 'object')
{
this.x = x.x || 0;
this.y = x.y || 0;
this.z = x.z || 0;
this.w = x.w || 0;
}
else
{
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
this.w = w || 0;
}
return this;
},
/**
* Add a given Vector to this Vector. Addition is component-wise.
*
* @method Phaser.Math.Vector4#add
* @since 3.0.0
*
* @param {(Phaser.Math.Vector2|Phaser.Math.Vector3|Phaser.Math.Vector4)} v - The Vector to add to this Vector.
*
* @return {Phaser.Math.Vector4} This Vector4.
*/
add: function (v)
{
this.x += v.x;
this.y += v.y;
this.z += v.z || 0;
this.w += v.w || 0;
return this;
},
/**
* Subtract the given Vector from this Vector. Subtraction is component-wise.
*
* @method Phaser.Math.Vector4#subtract
* @since 3.0.0
*
* @param {(Phaser.Math.Vector2|Phaser.Math.Vector3|Phaser.Math.Vector4)} v - The Vector to subtract from this Vector.
*
* @return {Phaser.Math.Vector4} This Vector4.
*/
subtract: function (v)
{
this.x -= v.x;
this.y -= v.y;
this.z -= v.z || 0;
this.w -= v.w || 0;
return this;
},
/**
* Scale this Vector by the given value.
*
* @method Phaser.Math.Vector4#scale
* @since 3.0.0
*
* @param {number} scale - The value to scale this Vector by.
*
* @return {Phaser.Math.Vector4} This Vector4.
*/
scale: function (scale)
{
this.x *= scale;
this.y *= scale;
this.z *= scale;
this.w *= scale;
return this;
},
/**
* Calculate the length (or magnitude) of this Vector.
*
* @method Phaser.Math.Vector4#length
* @since 3.0.0
*
* @return {number} The length of this Vector.
*/
length: function ()
{
var x = this.x;
var y = this.y;
var z = this.z;
var w = this.w;
return Math.sqrt(x * x + y * y + z * z + w * w);
},
/**
* Calculate the length of this Vector squared.
*
* @method Phaser.Math.Vector4#lengthSq
* @since 3.0.0
*
* @return {number} The length of this Vector, squared.
*/
lengthSq: function ()
{
var x = this.x;
var y = this.y;
var z = this.z;
var w = this.w;
return x * x + y * y + z * z + w * w;
},
/**
* Normalize this Vector.
*
* Makes the vector a unit length vector (magnitude of 1) in the same direction.
*
* @method Phaser.Math.Vector4#normalize
* @since 3.0.0
*
* @return {Phaser.Math.Vector4} This Vector4.
*/
normalize: function ()
{
var x = this.x;
var y = this.y;
var z = this.z;
var w = this.w;
var len = x * x + y * y + z * z + w * w;
if (len > 0)
{
len = 1 / Math.sqrt(len);
this.x = x * len;
this.y = y * len;
this.z = z * len;
this.w = w * len;
}
return this;
},
/**
* Calculate the dot product of this Vector and the given Vector.
*
* @method Phaser.Math.Vector4#dot
* @since 3.0.0
*
* @param {Phaser.Math.Vector4} v - The Vector4 to dot product with this Vector4.
*
* @return {number} The dot product of this Vector and the given Vector.
*/
dot: function (v)
{
return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
},
/**
* Linearly interpolate between this Vector and the given Vector.
*
* Interpolates this Vector towards the given Vector.
*
* @method Phaser.Math.Vector4#lerp
* @since 3.0.0
*
* @param {Phaser.Math.Vector4} v - The Vector4 to interpolate towards.
* @param {number} [t=0] - The interpolation percentage, between 0 and 1.
*
* @return {Phaser.Math.Vector4} This Vector4.
*/
lerp: function (v, t)
{
if (t === undefined) { t = 0; }
var ax = this.x;
var ay = this.y;
var az = this.z;
var aw = this.w;
this.x = ax + t * (v.x - ax);
this.y = ay + t * (v.y - ay);
this.z = az + t * (v.z - az);
this.w = aw + t * (v.w - aw);
return this;
},
/**
* Perform a component-wise multiplication between this Vector and the given Vector.
*
* Multiplies this Vector by the given Vector.
*
* @method Phaser.Math.Vector4#multiply
* @since 3.0.0
*
* @param {(Phaser.Math.Vector2|Phaser.Math.Vector3|Phaser.Math.Vector4)} v - The Vector to multiply this Vector by.
*
* @return {Phaser.Math.Vector4} This Vector4.
*/
multiply: function (v)
{
this.x *= v.x;
this.y *= v.y;
this.z *= v.z || 1;
this.w *= v.w || 1;
return this;
},
/**
* Perform a component-wise division between this Vector and the given Vector.
*
* Divides this Vector by the given Vector.
*
* @method Phaser.Math.Vector4#divide
* @since 3.0.0
*
* @param {(Phaser.Math.Vector2|Phaser.Math.Vector3|Phaser.Math.Vector4)} v - The Vector to divide this Vector by.
*
* @return {Phaser.Math.Vector4} This Vector4.
*/
divide: function (v)
{
this.x /= v.x;
this.y /= v.y;
this.z /= v.z || 1;
this.w /= v.w || 1;
return this;
},
/**
* Calculate the distance between this Vector and the given Vector.
*
* @method Phaser.Math.Vector4#distance
* @since 3.0.0
*
* @param {(Phaser.Math.Vector2|Phaser.Math.Vector3|Phaser.Math.Vector4)} v - The Vector to calculate the distance to.
*
* @return {number} The distance from this Vector to the given Vector.
*/
distance: function (v)
{
var dx = v.x - this.x;
var dy = v.y - this.y;
var dz = v.z - this.z || 0;
var dw = v.w - this.w || 0;
return Math.sqrt(dx * dx + dy * dy + dz * dz + dw * dw);
},
/**
* Calculate the distance between this Vector and the given Vector, squared.
*
* @method Phaser.Math.Vector4#distanceSq
* @since 3.0.0
*
* @param {(Phaser.Math.Vector2|Phaser.Math.Vector3|Phaser.Math.Vector4)} v - The Vector to calculate the distance to.
*
* @return {number} The distance from this Vector to the given Vector, squared.
*/
distanceSq: function (v)
{
var dx = v.x - this.x;
var dy = v.y - this.y;
var dz = v.z - this.z || 0;
var dw = v.w - this.w || 0;
return dx * dx + dy * dy + dz * dz + dw * dw;
},
/**
* Negate the `x`, `y`, `z` and `w` components of this Vector.
*
* @method Phaser.Math.Vector4#negate
* @since 3.0.0
*
* @return {Phaser.Math.Vector4} This Vector4.
*/
negate: function ()
{
this.x = -this.x;
this.y = -this.y;
this.z = -this.z;
this.w = -this.w;
return this;
},
/**
* Transform this Vector with the given Matrix.
*
* @method Phaser.Math.Vector4#transformMat4
* @since 3.0.0
*
* @param {Phaser.Math.Matrix4} mat - The Matrix4 to transform this Vector4 with.
*
* @return {Phaser.Math.Vector4} This Vector4.
*/
transformMat4: function (mat)
{
var x = this.x;
var y = this.y;
var z = this.z;
var w = this.w;
var m = mat.val;
this.x = m[0] * x + m[4] * y + m[8] * z + m[12] * w;
this.y = m[1] * x + m[5] * y + m[9] * z + m[13] * w;
this.z = m[2] * x + m[6] * y + m[10] * z + m[14] * w;
this.w = m[3] * x + m[7] * y + m[11] * z + m[15] * w;
return this;
},
/**
* Transform this Vector with the given Quaternion.
*
* @method Phaser.Math.Vector4#transformQuat
* @since 3.0.0
*
* @param {Phaser.Math.Quaternion} q - The Quaternion to transform this Vector with.
*
* @return {Phaser.Math.Vector4} This Vector4.
*/
transformQuat: function (q)
{
var x = this.x;
var y = this.y;
var z = this.z;
var qx = q.x;
var qy = q.y;
var qz = q.z;
var qw = q.w;
// calculate quat * vec
var ix = qw * x + qy * z - qz * y;
var iy = qw * y + qz * x - qx * z;
var iz = qw * z + qx * y - qy * x;
var iw = -qx * x - qy * y - qz * z;
// calculate result * inverse quat
this.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
this.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
this.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
return this;
},
/**
* Make this Vector the zero vector (0, 0, 0, 0).
*
* @method Phaser.Math.Vector4#reset
* @since 3.0.0
*
* @return {Phaser.Math.Vector4} This Vector4.
*/
reset: function ()
{
this.x = 0;
this.y = 0;
this.z = 0;
this.w = 0;
return this;
}
});
Vector4.prototype.sub = Vector4.prototype.subtract;
Vector4.prototype.mul = Vector4.prototype.multiply;
Vector4.prototype.div = Vector4.prototype.divide;
Vector4.prototype.dist = Vector4.prototype.distance;
Vector4.prototype.distSq = Vector4.prototype.distanceSq;
Vector4.prototype.len = Vector4.prototype.length;
Vector4.prototype.lenSq = Vector4.prototype.lengthSq;
module.exports = Vector4;