Input transform fixed

This commit is contained in:
Richard Davey 2017-06-21 00:26:21 +01:00
parent 2341f7752f
commit 001a0cb0d3
4 changed files with 52 additions and 69 deletions

View file

@ -1,4 +1,4 @@
var CHECKSUM = { var CHECKSUM = {
build: '02a4bdd0-55d7-11e7-87f5-37983a7cf73e' build: 'b1e0db40-5608-11e7-9343-73f81cbe0a9c'
}; };
module.exports = CHECKSUM; module.exports = CHECKSUM;

View file

@ -14,6 +14,7 @@ var TransformMatrix = function (a, b, c, d, tx, ty)
ty = typeof ty === 'number' ? ty : 0; ty = typeof ty === 'number' ? ty : 0;
this.matrix = new Float32Array([a, b, c, d, tx, ty, 0, 0, 1]); this.matrix = new Float32Array([a, b, c, d, tx, ty, 0, 0, 1]);
this.decomposedMatrix = { this.decomposedMatrix = {
translateX: 0, translateX: 0,
translateY: 0, translateY: 0,
@ -70,12 +71,14 @@ TransformMatrix.prototype.rotate = function (radian)
TransformMatrix.prototype.multiply = function (otherMatrix) TransformMatrix.prototype.multiply = function (otherMatrix)
{ {
var matrix = this.matrix; var matrix = this.matrix;
var a0 = matrix[0]; var a0 = matrix[0];
var b0 = matrix[1]; var b0 = matrix[1];
var c0 = matrix[2]; var c0 = matrix[2];
var d0 = matrix[3]; var d0 = matrix[3];
var tx0 = matrix[4]; var tx0 = matrix[4];
var ty0 = matrix[5]; var ty0 = matrix[5];
var a1 = otherMatrix[0]; var a1 = otherMatrix[0];
var b1 = otherMatrix[1]; var b1 = otherMatrix[1];
var c1 = otherMatrix[2]; var c1 = otherMatrix[2];
@ -96,6 +99,7 @@ TransformMatrix.prototype.multiply = function (otherMatrix)
TransformMatrix.prototype.transform = function (a, b, c, d, tx, ty) TransformMatrix.prototype.transform = function (a, b, c, d, tx, ty)
{ {
var matrix = this.matrix; var matrix = this.matrix;
var a0 = matrix[0]; var a0 = matrix[0];
var b0 = matrix[1]; var b0 = matrix[1];
var c0 = matrix[2]; var c0 = matrix[2];
@ -126,13 +130,8 @@ TransformMatrix.prototype.transformPoint = function (x, y, point)
var tx = matrix[4]; var tx = matrix[4];
var ty = matrix[5]; var ty = matrix[5];
// point.x = x * a + y * c + tx; point.x = x * a + y * c + tx;
// point.y = x * b + y * d + ty; point.y = x * b + y * d + ty;
var id = 1 / ((a * d) + (c * -b));
point.x = (d * id * x) + (-c * id * y) + (((ty * c) - (tx * d)) * id);
point.y = (a * id * y) + (-b * id * x) + (((-ty * a) + (tx * b)) * id);
return point; return point;
}; };
@ -141,20 +140,21 @@ TransformMatrix.prototype.invert = function ()
{ {
var matrix = this.matrix; var matrix = this.matrix;
var a1 = matrix[0]; var a = matrix[0];
var b1 = matrix[1]; var b = matrix[1];
var c1 = matrix[2]; var c = matrix[2];
var d1 = matrix[3]; var d = matrix[3];
var tx1 = matrix[4]; var tx = matrix[4];
var ty1 = matrix[5]; var ty = matrix[5];
var n = a1 * d1 - b1 * c1;
matrix[0] = d1 / n; var n = a * d - b * c;
matrix[1] = -b1 / n;
matrix[2] = -c1 / n; matrix[0] = d / n;
matrix[3] = a1 / n; matrix[1] = -b / n;
matrix[4] = (c1 * ty1 - d1 * tx1) / n; matrix[2] = -c / n;
matrix[5] = -(a1 * ty1 - b1 * tx1) / n; matrix[3] = a / n;
matrix[4] = (c * ty - d * tx) / n;
matrix[5] = -(a * ty - b * tx) / n;
return this; return this;
}; };
@ -176,43 +176,51 @@ TransformMatrix.prototype.setTransform = function (a, b, c, d, tx, ty)
TransformMatrix.prototype.decomposeMatrix = function () TransformMatrix.prototype.decomposeMatrix = function ()
{ {
var decomposedMatrix = this.decomposedMatrix; var decomposedMatrix = this.decomposedMatrix;
var matrix = this.matrix; var matrix = this.matrix;
var a = matrix[0]; var a = matrix[0];
var b = matrix[1]; var b = matrix[1];
var c = matrix[2]; var c = matrix[2];
var d = matrix[3]; var d = matrix[3];
var a2 = a * a; var a2 = a * a;
var b2 = b * b; var b2 = b * b;
var c2 = c * c; var c2 = c * c;
var d2 = d * d; var d2 = d * d;
var sx = mathSqrt(a2 + c2); var sx = mathSqrt(a2 + c2);
var sy = mathSqrt(b2 + d2); var sy = mathSqrt(b2 + d2);
decomposedMatrix.translateX = matrix[4]; decomposedMatrix.translateX = matrix[4];
decomposedMatrix.translateY = matrix[5]; decomposedMatrix.translateY = matrix[5];
decomposedMatrix.scaleX = sx; decomposedMatrix.scaleX = sx;
decomposedMatrix.scaleY = sy; decomposedMatrix.scaleY = sy;
decomposedMatrix.rotation = mathAcos(a / sx) * (mathAtan(-c / a) < 0 ? -1 : 1); decomposedMatrix.rotation = mathAcos(a / sx) * (mathAtan(-c / a) < 0 ? -1 : 1);
return decomposedMatrix; return decomposedMatrix;
}; };
/* identity + translate + rotate + scale */ /* identity + translate + rotate + scale */
TransformMatrix.prototype.applyITRS = function (x, y, rotation, scaleX, scaleY) TransformMatrix.prototype.applyITRS = function (x, y, rotation, scaleX, scaleY)
{ {
var matrix = this.matrix; var matrix = this.matrix;
var a = 1; var a = 1;
var b = 0; var b = 0;
var c = 0; var c = 0;
var d = 1; var d = 1;
var e = 0; var tx = 0;
var f = 0; var ty = 0;
var sr = mathSin(rotation); var sr = mathSin(rotation);
var cr = mathCos(rotation); var cr = mathCos(rotation);
// Translate // Translate
matrix[4] = a * x + c * y + e; matrix[4] = a * x + c * y + tx;
matrix[5] = b * x + d * y + f; matrix[5] = b * x + d * y + ty;
// Rotate // Rotate
matrix[0] = cr * a + -sr * c; matrix[0] = cr * a + -sr * c;

View file

@ -11,43 +11,27 @@ var GetTransformedPoint = function (matrix, gameObject, x, y, output)
if (output === undefined) { output = { x: 0, y: 0 }; } if (output === undefined) { output = { x: 0, y: 0 }; }
matrix.applyITRS(gameObject.x, gameObject.y, -gameObject.rotation, gameObject.scaleX, gameObject.scaleY); matrix.applyITRS(gameObject.x, gameObject.y, -gameObject.rotation, gameObject.scaleX, gameObject.scaleY);
matrix.invert(); matrix.invert();
// matrix.transformPoint(x, y, output);
var ma = matrix.matrix;
var a = ma[0];
var b = ma[1];
var c = ma[2];
var d = ma[3];
var e = ma[4];
var f = ma[5];
var tx = x * a + y * c + e; return matrix.transformPoint(x, y, output);
var ty = x * b + y * d + f;
output.x = tx; // var ma = matrix.matrix;
output.y = ty;
// var a = matrix.matrix[0]; // var a = ma[0];
// var b = matrix.matrix[1]; // var b = ma[1];
// var c = matrix.matrix[2]; // var c = ma[2];
// var d = matrix.matrix[3]; // var d = ma[3];
// var tx = matrix.matrix[4]; // var e = ma[4];
// var ty = matrix.matrix[5]; // var f = ma[5];
// Apply inverse of the matrix // var tx = x * a + y * c + e;
// var ty = x * b + y * d + f;
// var id = 1 / ((a * d) + (c * -b)); // output.x = tx;
// output.y = ty;
// output.x = (d * id * x) + (-c * id * y) + (((ty * c) - (tx * d)) * id); // return output;
// output.y = (a * id * y) + (-b * id * x) + (((-ty * a) + (tx * b)) * id);
// x -= tx;
// y -= ty;
// output.x = (a * x) + (c * y) + tx;
// output.y = (b * x) + (d * y) + ty;
return output;
}; };
module.exports = GetTransformedPoint; module.exports = GetTransformedPoint;

View file

@ -3,22 +3,13 @@
var PointWithinGameObject = function (gameObject, x, y) var PointWithinGameObject = function (gameObject, x, y)
{ {
var width = gameObject.displayWidth; var width = gameObject.width;
var height = gameObject.displayHeight; var height = gameObject.height;
var x1 = -width * gameObject.originX; var x1 = -width * gameObject.originX;
var y1 = -height * gameObject.originY;
if (x >= x1 && x < x1 + width) return (x >= x1 && x < (x1 + width) && y >= y1 && y < (y1 + height));
{
var y1 = -height * gameObject.originY;
if (y >= y1 && y < y1 + height)
{
return true;
}
}
return false;
}; };
module.exports = PointWithinGameObject; module.exports = PointWithinGameObject;