Skip to content

Commit d067f5d

Browse files
committed
Takes the x and y coordinates and transforms them into the same space as defined by the position, rotation and scale values.
1 parent 70b31c7 commit d067f5d

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

v3/src/math/TransformXY.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
var Vector2 = require('./Vector2');
2+
3+
/**
4+
* Takes the `x` and `y` coordinates and transforms them into the same space as
5+
* defined by the position, rotation and scale values.
6+
*
7+
* @function Phaser.Math.TransformXY
8+
* @since 3.0.0
9+
*
10+
* @param {number} x - The x coordinate to be transformed.
11+
* @param {number} y - The y coordinate to be transformed.
12+
* @param {number} positionX - Horizontal position of the transform point.
13+
* @param {number} positionY - Vertical position of the transform point.
14+
* @param {number} rotation - Rotation of the transform point, in radians.
15+
* @param {number} scaleX - Horizontal scale of the transform point.
16+
* @param {number} scaleY - Vertical scale of the transform point.
17+
* @param {Vector2|Point|object} [output] - [description]
18+
*
19+
* @return {Vector2|Point|object} The translated point.
20+
*/
21+
var TransformXY = function (x, y, positionX, positionY, rotation, scaleX, scaleY, output)
22+
{
23+
if (output === undefined) { output = new Vector2(); }
24+
25+
// ITRS
26+
27+
var sr = Math.sin(-rotation);
28+
var cr = Math.cos(-rotation);
29+
30+
var a = cr * scaleX;
31+
var b = -sr * scaleX;
32+
var c = sr * scaleY;
33+
var d = cr * scaleY;
34+
35+
// Invert
36+
37+
var n = a * d - b * c;
38+
39+
var m0 = d / n;
40+
var m1 = -b / n;
41+
var m2 = -c / n;
42+
var m3 = a / n;
43+
var m4 = (c * positionY - d * positionX) / n;
44+
var m5 = -(a * positionY - b * positionX) / n;
45+
46+
// Transform
47+
48+
output.x = x * m0 + y * m2 + m4;
49+
output.y = x * m1 + y * m3 + m5;
50+
51+
return output;
52+
};
53+
54+
module.exports = TransformXY;

0 commit comments

Comments
 (0)