forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.js
More file actions
388 lines (316 loc) · 10 KB
/
Copy pathMatrix.js
File metadata and controls
388 lines (316 loc) · 10 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
/**
* @author Mat Groves http://matgroves.com/ @Doormat23
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Matrix is a 3x3 matrix mostly used for display transforms within the renderer.
*
* It is represented like so:
*
* | a | b | tx |
* | c | d | ty |
* | 0 | 0 | 1 |
*
* @class Phaser.Matrix
* @constructor
* @param {number} [a=1] - Horizontal scaling
* @param {number} [b=0] - Horizontal skewing
* @param {number} [c=0] - Vertical skewing
* @param {number} [d=1] - Vertical scaling
* @param {number} [tx=0] - Horizontal translation
* @param {number} [ty=0] - Vertical translation
*/
Phaser.Matrix = function (a, b, c, d, tx, ty) {
if (a === undefined || a === null) { a = 1; }
if (b === undefined || b === null) { b = 0; }
if (c === undefined || c === null) { c = 0; }
if (d === undefined || d === null) { d = 1; }
if (tx === undefined || tx === null) { tx = 0; }
if (ty === undefined || ty === null) { ty = 0; }
/**
* @property {number} a
* @default 1
*/
this.a = a;
/**
* @property {number} b
* @default 0
*/
this.b = b;
/**
* @property {number} c
* @default 0
*/
this.c = c;
/**
* @property {number} d
* @default 1
*/
this.d = d;
/**
* @property {number} tx
* @default 0
*/
this.tx = tx;
/**
* @property {number} ty
* @default 0
*/
this.ty = ty;
/**
* @property {number} type - The const type of this object.
* @readonly
*/
this.type = Phaser.MATRIX;
};
Phaser.Matrix.prototype.constructor = Phaser.Matrix;
Phaser.Matrix.prototype = {
/**
* Sets the values of this Matrix to the values in the given array.
*
* The Array elements should be set as follows:
*
* a = array[0]
* b = array[1]
* c = array[3]
* d = array[4]
* tx = array[2]
* ty = array[5]
*
* @method Phaser.Matrix#fromArray
* @param {Array} array - The array to copy from.
* @return {Phaser.Matrix} This Matrix object.
*/
fromArray: function (array) {
return this.setTo(array[0], array[1], array[3], array[4], array[2], array[5]);
},
/**
* Sets the values of this Matrix to the given values.
*
* @method Phaser.Matrix#setTo
* @param {number} a - Horizontal scaling
* @param {number} b - Horizontal skewing
* @param {number} c - Vertical skewing
* @param {number} d - Vertical scaling
* @param {number} tx - Horizontal translation
* @param {number} ty - Vertical translation
* @return {Phaser.Matrix} This Matrix object.
*/
setTo: function (a, b, c, d, tx, ty) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.tx = tx;
this.ty = ty;
return this;
},
/**
* Creates a new Matrix object based on the values of this Matrix.
* If you provide the output parameter the values of this Matrix will be copied over to it.
* If the output parameter is blank a new Matrix object will be created.
*
* @method Phaser.Matrix#clone
* @param {Phaser.Matrix} [output] - If provided the values of this Matrix will be copied to it, otherwise a new Matrix object is created.
* @return {Phaser.Matrix} A clone of this Matrix.
*/
clone: function (output) {
if (output === undefined || output === null)
{
output = new Phaser.Matrix(this.a, this.b, this.c, this.d, this.tx, this.ty);
}
else
{
output.a = this.a;
output.b = this.b;
output.c = this.c;
output.d = this.d;
output.tx = this.tx;
output.ty = this.ty;
}
return output;
},
/**
* Copies the properties from this Matrix to the given Matrix.
*
* @method Phaser.Matrix#copyTo
* @param {Phaser.Matrix} matrix - The Matrix to copy from.
* @return {Phaser.Matrix} The destination Matrix object.
*/
copyTo: function (matrix) {
matrix.copyFrom(this);
return matrix;
},
/**
* Copies the properties from the given Matrix into this Matrix.
*
* @method Phaser.Matrix#copyFrom
* @param {Phaser.Matrix} matrix - The Matrix to copy from.
* @return {Phaser.Matrix} This Matrix object.
*/
copyFrom: function (matrix) {
this.a = matrix.a;
this.b = matrix.b;
this.c = matrix.c;
this.d = matrix.d;
this.tx = matrix.tx;
this.ty = matrix.ty;
return this;
},
/**
* Creates a Float32 Array with values populated from this Matrix object.
*
* @method Phaser.Matrix#toArray
* @param {boolean} [transpose=false] - Whether the values in the array are transposed or not.
* @param {Float32Array} [array] - If provided the values will be set into this array, otherwise a new Float32Array is created.
* @return {Float32Array} The newly created array which contains the matrix.
*/
toArray: function (transpose, array) {
if (array === undefined) { array = new Float32Array(9); }
if (transpose)
{
array[0] = this.a;
array[1] = this.b;
array[2] = 0;
array[3] = this.c;
array[4] = this.d;
array[5] = 0;
array[6] = this.tx;
array[7] = this.ty;
array[8] = 1;
}
else
{
array[0] = this.a;
array[1] = this.c;
array[2] = this.tx;
array[3] = this.b;
array[4] = this.d;
array[5] = this.ty;
array[6] = 0;
array[7] = 0;
array[8] = 1;
}
return array;
},
/**
* Get a new position with the current transformation applied.
*
* Can be used to go from a childs coordinate space to the world coordinate space (e.g. rendering)
*
* @method Phaser.Matrix#apply
* @param {Phaser.Point} pos - The origin Point.
* @param {Phaser.Point} [newPos] - The point that the new position is assigned to. This can be same as input point.
* @return {Phaser.Point} The new point, transformed through this matrix.
*/
apply: function (pos, newPos) {
if (newPos === undefined) { newPos = new Phaser.Point(); }
newPos.x = this.a * pos.x + this.c * pos.y + this.tx;
newPos.y = this.b * pos.x + this.d * pos.y + this.ty;
return newPos;
},
/**
* Get a new position with the inverse of the current transformation applied.
*
* Can be used to go from the world coordinate space to a childs coordinate space. (e.g. input)
*
* @method Phaser.Matrix#applyInverse
* @param {Phaser.Point} pos - The origin Point.
* @param {Phaser.Point} [newPos] - The point that the new position is assigned to. This can be same as input point.
* @return {Phaser.Point} The new point, inverse transformed through this matrix.
*/
applyInverse: function (pos, newPos) {
if (newPos === undefined) { newPos = new Phaser.Point(); }
var id = 1 / (this.a * this.d + this.c * -this.b);
var x = pos.x;
var y = pos.y;
newPos.x = this.d * id * x + -this.c * id * y + (this.ty * this.c - this.tx * this.d) * id;
newPos.y = this.a * id * y + -this.b * id * x + (-this.ty * this.a + this.tx * this.b) * id;
return newPos;
},
/**
* Translates the matrix on the x and y.
* This is the same as Matrix.tx += x.
*
* @method Phaser.Matrix#translate
* @param {number} x - The x value to translate on.
* @param {number} y - The y value to translate on.
* @return {Phaser.Matrix} This Matrix object.
*/
translate: function (x, y) {
this.tx += x;
this.ty += y;
return this;
},
/**
* Applies a scale transformation to this matrix.
*
* @method Phaser.Matrix#scale
* @param {number} x - The amount to scale horizontally.
* @param {number} y - The amount to scale vertically.
* @return {Phaser.Matrix} This Matrix object.
*/
scale: function (x, y) {
this.a *= x;
this.d *= y;
this.c *= x;
this.b *= y;
this.tx *= x;
this.ty *= y;
return this;
},
/**
* Applies a rotation transformation to this matrix.
*
* @method Phaser.Matrix#rotate
* @param {number} angle - The angle to rotate by, given in radians.
* @return {Phaser.Matrix} This Matrix object.
*/
rotate: function (angle) {
var cos = Math.cos(angle);
var sin = Math.sin(angle);
var a1 = this.a;
var c1 = this.c;
var tx1 = this.tx;
this.a = a1 * cos-this.b * sin;
this.b = a1 * sin+this.b * cos;
this.c = c1 * cos-this.d * sin;
this.d = c1 * sin+this.d * cos;
this.tx = tx1 * cos - this.ty * sin;
this.ty = tx1 * sin + this.ty * cos;
return this;
},
/**
* Appends the given Matrix to this Matrix.
*
* @method Phaser.Matrix#append
* @param {Phaser.Matrix} matrix - The matrix to append to this one.
* @return {Phaser.Matrix} This Matrix object.
*/
append: function (matrix) {
var a1 = this.a;
var b1 = this.b;
var c1 = this.c;
var d1 = this.d;
this.a = matrix.a * a1 + matrix.b * c1;
this.b = matrix.a * b1 + matrix.b * d1;
this.c = matrix.c * a1 + matrix.d * c1;
this.d = matrix.c * b1 + matrix.d * d1;
this.tx = matrix.tx * a1 + matrix.ty * c1 + this.tx;
this.ty = matrix.tx * b1 + matrix.ty * d1 + this.ty;
return this;
},
/**
* Resets this Matrix to an identity (default) matrix.
*
* @method Phaser.Matrix#identity
* @return {Phaser.Matrix} This Matrix object.
*/
identity: function () {
return this.setTo(1, 0, 0, 1, 0, 0);
}
};
Phaser.identityMatrix = new Phaser.Matrix();
Phaser.tempMatrix = new Phaser.Matrix();