melonjs 10.2.0 → 10.3.0
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/README.md +1 -1
- package/dist/melonjs.js +4435 -4283
- package/dist/melonjs.min.js +4 -4
- package/dist/melonjs.module.d.ts +3348 -3833
- package/dist/melonjs.module.js +4025 -3920
- package/package.json +13 -14
- package/src/audio/audio.js +45 -45
- package/src/camera/camera2d.js +78 -101
- package/src/entity/draggable.js +21 -29
- package/src/entity/droptarget.js +24 -31
- package/src/entity/entity.js +34 -38
- package/src/game.js +8 -8
- package/src/{shapes → geometries}/ellipse.js +46 -46
- package/src/{shapes → geometries}/line.js +14 -14
- package/src/{shapes → geometries}/poly.js +103 -54
- package/src/{shapes → geometries}/rectangle.js +73 -120
- package/src/index.js +18 -19
- package/src/input/gamepad.js +20 -20
- package/src/input/input.js +3 -3
- package/src/input/keyboard.js +122 -124
- package/src/input/pointer.js +102 -62
- package/src/input/pointerevent.js +97 -42
- package/src/lang/deprecated.js +29 -18
- package/src/level/level.js +34 -26
- package/src/level/tiled/TMXGroup.js +12 -13
- package/src/level/tiled/TMXLayer.js +41 -42
- package/src/level/tiled/TMXObject.js +76 -70
- package/src/level/tiled/TMXTile.js +13 -15
- package/src/level/tiled/TMXTileMap.js +26 -25
- package/src/level/tiled/TMXTileset.js +14 -15
- package/src/level/tiled/TMXTilesetGroup.js +5 -6
- package/src/level/tiled/TMXUtils.js +13 -11
- package/src/level/tiled/renderer/TMXHexagonalRenderer.js +3 -4
- package/src/level/tiled/renderer/TMXIsometricRenderer.js +3 -4
- package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +2 -3
- package/src/level/tiled/renderer/TMXRenderer.js +18 -19
- package/src/level/tiled/renderer/TMXStaggeredRenderer.js +2 -3
- package/src/loader/loader.js +46 -40
- package/src/loader/loadingscreen.js +7 -7
- package/src/math/color.js +68 -88
- package/src/math/math.js +33 -33
- package/src/math/matrix2.js +70 -71
- package/src/math/matrix3.js +90 -91
- package/src/math/observable_vector2.js +91 -92
- package/src/math/observable_vector3.js +110 -106
- package/src/math/vector2.js +116 -104
- package/src/math/vector3.js +129 -110
- package/src/particles/emitter.js +116 -126
- package/src/particles/particle.js +4 -5
- package/src/particles/particlecontainer.js +2 -3
- package/src/physics/body.js +82 -83
- package/src/physics/bounds.js +64 -66
- package/src/physics/collision.js +21 -22
- package/src/physics/detector.js +13 -13
- package/src/physics/quadtree.js +26 -25
- package/src/physics/sat.js +21 -21
- package/src/physics/world.js +23 -22
- package/src/plugin/plugin.js +12 -13
- package/src/renderable/GUI.js +20 -26
- package/src/renderable/collectable.js +6 -7
- package/src/renderable/colorlayer.js +11 -12
- package/src/renderable/container.js +98 -81
- package/src/renderable/imagelayer.js +33 -35
- package/src/renderable/nineslicesprite.js +15 -16
- package/src/renderable/renderable.js +112 -111
- package/src/renderable/sprite.js +71 -58
- package/src/renderable/trigger.js +17 -19
- package/src/state/stage.js +14 -15
- package/src/state/state.js +78 -78
- package/src/system/device.js +137 -180
- package/src/system/event.js +116 -104
- package/src/system/pooling.js +15 -15
- package/src/system/save.js +9 -6
- package/src/system/timer.js +33 -33
- package/src/text/bitmaptext.js +39 -46
- package/src/text/bitmaptextdata.js +14 -15
- package/src/text/text.js +55 -58
- package/src/tweens/easing.js +5 -5
- package/src/tweens/interpolation.js +5 -5
- package/src/tweens/tween.js +49 -40
- package/src/utils/agent.js +12 -11
- package/src/utils/array.js +8 -8
- package/src/utils/file.js +7 -7
- package/src/utils/function.js +8 -8
- package/src/utils/string.js +19 -19
- package/src/utils/utils.js +23 -23
- package/src/video/canvas/canvas_renderer.js +127 -128
- package/src/video/renderer.js +69 -69
- package/src/video/texture.js +80 -82
- package/src/video/texture_cache.js +2 -4
- package/src/video/video.js +38 -38
- package/src/video/webgl/buffer/vertex.js +11 -3
- package/src/video/webgl/glshader.js +31 -32
- package/src/video/webgl/webgl_compositor.js +145 -127
- package/src/video/webgl/webgl_renderer.js +196 -175
package/src/math/vector2.js
CHANGED
|
@@ -5,10 +5,9 @@ import pool from "./../system/pooling.js";
|
|
|
5
5
|
* @classdesc
|
|
6
6
|
* a generic 2D Vector Object
|
|
7
7
|
* @class Vector2d
|
|
8
|
-
* @
|
|
9
|
-
* @
|
|
10
|
-
* @param {
|
|
11
|
-
* @param {Number} [y=0] y value of the vector
|
|
8
|
+
* @memberof me
|
|
9
|
+
* @param {number} [x=0] x value of the vector
|
|
10
|
+
* @param {number} [y=0] y value of the vector
|
|
12
11
|
*/
|
|
13
12
|
class Vector2d {
|
|
14
13
|
|
|
@@ -27,7 +26,8 @@ class Vector2d {
|
|
|
27
26
|
}
|
|
28
27
|
|
|
29
28
|
/**
|
|
30
|
-
* @ignore
|
|
29
|
+
* @ignore
|
|
30
|
+
*/
|
|
31
31
|
_set(x, y) {
|
|
32
32
|
this.x = x;
|
|
33
33
|
this.y = y;
|
|
@@ -37,11 +37,11 @@ class Vector2d {
|
|
|
37
37
|
/**
|
|
38
38
|
* set the Vector x and y properties to the given values<br>
|
|
39
39
|
* @name set
|
|
40
|
-
* @
|
|
40
|
+
* @memberof me.Vector2d
|
|
41
41
|
* @function
|
|
42
|
-
* @param {
|
|
43
|
-
* @param {
|
|
44
|
-
* @
|
|
42
|
+
* @param {number} x
|
|
43
|
+
* @param {number} y
|
|
44
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
45
45
|
*/
|
|
46
46
|
set(x, y) {
|
|
47
47
|
if (x !== +x || y !== +y) {
|
|
@@ -53,18 +53,18 @@ class Vector2d {
|
|
|
53
53
|
/**
|
|
54
54
|
* x value of the vector
|
|
55
55
|
* @public
|
|
56
|
-
* @type
|
|
56
|
+
* @type {number}
|
|
57
57
|
* @name x
|
|
58
|
-
* @
|
|
58
|
+
* @memberof me.Vector2d
|
|
59
59
|
*/
|
|
60
60
|
//this.x = x;
|
|
61
61
|
|
|
62
62
|
/**
|
|
63
63
|
* y value of the vector
|
|
64
64
|
* @public
|
|
65
|
-
* @type
|
|
65
|
+
* @type {number}
|
|
66
66
|
* @name y
|
|
67
|
-
* @
|
|
67
|
+
* @memberof me.Vector2d
|
|
68
68
|
*/
|
|
69
69
|
//this.y = y;
|
|
70
70
|
|
|
@@ -74,9 +74,9 @@ class Vector2d {
|
|
|
74
74
|
/**
|
|
75
75
|
* set the Vector x and y properties to 0
|
|
76
76
|
* @name setZero
|
|
77
|
-
* @
|
|
77
|
+
* @memberof me.Vector2d
|
|
78
78
|
* @function
|
|
79
|
-
* @
|
|
79
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
80
80
|
*/
|
|
81
81
|
setZero() {
|
|
82
82
|
return this.set(0, 0);
|
|
@@ -85,10 +85,10 @@ class Vector2d {
|
|
|
85
85
|
/**
|
|
86
86
|
* set the Vector x and y properties using the passed vector
|
|
87
87
|
* @name setV
|
|
88
|
-
* @
|
|
88
|
+
* @memberof me.Vector2d
|
|
89
89
|
* @function
|
|
90
90
|
* @param {me.Vector2d} v
|
|
91
|
-
* @
|
|
91
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
92
92
|
*/
|
|
93
93
|
setV(v) {
|
|
94
94
|
return this._set(v.x, v.y);
|
|
@@ -97,10 +97,10 @@ class Vector2d {
|
|
|
97
97
|
/**
|
|
98
98
|
* Add the passed vector to this vector
|
|
99
99
|
* @name add
|
|
100
|
-
* @
|
|
100
|
+
* @memberof me.Vector2d
|
|
101
101
|
* @function
|
|
102
102
|
* @param {me.Vector2d} v
|
|
103
|
-
* @
|
|
103
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
104
104
|
*/
|
|
105
105
|
add(v) {
|
|
106
106
|
return this._set(this.x + v.x, this.y + v.y);
|
|
@@ -109,10 +109,10 @@ class Vector2d {
|
|
|
109
109
|
/**
|
|
110
110
|
* Substract the passed vector to this vector
|
|
111
111
|
* @name sub
|
|
112
|
-
* @
|
|
112
|
+
* @memberof me.Vector2d
|
|
113
113
|
* @function
|
|
114
114
|
* @param {me.Vector2d} v
|
|
115
|
-
* @
|
|
115
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
116
116
|
*/
|
|
117
117
|
sub(v) {
|
|
118
118
|
return this._set(this.x - v.x, this.y - v.y);
|
|
@@ -121,11 +121,11 @@ class Vector2d {
|
|
|
121
121
|
/**
|
|
122
122
|
* Multiply this vector values by the given scalar
|
|
123
123
|
* @name scale
|
|
124
|
-
* @
|
|
124
|
+
* @memberof me.Vector2d
|
|
125
125
|
* @function
|
|
126
|
-
* @param {
|
|
127
|
-
* @param {
|
|
128
|
-
* @
|
|
126
|
+
* @param {number} x
|
|
127
|
+
* @param {number} [y=x]
|
|
128
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
129
129
|
*/
|
|
130
130
|
scale(x, y) {
|
|
131
131
|
return this._set(this.x * x, this.y * (typeof (y) !== "undefined" ? y : x));
|
|
@@ -134,9 +134,9 @@ class Vector2d {
|
|
|
134
134
|
/**
|
|
135
135
|
* Convert this vector into isometric coordinate space
|
|
136
136
|
* @name toIso
|
|
137
|
-
* @
|
|
137
|
+
* @memberof me.Vector2d
|
|
138
138
|
* @function
|
|
139
|
-
* @
|
|
139
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
140
140
|
*/
|
|
141
141
|
toIso() {
|
|
142
142
|
return this._set(this.x - this.y, (this.x + this.y) * 0.5);
|
|
@@ -145,9 +145,9 @@ class Vector2d {
|
|
|
145
145
|
/**
|
|
146
146
|
* Convert this vector into 2d coordinate space
|
|
147
147
|
* @name to2d
|
|
148
|
-
* @
|
|
148
|
+
* @memberof me.Vector2d
|
|
149
149
|
* @function
|
|
150
|
-
* @
|
|
150
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
151
151
|
*/
|
|
152
152
|
to2d() {
|
|
153
153
|
return this._set(this.y + this.x / 2, this.y - this.x / 2);
|
|
@@ -156,10 +156,10 @@ class Vector2d {
|
|
|
156
156
|
/**
|
|
157
157
|
* Multiply this vector values by the passed vector
|
|
158
158
|
* @name scaleV
|
|
159
|
-
* @
|
|
159
|
+
* @memberof me.Vector2d
|
|
160
160
|
* @function
|
|
161
161
|
* @param {me.Vector2d} v
|
|
162
|
-
* @
|
|
162
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
163
163
|
*/
|
|
164
164
|
scaleV(v) {
|
|
165
165
|
return this._set(this.x * v.x, this.y * v.y);
|
|
@@ -168,10 +168,10 @@ class Vector2d {
|
|
|
168
168
|
/**
|
|
169
169
|
* Divide this vector values by the passed value
|
|
170
170
|
* @name div
|
|
171
|
-
* @
|
|
171
|
+
* @memberof me.Vector2d
|
|
172
172
|
* @function
|
|
173
|
-
* @param {
|
|
174
|
-
* @
|
|
173
|
+
* @param {number} n the value to divide the vector by
|
|
174
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
175
175
|
*/
|
|
176
176
|
div(n) {
|
|
177
177
|
return this._set(this.x / n, this.y / n);
|
|
@@ -180,9 +180,9 @@ class Vector2d {
|
|
|
180
180
|
/**
|
|
181
181
|
* Update this vector values to absolute values
|
|
182
182
|
* @name abs
|
|
183
|
-
* @
|
|
183
|
+
* @memberof me.Vector2d
|
|
184
184
|
* @function
|
|
185
|
-
* @
|
|
185
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
186
186
|
*/
|
|
187
187
|
abs() {
|
|
188
188
|
return this._set((this.x < 0) ? -this.x : this.x, (this.y < 0) ? -this.y : this.y);
|
|
@@ -191,11 +191,11 @@ class Vector2d {
|
|
|
191
191
|
/**
|
|
192
192
|
* Clamp the vector value within the specified value range
|
|
193
193
|
* @name clamp
|
|
194
|
-
* @
|
|
194
|
+
* @memberof me.Vector2d
|
|
195
195
|
* @function
|
|
196
|
-
* @param {
|
|
197
|
-
* @param {
|
|
198
|
-
* @
|
|
196
|
+
* @param {number} low
|
|
197
|
+
* @param {number} high
|
|
198
|
+
* @returns {me.Vector2d} new me.Vector2d
|
|
199
199
|
*/
|
|
200
200
|
clamp(low, high) {
|
|
201
201
|
return new Vector2d(clamp(this.x, low, high), clamp(this.y, low, high));
|
|
@@ -204,11 +204,11 @@ class Vector2d {
|
|
|
204
204
|
/**
|
|
205
205
|
* Clamp this vector value within the specified value range
|
|
206
206
|
* @name clampSelf
|
|
207
|
-
* @
|
|
207
|
+
* @memberof me.Vector2d
|
|
208
208
|
* @function
|
|
209
|
-
* @param {
|
|
210
|
-
* @param {
|
|
211
|
-
* @
|
|
209
|
+
* @param {number} low
|
|
210
|
+
* @param {number} high
|
|
211
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
212
212
|
*/
|
|
213
213
|
clampSelf(low, high) {
|
|
214
214
|
return this._set(clamp(this.x, low, high), clamp(this.y, low, high));
|
|
@@ -217,10 +217,10 @@ class Vector2d {
|
|
|
217
217
|
/**
|
|
218
218
|
* Update this vector with the minimum value between this and the passed vector
|
|
219
219
|
* @name minV
|
|
220
|
-
* @
|
|
220
|
+
* @memberof me.Vector2d
|
|
221
221
|
* @function
|
|
222
222
|
* @param {me.Vector2d} v
|
|
223
|
-
* @
|
|
223
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
224
224
|
*/
|
|
225
225
|
minV(v) {
|
|
226
226
|
return this._set((this.x < v.x) ? this.x : v.x, (this.y < v.y) ? this.y : v.y);
|
|
@@ -229,10 +229,10 @@ class Vector2d {
|
|
|
229
229
|
/**
|
|
230
230
|
* Update this vector with the maximum value between this and the passed vector
|
|
231
231
|
* @name maxV
|
|
232
|
-
* @
|
|
232
|
+
* @memberof me.Vector2d
|
|
233
233
|
* @function
|
|
234
234
|
* @param {me.Vector2d} v
|
|
235
|
-
* @
|
|
235
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
236
236
|
*/
|
|
237
237
|
maxV(v) {
|
|
238
238
|
return this._set((this.x > v.x) ? this.x : v.x, (this.y > v.y) ? this.y : v.y);
|
|
@@ -241,9 +241,9 @@ class Vector2d {
|
|
|
241
241
|
/**
|
|
242
242
|
* Floor the vector values
|
|
243
243
|
* @name floor
|
|
244
|
-
* @
|
|
244
|
+
* @memberof me.Vector2d
|
|
245
245
|
* @function
|
|
246
|
-
* @
|
|
246
|
+
* @returns {me.Vector2d} new me.Vector2d
|
|
247
247
|
*/
|
|
248
248
|
floor() {
|
|
249
249
|
return new Vector2d(Math.floor(this.x), Math.floor(this.y));
|
|
@@ -252,9 +252,9 @@ class Vector2d {
|
|
|
252
252
|
/**
|
|
253
253
|
* Floor this vector values
|
|
254
254
|
* @name floorSelf
|
|
255
|
-
* @
|
|
255
|
+
* @memberof me.Vector2d
|
|
256
256
|
* @function
|
|
257
|
-
* @
|
|
257
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
258
258
|
*/
|
|
259
259
|
floorSelf() {
|
|
260
260
|
return this._set(Math.floor(this.x), Math.floor(this.y));
|
|
@@ -263,9 +263,9 @@ class Vector2d {
|
|
|
263
263
|
/**
|
|
264
264
|
* Ceil the vector values
|
|
265
265
|
* @name ceil
|
|
266
|
-
* @
|
|
266
|
+
* @memberof me.Vector2d
|
|
267
267
|
* @function
|
|
268
|
-
* @
|
|
268
|
+
* @returns {me.Vector2d} new me.Vector2d
|
|
269
269
|
*/
|
|
270
270
|
ceil() {
|
|
271
271
|
return new Vector2d(Math.ceil(this.x), Math.ceil(this.y));
|
|
@@ -274,9 +274,9 @@ class Vector2d {
|
|
|
274
274
|
/**
|
|
275
275
|
* Ceil this vector values
|
|
276
276
|
* @name ceilSelf
|
|
277
|
-
* @
|
|
277
|
+
* @memberof me.Vector2d
|
|
278
278
|
* @function
|
|
279
|
-
* @
|
|
279
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
280
280
|
*/
|
|
281
281
|
ceilSelf() {
|
|
282
282
|
return this._set(Math.ceil(this.x), Math.ceil(this.y));
|
|
@@ -285,9 +285,9 @@ class Vector2d {
|
|
|
285
285
|
/**
|
|
286
286
|
* Negate the vector values
|
|
287
287
|
* @name negate
|
|
288
|
-
* @
|
|
288
|
+
* @memberof me.Vector2d
|
|
289
289
|
* @function
|
|
290
|
-
* @
|
|
290
|
+
* @returns {me.Vector2d} new me.Vector2d
|
|
291
291
|
*/
|
|
292
292
|
negate() {
|
|
293
293
|
return new Vector2d(-this.x, -this.y);
|
|
@@ -296,9 +296,9 @@ class Vector2d {
|
|
|
296
296
|
/**
|
|
297
297
|
* Negate this vector values
|
|
298
298
|
* @name negateSelf
|
|
299
|
-
* @
|
|
299
|
+
* @memberof me.Vector2d
|
|
300
300
|
* @function
|
|
301
|
-
* @
|
|
301
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
302
302
|
*/
|
|
303
303
|
negateSelf() {
|
|
304
304
|
return this._set(-this.x, -this.y);
|
|
@@ -307,10 +307,10 @@ class Vector2d {
|
|
|
307
307
|
/**
|
|
308
308
|
* Copy the x,y values of the passed vector to this one
|
|
309
309
|
* @name copy
|
|
310
|
-
* @
|
|
310
|
+
* @memberof me.Vector2d
|
|
311
311
|
* @function
|
|
312
312
|
* @param {me.Vector2d} v
|
|
313
|
-
* @
|
|
313
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
314
314
|
*/
|
|
315
315
|
copy(v) {
|
|
316
316
|
return this._set(v.x, v.y);
|
|
@@ -319,19 +319,19 @@ class Vector2d {
|
|
|
319
319
|
/**
|
|
320
320
|
* return true if the two vectors are the same
|
|
321
321
|
* @name equals
|
|
322
|
-
* @
|
|
322
|
+
* @memberof me.Vector2d
|
|
323
323
|
* @function
|
|
324
324
|
* @param {me.Vector2d} v
|
|
325
|
-
* @
|
|
325
|
+
* @returns {boolean}
|
|
326
326
|
*/
|
|
327
327
|
/**
|
|
328
328
|
* return true if this vector is equal to the given values
|
|
329
329
|
* @name equals
|
|
330
|
-
* @
|
|
330
|
+
* @memberof me.Vector2d
|
|
331
331
|
* @function
|
|
332
|
-
* @param {
|
|
333
|
-
* @param {
|
|
334
|
-
* @
|
|
332
|
+
* @param {number} x
|
|
333
|
+
* @param {number} y
|
|
334
|
+
* @returns {boolean}
|
|
335
335
|
*/
|
|
336
336
|
equals() {
|
|
337
337
|
var _x, _y;
|
|
@@ -350,9 +350,9 @@ class Vector2d {
|
|
|
350
350
|
/**
|
|
351
351
|
* normalize this vector (scale the vector so that its magnitude is 1)
|
|
352
352
|
* @name normalize
|
|
353
|
-
* @
|
|
353
|
+
* @memberof me.Vector2d
|
|
354
354
|
* @function
|
|
355
|
-
* @
|
|
355
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
356
356
|
*/
|
|
357
357
|
normalize() {
|
|
358
358
|
return this.div(this.length() || 1);
|
|
@@ -362,9 +362,9 @@ class Vector2d {
|
|
|
362
362
|
* change this vector to be perpendicular to what it was before.<br>
|
|
363
363
|
* (Effectively rotates it 90 degrees in a clockwise direction)
|
|
364
364
|
* @name perp
|
|
365
|
-
* @
|
|
365
|
+
* @memberof me.Vector2d
|
|
366
366
|
* @function
|
|
367
|
-
* @
|
|
367
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
368
368
|
*/
|
|
369
369
|
perp() {
|
|
370
370
|
return this._set(this.y, -this.x);
|
|
@@ -373,11 +373,11 @@ class Vector2d {
|
|
|
373
373
|
/**
|
|
374
374
|
* Rotate this vector (counter-clockwise) by the specified angle (in radians).
|
|
375
375
|
* @name rotate
|
|
376
|
-
* @
|
|
376
|
+
* @memberof me.Vector2d
|
|
377
377
|
* @function
|
|
378
378
|
* @param {number} angle The angle to rotate (in radians)
|
|
379
379
|
* @param {me.Vector2d|me.ObservableVector2d} [v] an optional point to rotate around
|
|
380
|
-
* @
|
|
380
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
381
381
|
*/
|
|
382
382
|
rotate(angle, v) {
|
|
383
383
|
var cx = 0;
|
|
@@ -399,33 +399,45 @@ class Vector2d {
|
|
|
399
399
|
|
|
400
400
|
/**
|
|
401
401
|
* return the dot product of this vector and the passed one
|
|
402
|
-
* @name
|
|
403
|
-
* @
|
|
402
|
+
* @name dot
|
|
403
|
+
* @memberof me.Vector2d
|
|
404
404
|
* @function
|
|
405
405
|
* @param {me.Vector2d} v
|
|
406
|
-
* @
|
|
406
|
+
* @returns {number} The dot product.
|
|
407
407
|
*/
|
|
408
|
-
|
|
408
|
+
dot(v) {
|
|
409
409
|
return this.x * v.x + this.y * v.y;
|
|
410
410
|
}
|
|
411
411
|
|
|
412
|
-
|
|
413
|
-
* return the
|
|
414
|
-
* @name
|
|
415
|
-
* @
|
|
412
|
+
/**
|
|
413
|
+
* return the cross product of this vector and the passed one
|
|
414
|
+
* @name cross
|
|
415
|
+
* @memberof me.Vector2d
|
|
416
416
|
* @function
|
|
417
|
-
* @
|
|
417
|
+
* @param {me.Vector2d} v
|
|
418
|
+
* @returns {number} The cross product.
|
|
418
419
|
*/
|
|
420
|
+
cross(v) {
|
|
421
|
+
return this.x * v.y - this.y * v.x;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* return the square length of this vector
|
|
426
|
+
* @name length2
|
|
427
|
+
* @memberof me.Vector2d
|
|
428
|
+
* @function
|
|
429
|
+
* @returns {number} The length^2 of this vector.
|
|
430
|
+
*/
|
|
419
431
|
length2() {
|
|
420
|
-
return this.
|
|
432
|
+
return this.dot(this);
|
|
421
433
|
}
|
|
422
434
|
|
|
423
435
|
/**
|
|
424
436
|
* return the length (magnitude) of this vector
|
|
425
437
|
* @name length
|
|
426
|
-
* @
|
|
438
|
+
* @memberof me.Vector2d
|
|
427
439
|
* @function
|
|
428
|
-
* @
|
|
440
|
+
* @returns {number} the length of this vector
|
|
429
441
|
*/
|
|
430
442
|
length() {
|
|
431
443
|
return Math.sqrt(this.length2());
|
|
@@ -434,11 +446,11 @@ class Vector2d {
|
|
|
434
446
|
/**
|
|
435
447
|
* Linearly interpolate between this vector and the given one.
|
|
436
448
|
* @name lerp
|
|
437
|
-
* @
|
|
449
|
+
* @memberof me.Vector2d
|
|
438
450
|
* @function
|
|
439
451
|
* @param {me.Vector2d} v
|
|
440
|
-
* @param {
|
|
441
|
-
* @
|
|
452
|
+
* @param {number} alpha distance along the line (alpha = 0 will be this vector, and alpha = 1 will be the given one).
|
|
453
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
442
454
|
*/
|
|
443
455
|
lerp(v, alpha) {
|
|
444
456
|
this.x += ( v.x - this.x ) * alpha;
|
|
@@ -449,10 +461,10 @@ class Vector2d {
|
|
|
449
461
|
/**
|
|
450
462
|
* return the distance between this vector and the passed one
|
|
451
463
|
* @name distance
|
|
452
|
-
* @
|
|
464
|
+
* @memberof me.Vector2d
|
|
453
465
|
* @function
|
|
454
466
|
* @param {me.Vector2d} v
|
|
455
|
-
* @
|
|
467
|
+
* @returns {number}
|
|
456
468
|
*/
|
|
457
469
|
distance(v) {
|
|
458
470
|
var dx = this.x - v.x, dy = this.y - v.y;
|
|
@@ -462,46 +474,46 @@ class Vector2d {
|
|
|
462
474
|
/**
|
|
463
475
|
* return the angle between this vector and the passed one
|
|
464
476
|
* @name angle
|
|
465
|
-
* @
|
|
477
|
+
* @memberof me.Vector2d
|
|
466
478
|
* @function
|
|
467
479
|
* @param {me.Vector2d} v
|
|
468
|
-
* @
|
|
480
|
+
* @returns {number} angle in radians
|
|
469
481
|
*/
|
|
470
482
|
angle(v) {
|
|
471
|
-
return Math.acos(clamp(this.
|
|
483
|
+
return Math.acos(clamp(this.dot(v) / (this.length() * v.length()), -1, 1));
|
|
472
484
|
}
|
|
473
485
|
|
|
474
486
|
/**
|
|
475
487
|
* project this vector on to another vector.
|
|
476
488
|
* @name project
|
|
477
|
-
* @
|
|
489
|
+
* @memberof me.Vector2d
|
|
478
490
|
* @function
|
|
479
491
|
* @param {me.Vector2d} v The vector to project onto.
|
|
480
|
-
* @
|
|
492
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
481
493
|
*/
|
|
482
494
|
project(v) {
|
|
483
|
-
return this.scale(this.
|
|
495
|
+
return this.scale(this.dot(v) / v.length2());
|
|
484
496
|
}
|
|
485
497
|
|
|
486
498
|
/**
|
|
487
499
|
* Project this vector onto a vector of unit length.<br>
|
|
488
500
|
* This is slightly more efficient than `project` when dealing with unit vectors.
|
|
489
501
|
* @name projectN
|
|
490
|
-
* @
|
|
502
|
+
* @memberof me.Vector2d
|
|
491
503
|
* @function
|
|
492
504
|
* @param {me.Vector2d} v The unit vector to project onto.
|
|
493
|
-
* @
|
|
505
|
+
* @returns {me.Vector2d} Reference to this object for method chaining
|
|
494
506
|
*/
|
|
495
507
|
projectN(v) {
|
|
496
|
-
return this.scale(this.
|
|
508
|
+
return this.scale(this.dot(v));
|
|
497
509
|
}
|
|
498
510
|
|
|
499
511
|
/**
|
|
500
512
|
* return a clone copy of this vector
|
|
501
513
|
* @name clone
|
|
502
|
-
* @
|
|
514
|
+
* @memberof me.Vector2d
|
|
503
515
|
* @function
|
|
504
|
-
* @
|
|
516
|
+
* @returns {me.Vector2d} new me.Vector2d
|
|
505
517
|
*/
|
|
506
518
|
clone() {
|
|
507
519
|
return pool.pull("Vector2d", this.x, this.y);
|
|
@@ -510,9 +522,9 @@ class Vector2d {
|
|
|
510
522
|
/**
|
|
511
523
|
* convert the object to a string representation
|
|
512
524
|
* @name toString
|
|
513
|
-
* @
|
|
525
|
+
* @memberof me.Vector2d
|
|
514
526
|
* @function
|
|
515
|
-
* @
|
|
527
|
+
* @returns {string}
|
|
516
528
|
*/
|
|
517
529
|
toString() {
|
|
518
530
|
return "x:" + this.x + ",y:" + this.y;
|