@makefully/platypus 4.1.4 → 4.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/README.md +4 -2
- package/lib/platypus.js +1 -1
- package/package.json +7 -2
- package/src/AABB.js +685 -0
- package/src/ActionState.js +121 -0
- package/src/AssetManager.js +206 -0
- package/src/CollisionData.js +96 -0
- package/src/CollisionDataContainer.js +127 -0
- package/src/CollisionShape.js +315 -0
- package/src/Component.js +231 -0
- package/src/Data.js +64 -0
- package/src/DataMap.js +204 -0
- package/src/Entity.js +404 -0
- package/src/EntityLinker.js +113 -0
- package/src/EventHandler.js +115 -0
- package/src/EventHandlerList.js +251 -0
- package/src/Game.js +840 -0
- package/src/Gamepad.js +175 -0
- package/src/ID3CaptionPlayer.js +61 -0
- package/src/Messenger.js +365 -0
- package/src/PIXIAnimation.js +622 -0
- package/src/RandomSet.js +140 -0
- package/src/SFXPlayer.js +120 -0
- package/src/StateMap.js +252 -0
- package/src/Storage.js +152 -0
- package/src/TickerClient.js +48 -0
- package/src/TickerServer.js +23 -0
- package/src/TimeEvent.js +50 -0
- package/src/TimeEventList.js +100 -0
- package/src/VOPlayer.js +684 -0
- package/src/Vector.js +702 -0
- package/src/components/AIChaser.js +161 -0
- package/src/components/AIPacer.js +107 -0
- package/src/components/AudioMusic.js +151 -0
- package/src/components/AudioSFX.js +673 -0
- package/src/components/AudioVO.js +301 -0
- package/src/components/CollisionBasic.js +826 -0
- package/src/components/CollisionFilter.js +74 -0
- package/src/components/CollisionGroup.js +448 -0
- package/src/components/CollisionTiles.js +608 -0
- package/src/components/ComponentSwitcher.js +173 -0
- package/src/components/ControllerInput.js +65 -0
- package/src/components/Counter.js +165 -0
- package/src/components/EntityCamera.js +215 -0
- package/src/components/EntityContainer.js +549 -0
- package/src/components/EntityController.js +541 -0
- package/src/components/HandlerCamera.js +1052 -0
- package/src/components/HandlerCollision.js +1318 -0
- package/src/components/HandlerController.js +291 -0
- package/src/components/HandlerLogic.js +394 -0
- package/src/components/HandlerRender.js +217 -0
- package/src/components/Interactive.js +502 -0
- package/src/components/LevelBuilder.js +524 -0
- package/src/components/LogicAngularMovement.js +154 -0
- package/src/components/LogicAttachment.js +265 -0
- package/src/components/LogicButton.js +466 -0
- package/src/components/LogicCarrier.js +39 -0
- package/src/components/LogicDestroyMe.js +58 -0
- package/src/components/LogicDirectionalMovement.js +325 -0
- package/src/components/LogicDragDrop.js +371 -0
- package/src/components/LogicImpactLaunch.js +144 -0
- package/src/components/LogicPacingPlatform.js +64 -0
- package/src/components/LogicPortable.js +112 -0
- package/src/components/LogicPortal.js +115 -0
- package/src/components/LogicPushable.js +98 -0
- package/src/components/LogicRebounder.js +136 -0
- package/src/components/LogicRegionSpawner.js +139 -0
- package/src/components/LogicRotationalMovement.js +205 -0
- package/src/components/LogicSpawner.js +159 -0
- package/src/components/LogicStateMachine.js +382 -0
- package/src/components/LogicSwitch.js +77 -0
- package/src/components/LogicTeleportee.js +68 -0
- package/src/components/LogicTeleporter.js +93 -0
- package/src/components/LogicTimer.js +178 -0
- package/src/components/LogicWindUpRacer.js +179 -0
- package/src/components/Motion.js +371 -0
- package/src/components/Mover.js +566 -0
- package/src/components/Node.js +289 -0
- package/src/components/NodeMap.js +295 -0
- package/src/components/NodeResident.js +636 -0
- package/src/components/Orientation.js +697 -0
- package/src/components/RandomEvents.js +73 -0
- package/src/components/RelativePosition.js +204 -0
- package/src/components/RelayFamily.js +106 -0
- package/src/components/RelayGame.js +53 -0
- package/src/components/RelayLinker.js +94 -0
- package/src/components/RelayParent.js +57 -0
- package/src/components/RelaySelf.js +55 -0
- package/src/components/Render.js +178 -0
- package/src/components/RenderAnimator.js +275 -0
- package/src/components/RenderContainer.js +646 -0
- package/src/components/RenderDebug.js +319 -0
- package/src/components/RenderDestroyMe.js +59 -0
- package/src/components/RenderLottie.js +149 -0
- package/src/components/RenderProgress.js +120 -0
- package/src/components/RenderSpine.js +833 -0
- package/src/components/RenderSprite.js +527 -0
- package/src/components/RenderText.js +147 -0
- package/src/components/RenderTiles.js +1391 -0
- package/src/components/SceneChanger.js +128 -0
- package/src/components/TiledLoader.js +1451 -0
- package/src/components/Timeline.js +314 -0
- package/src/components/Tutorial.js +267 -0
- package/src/components/Tween.js +296 -0
- package/src/components/VoiceOver.js +373 -0
- package/src/components/XHR.js +129 -0
- package/src/components/index.js +79 -0
- package/src/factory.js +305 -0
- package/src/index.js +159 -0
- package/src/mixins/PIXI.Graphics.js +41 -0
- package/src/platypus.css +68 -0
- package/src/sayHello.js +50 -0
- package/src/shapes/Capsule.js +74 -0
- package/src/shapes/Circle.js +43 -0
- package/src/shapes/Ellipse.js +117 -0
- package/src/shapes/Point.js +191 -0
- package/src/shapes/Polygon.js +112 -0
- package/src/shapes/Polyline.js +97 -0
- package/src/shapes/Rectangle.js +78 -0
- package/src/shapes/RoundedRectangle.js +33 -0
- package/src/shapes/Segment.js +346 -0
- package/src/shapes/adjustedXY.js +19 -0
- package/src/shapes/castFromBox2D.js +92 -0
- package/src/shapes/castFromObject.js +53 -0
- package/src/shapes/castFromPixiGraphics.js +6 -0
- package/src/shapes/castFromPixiShape.js +89 -0
- package/src/shapes/castToBox2D.js +190 -0
- package/src/shapes/castToPixiGraphics.js +100 -0
- package/src/shapes/castToPixiShape.js +61 -0
- package/src/shapes/checkPointsMap.js +313 -0
- package/src/shapes/index.js +35 -0
- package/src/utils/array.js +73 -0
- package/src/utils/string.js +39 -0
package/src/Vector.js
ADDED
|
@@ -0,0 +1,702 @@
|
|
|
1
|
+
/* global platypus */
|
|
2
|
+
import {arrayCache, greenSlice} from './utils/array.js';
|
|
3
|
+
import recycle from 'recycle';
|
|
4
|
+
|
|
5
|
+
export default (function () {
|
|
6
|
+
/**
|
|
7
|
+
* This class defines a multi-dimensional vector object and a variety of methods for manipulating the vector.
|
|
8
|
+
*
|
|
9
|
+
* @memberof platypus
|
|
10
|
+
* @class Vector
|
|
11
|
+
* @param {number|Array|Vector} x The x coordinate or an array or Vector describing the whole vector.
|
|
12
|
+
* @param {number} [y] The y coordinate.
|
|
13
|
+
* @param {number} [z] The z coordinate.
|
|
14
|
+
* @property {number} x The x coordinate.
|
|
15
|
+
* @property {number} [y] The y coordinate.
|
|
16
|
+
* @property {number} [z] The z coordinate.
|
|
17
|
+
*/
|
|
18
|
+
const
|
|
19
|
+
Vector = function (x, y, z) {
|
|
20
|
+
if (this.matrix) { // Recycled vectors will already have a matrix array. Resetting x, y, z to 0's to properly handle a set-up array of less than 3 dimensions.
|
|
21
|
+
this.matrix[0] = 0;
|
|
22
|
+
this.matrix[1] = 0;
|
|
23
|
+
this.matrix[2] = 0;
|
|
24
|
+
} else {
|
|
25
|
+
this.matrix = arrayCache.setUp(0, 0, 0);
|
|
26
|
+
}
|
|
27
|
+
this.set(x, y, z);
|
|
28
|
+
},
|
|
29
|
+
proto = Vector.prototype;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The x component of the vector.
|
|
33
|
+
*
|
|
34
|
+
* @memberof platypus.Vector.prototype
|
|
35
|
+
* @member x
|
|
36
|
+
* @type number
|
|
37
|
+
* @default 0
|
|
38
|
+
*/
|
|
39
|
+
Object.defineProperty(proto, 'x', {
|
|
40
|
+
get: function () {
|
|
41
|
+
return this.matrix[0];
|
|
42
|
+
},
|
|
43
|
+
set: function (value) {
|
|
44
|
+
this.matrix[0] = value;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The y component of the vector.
|
|
50
|
+
*
|
|
51
|
+
* @memberof platypus.Vector.prototype
|
|
52
|
+
* @member y
|
|
53
|
+
* @type number
|
|
54
|
+
* @default 0
|
|
55
|
+
*/
|
|
56
|
+
Object.defineProperty(proto, 'y', {
|
|
57
|
+
get: function () {
|
|
58
|
+
return this.matrix[1];
|
|
59
|
+
},
|
|
60
|
+
set: function (value) {
|
|
61
|
+
this.matrix[1] = value;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* The z component of the vector.
|
|
67
|
+
*
|
|
68
|
+
* @memberof platypus.Vector.prototype
|
|
69
|
+
* @member z
|
|
70
|
+
* @type number
|
|
71
|
+
* @default 0
|
|
72
|
+
*/
|
|
73
|
+
Object.defineProperty(proto, 'z', {
|
|
74
|
+
get: function () {
|
|
75
|
+
return this.matrix[2];
|
|
76
|
+
},
|
|
77
|
+
set: function (value) {
|
|
78
|
+
this.matrix[2] = value;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Returns a string describing the vector in the format of "[x, y, z]".
|
|
84
|
+
*
|
|
85
|
+
* @method platypus.Vector#toString
|
|
86
|
+
* @return {String}
|
|
87
|
+
*/
|
|
88
|
+
proto.toString = function () {
|
|
89
|
+
return '[' + this.matrix.join(',') + ']';
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Sets the coordinates of the vector.
|
|
94
|
+
*
|
|
95
|
+
* @method platypus.Vector#set
|
|
96
|
+
* @param x {number|Array|Vector} The x coordinate or an array or Vector describing the whole vector.
|
|
97
|
+
* @param [y] {number} The y coordinate, or if x is an array/Vector this is the number of elements to copy from the array/Vector.
|
|
98
|
+
* @param [z] {number} The z coordinate.
|
|
99
|
+
* @chainable
|
|
100
|
+
*/
|
|
101
|
+
proto.set = function (x, y, z) {
|
|
102
|
+
if (x && x.matrix) { // Passing in a vector.
|
|
103
|
+
return this.setVector(x, y);
|
|
104
|
+
} else if (x && (typeof x.x === 'number') && (typeof x.y === 'number')) { // Passing in a vector-like object.
|
|
105
|
+
return this.setXYZ(x.x, x.y, x.z);
|
|
106
|
+
} else if (x && Array.isArray(x)) { // Passing in an array.
|
|
107
|
+
return this.setArray(x, y);
|
|
108
|
+
} else { // Passing in coordinates.
|
|
109
|
+
return this.setXYZ(x, y, z);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Sets the coordinates of the vector.
|
|
115
|
+
*
|
|
116
|
+
* @method platypus.Vector#setXYZ
|
|
117
|
+
* @param x {number} The x coordinate.
|
|
118
|
+
* @param [y] {number} The y coordinate.
|
|
119
|
+
* @param [z] {number} The z coordinate.
|
|
120
|
+
* @chainable
|
|
121
|
+
*/
|
|
122
|
+
proto.setXYZ = function (x, y, z) {
|
|
123
|
+
const
|
|
124
|
+
{matrix} = this;
|
|
125
|
+
|
|
126
|
+
matrix[0] = x || 0;
|
|
127
|
+
matrix[1] = y || 0;
|
|
128
|
+
matrix[2] = z || 0;
|
|
129
|
+
|
|
130
|
+
return this;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Sets the coordinates of the vector.
|
|
135
|
+
*
|
|
136
|
+
* @method platypus.Vector#setVector
|
|
137
|
+
* @param vector {Vector} The Vector to copy.
|
|
138
|
+
* @param [dimensions] {number} The number of elements to copy from the Vector.
|
|
139
|
+
* @chainable
|
|
140
|
+
*/
|
|
141
|
+
proto.setVector = function (vector, dimensions) {
|
|
142
|
+
return this.setArray(vector.matrix, dimensions);
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Sets the coordinates of the vector.
|
|
147
|
+
*
|
|
148
|
+
* @method platypus.Vector#setArray
|
|
149
|
+
* @param arr {Array} The array to copy.
|
|
150
|
+
* @param [dimensions] {number} The number of elements to copy from the Array.
|
|
151
|
+
* @chainable
|
|
152
|
+
*/
|
|
153
|
+
proto.setArray = function (arr, dimensions) {
|
|
154
|
+
const
|
|
155
|
+
{matrix} = this;
|
|
156
|
+
let q = dimensions || arr.length;
|
|
157
|
+
|
|
158
|
+
while (q--) {
|
|
159
|
+
matrix[q] = arr[q];
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return this;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Determines whether two vectors are equal.
|
|
167
|
+
*
|
|
168
|
+
* @method platypus.Vector#equals
|
|
169
|
+
* @param x {number|Array|Vector} The x coordinate or an array or Vector to check against.
|
|
170
|
+
* @param [y] {number} The y coordinate, or if x is an array/Vector this is the number of dimensions to check from the array/Vector.
|
|
171
|
+
* @param [z] {number} The z coordinate.
|
|
172
|
+
* @return {Boolean} Whether the vectors are equal.
|
|
173
|
+
*/
|
|
174
|
+
proto.equals = function (x, y, z) {
|
|
175
|
+
const
|
|
176
|
+
{matrix} = this;
|
|
177
|
+
|
|
178
|
+
if (x && Array.isArray(x)) { // Passing in an array.
|
|
179
|
+
let q = y || x.length;
|
|
180
|
+
|
|
181
|
+
while (q--) {
|
|
182
|
+
if (matrix[q] !== x[q]) {
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return true;
|
|
187
|
+
} else if (x && x.matrix) { // Passing in a vector.
|
|
188
|
+
const
|
|
189
|
+
m = x.matrix;
|
|
190
|
+
let q = y || m.length;
|
|
191
|
+
|
|
192
|
+
while (q--) {
|
|
193
|
+
if (matrix[q] !== m[q]) {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return true;
|
|
198
|
+
} else { // Passing in coordinates.
|
|
199
|
+
return ((typeof x === 'number') && (matrix[0] === x)) && ((typeof y !== 'number') || (matrix[1] === y)) && ((typeof z !== 'number') || (matrix[2] === z));
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Returns the magnitude of the vector.
|
|
205
|
+
*
|
|
206
|
+
* @method platypus.Vector#magnitude
|
|
207
|
+
* @param [dimensions] {number} The dimensions to include. Defaults to all dimensions.
|
|
208
|
+
* @return {number} The magnitude of the vector.
|
|
209
|
+
*/
|
|
210
|
+
proto.magnitude = function (dimensions) {
|
|
211
|
+
return Math.sqrt(this.magnitudeSquared(dimensions));
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Returns the magnitude squared of the vector. This is slightly faster than finding the magnitude.
|
|
216
|
+
*
|
|
217
|
+
* @method platypus.Vector#magnitudeSquared
|
|
218
|
+
* @param [dimensions] {number} The dimensions to include. Defaults to all dimensions.
|
|
219
|
+
* @return {number} The magnitude squared of the vector.
|
|
220
|
+
*/
|
|
221
|
+
proto.magnitudeSquared = function (dimensions) {
|
|
222
|
+
const
|
|
223
|
+
{matrix} = this;
|
|
224
|
+
let squares = 0;
|
|
225
|
+
|
|
226
|
+
dimensions = dimensions || matrix.length;
|
|
227
|
+
|
|
228
|
+
for (let x = 0; x < dimensions; x++) {
|
|
229
|
+
squares += matrix[x] ** 2;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return squares;
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Returns the direction of the vector from the z-axis
|
|
237
|
+
*
|
|
238
|
+
* @method platypus.Vector#getAngle
|
|
239
|
+
* @return {number} The direction of the vector in radians.
|
|
240
|
+
*/
|
|
241
|
+
proto.getAngle = function () {
|
|
242
|
+
const
|
|
243
|
+
mag = this.magnitude(2);
|
|
244
|
+
let angle = 0;
|
|
245
|
+
|
|
246
|
+
if (mag !== 0) {
|
|
247
|
+
angle = Math.acos(this.x / mag);
|
|
248
|
+
if (this.y < 0) {
|
|
249
|
+
angle = (Math.PI * 2) - angle;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return angle;
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Returns a normalized copy of the vector.
|
|
257
|
+
*
|
|
258
|
+
* @method platypus.Vector#getUnit
|
|
259
|
+
* @return {platypus.Vector} A normalized vector in the same direction as this vector.
|
|
260
|
+
*/
|
|
261
|
+
proto.getUnit = function () {
|
|
262
|
+
return Vector.setUp(this).normalize();
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Returns a copy of the Vector inverted.
|
|
267
|
+
*
|
|
268
|
+
* @method platypus.Vector#getInverse
|
|
269
|
+
* @return {platypus.Vector}
|
|
270
|
+
*/
|
|
271
|
+
proto.getInverse = function () {
|
|
272
|
+
return Vector.setUp(this).multiply(-1);
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Normalizes the vector.
|
|
277
|
+
*
|
|
278
|
+
* @method platypus.Vector#normalize
|
|
279
|
+
* @chainable
|
|
280
|
+
*/
|
|
281
|
+
proto.normalize = function () {
|
|
282
|
+
const
|
|
283
|
+
mag = this.magnitude();
|
|
284
|
+
|
|
285
|
+
if (mag === 0) {
|
|
286
|
+
// Ignores attempt to normalize a vector of zero magnitude.
|
|
287
|
+
return this;
|
|
288
|
+
} else {
|
|
289
|
+
return this.multiply(1 / mag);
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Crosses this vector with the parameter vector.
|
|
295
|
+
*
|
|
296
|
+
* @method platypus.Vector#cross
|
|
297
|
+
* @param vector {platypus.Vector} The vector to cross this vector with.
|
|
298
|
+
* @chainable
|
|
299
|
+
*/
|
|
300
|
+
proto.cross = (function () {
|
|
301
|
+
const
|
|
302
|
+
det = function (a, b, c, d) {
|
|
303
|
+
return a * d - b * c;
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
return function ({x: vx, y: vy, z: vz}) {
|
|
307
|
+
const
|
|
308
|
+
{x, y, z} = this;
|
|
309
|
+
|
|
310
|
+
this.x = det(y, z, vy, vz),
|
|
311
|
+
this.y = -det(x, z, vx, vz),
|
|
312
|
+
this.z = det(x, y, vx, vy);
|
|
313
|
+
|
|
314
|
+
return this;
|
|
315
|
+
};
|
|
316
|
+
}());
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Crosses this vector with the parameter vector and returns the cross product.
|
|
320
|
+
*
|
|
321
|
+
* @method platypus.Vector#getCrossProduct
|
|
322
|
+
* @param vector {platypus.Vector} The vector to cross this vector with.
|
|
323
|
+
* @return {platypus.Vector} The cross product.
|
|
324
|
+
*/
|
|
325
|
+
proto.getCrossProduct = function (v) {
|
|
326
|
+
return Vector.setUp(this).cross(v);
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
const
|
|
330
|
+
UNIT_X_AXIS = new Vector(1, 0, 0),
|
|
331
|
+
UNIT_Y_AXIS = new Vector(0, 1, 0),
|
|
332
|
+
UNIT_Z_AXIS = new Vector(0, 0, 1);
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Rotates the vector by the given amount.
|
|
336
|
+
*
|
|
337
|
+
* @method platypus.Vector#rotate
|
|
338
|
+
* @param angle {number} The amount to rotate the vector in radians.
|
|
339
|
+
* @param [axis="z"] {String|Vector} A vector describing the axis around which the rotation should occur or 'x', 'y', or 'z'.
|
|
340
|
+
* @chainable
|
|
341
|
+
*/
|
|
342
|
+
proto.rotate = function (angle, axis = UNIT_Z_AXIS) {
|
|
343
|
+
const
|
|
344
|
+
{x, y, z} = axis === 'x' ? UNIT_X_AXIS : axis === 'y' ? UNIT_Y_AXIS : axis,
|
|
345
|
+
cos = Math.cos(angle),
|
|
346
|
+
sin = Math.sin(angle),
|
|
347
|
+
icos = 1 - cos,
|
|
348
|
+
arr = arrayCache.setUp(
|
|
349
|
+
arrayCache.setUp( cos + x * x * icos, x * y * icos - z * sin, x * z * icos + y * sin),
|
|
350
|
+
arrayCache.setUp(y * x * icos + z * sin, cos + y * y * icos, y * z * icos - x * sin),
|
|
351
|
+
arrayCache.setUp(z * x * icos - y * sin, z * y * icos + x * sin, cos + z * z * icos)
|
|
352
|
+
);
|
|
353
|
+
|
|
354
|
+
this.multiply(arr);
|
|
355
|
+
arrayCache.recycle(arr, 2);
|
|
356
|
+
|
|
357
|
+
return this;
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Rotates the vector position around a given point on the cartesian plane.
|
|
362
|
+
*
|
|
363
|
+
* @method platypus.Vector#rotateAbout
|
|
364
|
+
* @param point {Vector} A vector describing the point around which the rotation should occur.
|
|
365
|
+
* @param angle {number} The amount to rotate the vector in radians.
|
|
366
|
+
* @chainable
|
|
367
|
+
*/
|
|
368
|
+
proto.rotateAbout = function (point, angle) {
|
|
369
|
+
const cos = Math.cos(angle),
|
|
370
|
+
sin = Math.sin(angle),
|
|
371
|
+
dx = this.x - point.x,
|
|
372
|
+
dy = this.y - point.y;
|
|
373
|
+
|
|
374
|
+
this.x = point.x + (dx * cos - dy * sin);
|
|
375
|
+
this.y = point.y + (dx * sin + dy * cos);
|
|
376
|
+
|
|
377
|
+
return this;
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Scales the vector by the given factor or performs a transform if a matrix is provided.
|
|
382
|
+
*
|
|
383
|
+
* @method platypus.Vector#multiply
|
|
384
|
+
* @param multiplier {number|Array} The factor to scale by or a 2D array describing a multiplication matrix.
|
|
385
|
+
* @param limit {number} For scaling, determines which coordinates are affected.
|
|
386
|
+
* @chainable
|
|
387
|
+
*/
|
|
388
|
+
proto.multiply = function (multiplier, limit) {
|
|
389
|
+
const
|
|
390
|
+
matrix = this.matrix;
|
|
391
|
+
|
|
392
|
+
if (Array.isArray(multiplier)) {
|
|
393
|
+
const
|
|
394
|
+
arr = greenSlice(matrix);
|
|
395
|
+
|
|
396
|
+
if (multiplier.length === 2) {
|
|
397
|
+
matrix[0] = arr[0] * multiplier[0][0] + arr[1] * multiplier[0][1];
|
|
398
|
+
matrix[1] = arr[0] * multiplier[1][0] + arr[1] * multiplier[1][1];
|
|
399
|
+
} else if (multiplier.length >= 3) {
|
|
400
|
+
matrix[0] = arr[0] * multiplier[0][0] + arr[1] * multiplier[0][1] + arr[2] * multiplier[0][2];
|
|
401
|
+
matrix[1] = arr[0] * multiplier[1][0] + arr[1] * multiplier[1][1] + arr[2] * multiplier[1][2];
|
|
402
|
+
matrix[2] = arr[0] * multiplier[2][0] + arr[1] * multiplier[2][1] + arr[2] * multiplier[2][2];
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
arrayCache.recycle(arr);
|
|
406
|
+
} else {
|
|
407
|
+
const
|
|
408
|
+
l = limit || matrix.length;
|
|
409
|
+
|
|
410
|
+
for (let i = 0; i < l; i++) {
|
|
411
|
+
matrix[i] *= multiplier;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
return this;
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Adds the given components to this vector.
|
|
420
|
+
*
|
|
421
|
+
* @method platypus.Vector#add
|
|
422
|
+
* @param x {number|Array|Vector} The x component to add, or an array or vector describing the whole addition.
|
|
423
|
+
* @param [y] {number} The y component to add or the limit if the first parameter is a vector or array.
|
|
424
|
+
* @param [z] {number} The z component to add.
|
|
425
|
+
* @chainable
|
|
426
|
+
*/
|
|
427
|
+
proto.add = function (x, y, z) {
|
|
428
|
+
let addMatrix = x,
|
|
429
|
+
limit = 0;
|
|
430
|
+
|
|
431
|
+
if (!Array.isArray(addMatrix)) {
|
|
432
|
+
if (addMatrix instanceof Vector) {
|
|
433
|
+
addMatrix = addMatrix.matrix;
|
|
434
|
+
limit = y || this.matrix.length;
|
|
435
|
+
} else {
|
|
436
|
+
addMatrix = [x || 0, y || 0, z || 0];
|
|
437
|
+
limit = this.matrix.length;
|
|
438
|
+
}
|
|
439
|
+
} else {
|
|
440
|
+
limit = y || this.matrix.length;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
for (let q = 0; q < limit; q++) {
|
|
444
|
+
this.matrix[q] += addMatrix[q];
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
return this;
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* Adds the given vector to this vector.
|
|
452
|
+
*
|
|
453
|
+
* @method platypus.Vector#addVector
|
|
454
|
+
* @param otherVector {platypus.Vector} The vector to add.
|
|
455
|
+
* @chainable
|
|
456
|
+
*/
|
|
457
|
+
proto.addVector = function (otherVector, dimensions) {
|
|
458
|
+
return this.add(otherVector, dimensions);
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Subtracts the given vector from this vector.
|
|
463
|
+
*
|
|
464
|
+
* @method platypus.Vector#subtractVector
|
|
465
|
+
* @param otherVector {platypus.Vector} The vector to subtract.
|
|
466
|
+
* @chainable
|
|
467
|
+
*/
|
|
468
|
+
proto.subtractVector = function (otherVector, dimensions) {
|
|
469
|
+
const
|
|
470
|
+
inv = otherVector.getInverse();
|
|
471
|
+
|
|
472
|
+
this.add(inv, dimensions);
|
|
473
|
+
inv.recycle();
|
|
474
|
+
|
|
475
|
+
return this;
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Returns the perpendicular vector.
|
|
480
|
+
*
|
|
481
|
+
* @method platypus.Vector#perpendicular
|
|
482
|
+
* @param opposite {Boolean} Whether to negate the perpendicular vector.
|
|
483
|
+
* @chainable
|
|
484
|
+
*/
|
|
485
|
+
proto.perpendicular = function (negate) {
|
|
486
|
+
const matrix = this.matrix,
|
|
487
|
+
mult = (negate === true) ? -1 : 1,
|
|
488
|
+
x = -this.matrix[1];
|
|
489
|
+
|
|
490
|
+
matrix[1] = matrix[0];
|
|
491
|
+
matrix[0] = x;
|
|
492
|
+
|
|
493
|
+
if (negate) {
|
|
494
|
+
matrix[1] *= mult;
|
|
495
|
+
matrix[0] *= mult;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
return this;
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
/**
|
|
502
|
+
* Scales the vector by the given factor.
|
|
503
|
+
*
|
|
504
|
+
* @method platypus.Vector#multiply
|
|
505
|
+
* @param factor {number} The factor to scale by.
|
|
506
|
+
* @param limit {number} Determines which coordinates are affected. Defaults to all coordinates.
|
|
507
|
+
* @chainable
|
|
508
|
+
*/
|
|
509
|
+
proto.scale = function (factor, limit) {
|
|
510
|
+
return this.multiply(factor, limit);
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Finds the dot product of the two vectors.
|
|
515
|
+
*
|
|
516
|
+
* @method platypus.Vector#dot
|
|
517
|
+
* @param otherVector {platypus.Vector} The other vector.
|
|
518
|
+
* @param limit {number} The number of vector indexes to include in the dot product.
|
|
519
|
+
* @return {number} The dot product.
|
|
520
|
+
*/
|
|
521
|
+
proto.dot = function ({matrix: oM}, limit) {
|
|
522
|
+
const
|
|
523
|
+
{matrix: m} = this;
|
|
524
|
+
let sum = 0,
|
|
525
|
+
q = limit || m.length;
|
|
526
|
+
|
|
527
|
+
while (q--) {
|
|
528
|
+
sum += m[q] * (oM[q] || 0);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
return sum;
|
|
532
|
+
};
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* Finds the shortest angle between the two vectors.
|
|
536
|
+
*
|
|
537
|
+
* @method platypus.Vector#angleTo
|
|
538
|
+
* @param otherVector {platypus.Vector} The other vector.
|
|
539
|
+
* @return {number} The angle between this vector and the received vector.
|
|
540
|
+
*/
|
|
541
|
+
proto.angleTo = function (otherVector) {
|
|
542
|
+
const
|
|
543
|
+
v1 = this.getUnit(),
|
|
544
|
+
v2 = otherVector.getUnit();
|
|
545
|
+
let ang = 0;
|
|
546
|
+
|
|
547
|
+
if (v1.magnitude() && v2.magnitude()) { // Probably want a less expensive check here for zero-length vectors.
|
|
548
|
+
ang = Math.acos(v1.dot(v2));
|
|
549
|
+
} else {
|
|
550
|
+
platypus.debug.warn('Vector: Attempted to find the angle of a zero-length vector.');
|
|
551
|
+
ang = NaN;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
v1.recycle();
|
|
555
|
+
v2.recycle();
|
|
556
|
+
|
|
557
|
+
return ang;
|
|
558
|
+
};
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* Finds the shortest signed angle between the two vectors.
|
|
562
|
+
*
|
|
563
|
+
* @method platypus.Vector#signedAngleTo
|
|
564
|
+
* @param otherVector {platypus.Vector} The other vector.
|
|
565
|
+
* @param normal {platypus.Vector} A normal vector determining the resultant sign of the angle between two vectors.
|
|
566
|
+
* @return {number} The angle between this vector and the received vector.
|
|
567
|
+
*/
|
|
568
|
+
proto.signedAngleTo = function (otherVector, normal) {
|
|
569
|
+
const
|
|
570
|
+
v1 = this.getUnit(),
|
|
571
|
+
v2 = otherVector.getUnit(),
|
|
572
|
+
v3 = v1.getCrossProduct(v2);
|
|
573
|
+
let ang = 0;
|
|
574
|
+
|
|
575
|
+
if (v3.magnitude() === 0) {
|
|
576
|
+
if (v1.x === v2.x && v1.y === v2.y && v1.z === v2.z) {
|
|
577
|
+
ang = 0;
|
|
578
|
+
} else {
|
|
579
|
+
ang = Math.PI; //In this case, the vectors are pointing in opposite directions.
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
} else if (v3.dot(normal) < 0) {
|
|
583
|
+
ang = -Math.acos(v1.dot(v2));
|
|
584
|
+
} else {
|
|
585
|
+
ang = Math.acos(v1.dot(v2));
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
v1.recycle();
|
|
589
|
+
v2.recycle();
|
|
590
|
+
v3.recycle();
|
|
591
|
+
|
|
592
|
+
return ang;
|
|
593
|
+
};
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* Find the scalar value of projecting this vector onto the parameter vector or onto a vector at the specified angle away.
|
|
597
|
+
*
|
|
598
|
+
* @method platypus.Vector#scalerProjection
|
|
599
|
+
* @param vectorOrAngle {Vector|number} The other vector or the angle between the vectors.
|
|
600
|
+
* @return {number} The magnitude of the projection.
|
|
601
|
+
*/
|
|
602
|
+
proto.scalarProjection = function (vectorOrAngle) {
|
|
603
|
+
if (typeof vectorOrAngle === "number") {
|
|
604
|
+
return this.magnitude(2) * Math.cos(vectorOrAngle);
|
|
605
|
+
} else {
|
|
606
|
+
const
|
|
607
|
+
v = Vector.setUp(vectorOrAngle).normalize(),
|
|
608
|
+
d = this.dot(v);
|
|
609
|
+
|
|
610
|
+
v.recycle();
|
|
611
|
+
return d;
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* Returns a copy of this vector.
|
|
617
|
+
*
|
|
618
|
+
* @method platypus.Vector#copy
|
|
619
|
+
* @return {platypus.Vector} A copy of this vector.
|
|
620
|
+
*/
|
|
621
|
+
proto.copy = function () {
|
|
622
|
+
return Vector.setUp(this);
|
|
623
|
+
};
|
|
624
|
+
|
|
625
|
+
/**
|
|
626
|
+
* Adds properties to an object that describe the coordinates of a vector.
|
|
627
|
+
*
|
|
628
|
+
* @method platypus.Vector.assign
|
|
629
|
+
* @param object {Object} Object on which the coordinates and vector will be added.
|
|
630
|
+
* @param propertyName {String} A string describing the property name where the vector is accessable.
|
|
631
|
+
* @param [coordinateName*] {String} One or more parameters describing coordinate values on the object.
|
|
632
|
+
*/
|
|
633
|
+
Vector.assign = (function () {
|
|
634
|
+
const
|
|
635
|
+
createProperty = function (property, obj, vector, index) {
|
|
636
|
+
let temp = null,
|
|
637
|
+
propertyInUse = false;
|
|
638
|
+
|
|
639
|
+
if (typeof obj[property] !== 'undefined') {
|
|
640
|
+
temp = obj[property];
|
|
641
|
+
delete obj[property];
|
|
642
|
+
propertyInUse = true;
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
Object.defineProperty(obj, property, {
|
|
646
|
+
get: function () {
|
|
647
|
+
return vector.matrix[index];
|
|
648
|
+
},
|
|
649
|
+
set: function (value) {
|
|
650
|
+
vector.matrix[index] = value;
|
|
651
|
+
},
|
|
652
|
+
enumerable: true
|
|
653
|
+
});
|
|
654
|
+
|
|
655
|
+
if (propertyInUse) {
|
|
656
|
+
obj[property] = temp;
|
|
657
|
+
}
|
|
658
|
+
};
|
|
659
|
+
|
|
660
|
+
return function (obj, prop) {
|
|
661
|
+
if (obj && prop) {
|
|
662
|
+
if (!obj[prop]) {
|
|
663
|
+
obj[prop] = Vector.setUp();
|
|
664
|
+
|
|
665
|
+
for (let i = 2; i < arguments.length; i++) {
|
|
666
|
+
if (arguments[i] !== prop) {
|
|
667
|
+
createProperty(arguments[i], obj, obj[prop], i - 2);
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
return null;
|
|
672
|
+
}
|
|
673
|
+
return obj[prop];
|
|
674
|
+
} else {
|
|
675
|
+
return null;
|
|
676
|
+
}
|
|
677
|
+
};
|
|
678
|
+
}());
|
|
679
|
+
|
|
680
|
+
/**
|
|
681
|
+
* Returns a Vector from cache or creates a new one if none are available.
|
|
682
|
+
*
|
|
683
|
+
* @method platypus.Vector.setUp
|
|
684
|
+
* @return {platypus.Vector} The instantiated Vector.
|
|
685
|
+
*/
|
|
686
|
+
/**
|
|
687
|
+
* Returns a Vector back to the cache. Prefer the Vector's recycle method since it recycles property objects as well.
|
|
688
|
+
*
|
|
689
|
+
* @method platypus.Vector.recycle
|
|
690
|
+
* @param {platypus.Vector} vector The Vector to be recycled.
|
|
691
|
+
*/
|
|
692
|
+
/**
|
|
693
|
+
* Relinquishes properties of the vector and recycles it.
|
|
694
|
+
*
|
|
695
|
+
* @method platypus.Vector#recycle
|
|
696
|
+
*/
|
|
697
|
+
recycle.add(Vector, 'Vector', Vector, function () {
|
|
698
|
+
this.matrix.length = 0;
|
|
699
|
+
}, true);
|
|
700
|
+
|
|
701
|
+
return Vector;
|
|
702
|
+
}());
|