Adds constraint for rotating a node towards another node

This commit is contained in:
Viktor Lidholt
2015-08-25 14:47:56 -07:00
parent dc3159316e
commit dcd17aefd6

View File

@@ -18,6 +18,7 @@ double _dampenRotation(double src, double dst, double dampening) {
class ConstraintRotationToMovement {
ConstraintRotationToMovement([this.baseRotation = 0.0, this.dampening]);
final double dampening;
final double baseRotation;
@@ -41,3 +42,29 @@ class ConstraintRotationToMovement {
node.rotation = _dampenRotation(node.rotation, target, dampening);
}
}
class ConstraintRotationToNode {
ConstraintRotationToNode(this.targetNode, [this.baseRotation, this.dampening]);
final Node targetNode;
final double baseRotation;
final double dampening;
void constrain(Node node, double dt) {
Offset offset;
if (targetNode.parent == node.parent) {
offset = targetNode.position - node.position;
} else {
offset = node.convertPointToBoxSpace(Point.origin)
- targetNode.convertPointToBoxSpace(Point.origin);
}
double target = degrees(GameMath.atan2(offset.dy, offset.dx)) + baseRotation;
if (dampening == null)
node.rotation = target;
else
node.rotation = _dampenRotation(node.rotation, target, dampening);
}
}