update method
Implementation
void update(double dt) {
if (_dragPosition == null ||
_rectBackgroundDirection == null ||
_rect == null) return;
if (_dragging) {
double radAngle = atan2(
_dragPosition!.dy - _rectBackgroundDirection!.center.dy,
_dragPosition!.dx - _rectBackgroundDirection!.center.dx,
);
// Distance between the center of joystick background & drag position
Vector2 centerPosition = _rectBackgroundDirection!.center.toVector2();
Vector2 dragPosition = _dragPosition!.toVector2();
double dist = centerPosition.distanceTo(dragPosition);
// The maximum distance for the knob position the edge of
// the background + half of its own size. The knob can wander in the
// background image, but not outside.
dist = min(dist, _tileSize);
// Calculation the knob position
double nextX = dist * cos(radAngle);
double nextY = dist * sin(radAngle);
Offset nextPoint = Offset(nextX, nextY);
Offset diff = Offset(
_rectBackgroundDirection!.center.dx + nextPoint.dx,
_rectBackgroundDirection!.center.dy + nextPoint.dy,
) -
_rect!.center;
_rect = _rect!.shift(diff);
double intensity = dist / _tileSize;
_joystickController?.joystickAction(
JoystickActionEvent(
id: actionId,
event: ActionEvent.MOVE,
intensity: intensity,
radAngle: radAngle,
),
);
} else {
Offset diff = _dragPosition! - _rect!.center;
_rect = _rect!.shift(diff);
}
}