Adds constraint for rotating a node towards another node
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user