worldToScreenOffset function
- required CameraComponent camera,
- required Vector2 worldPosition,
- required Offset gameWidgetTopLeft,
Converts a Flame world-space worldPosition (e.g. a component's
position) into an on-screen Flutter Offset, via camera's own
localToGlobal (which already accounts for viewfinder pan/zoom and
viewport scaling — see camera_component.dart in the flame package).
gameWidgetTopLeft is the hosting GameWidget's top-left corner in the
same coordinate space Flutter's Positioned/Stack overlays use (from
renderBox.localToGlobal(Offset.zero)).
Known limitation: this assumes game.size maps 1:1 to the GameWidget's
own rendered logical size — true for a plain GameWidget with no custom
Viewport/aspect-ratio letterboxing (exactly RoyGame's setup). A
consumer using a FixedResolutionViewport or similar would need to
additionally account for that scaling ratio; out of scope here.
Implementation
Offset worldToScreenOffset({
required CameraComponent camera,
required Vector2 worldPosition,
required Offset gameWidgetTopLeft,
}) {
final global = camera.localToGlobal(worldPosition);
return gameWidgetTopLeft + Offset(global.x, global.y);
}