Commit dcd17aef authored by Viktor Lidholt's avatar Viktor Lidholt

Adds constraint for rotating a node towards another node

parent dc315931
......@@ -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);
}
}
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