Commit ac2a8a66 authored by Viktor Lidholt's avatar Viktor Lidholt

sprite nodes now caches inverse transforms

parent 5d0c090d
...@@ -24,6 +24,7 @@ class Node { ...@@ -24,6 +24,7 @@ class Node {
double _rotation = 0.0; double _rotation = 0.0;
Matrix4 _transformMatrix = new Matrix4.identity(); Matrix4 _transformMatrix = new Matrix4.identity();
Matrix4 _transformMatrixInverse;
Matrix4 _transformMatrixNodeToBox; Matrix4 _transformMatrixNodeToBox;
Matrix4 _transformMatrixBoxToNode; Matrix4 _transformMatrixBoxToNode;
...@@ -246,9 +247,7 @@ class Node { ...@@ -246,9 +247,7 @@ class Node {
return position; return position;
} else if (physicsParent is PhysicsGroup) { } else if (physicsParent is PhysicsGroup) {
// Transform the position // Transform the position
Matrix4 inverseTransform = new Matrix4.copy(physicsParent.transformMatrix); Vector4 parentPos = physicsParent._inverseMatrix().transform(new Vector4(position.x, position.y, 0.0, 1.0));
inverseTransform.invert();
Vector4 parentPos = inverseTransform.transform(new Vector4(position.x, position.y, 0.0, 1.0));
Point newPos = new Point(parentPos.x, parentPos.y); Point newPos = new Point(parentPos.x, parentPos.y);
return _positionToPhysics(newPos, physicsParent.parent); return _positionToPhysics(newPos, physicsParent.parent);
} else { } else {
...@@ -531,6 +530,7 @@ class Node { ...@@ -531,6 +530,7 @@ class Node {
/// changes that affects the matrix. /// changes that affects the matrix.
void invalidateTransformMatrix() { void invalidateTransformMatrix() {
_transformMatrix = null; _transformMatrix = null;
_transformMatrixInverse = null;
_invalidateToBoxTransformMatrix(); _invalidateToBoxTransformMatrix();
} }
...@@ -575,6 +575,14 @@ class Node { ...@@ -575,6 +575,14 @@ class Node {
return _transformMatrixBoxToNode; return _transformMatrixBoxToNode;
} }
Matrix4 _inverseMatrix() {
if (_transformMatrixInverse == null) {
_transformMatrixInverse = new Matrix4.copy(transformMatrix);
_transformMatrixInverse.invert();
}
return _transformMatrixInverse;
}
/// Converts a point from the coordinate system of the [SpriteBox] to the local coordinate system of the node. /// Converts a point from the coordinate system of the [SpriteBox] to the local coordinate system of the node.
/// ///
/// This method is particularly useful when handling pointer events and need the pointers position in a local /// This method is particularly useful when handling pointer events and need the pointers position in a local
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment