worldToScreen method

Offset worldToScreen(
  1. Offset worldPos
)

Convert world coordinates to screen coordinates

Implementation

Offset worldToScreen(Offset worldPos) {
  // Step 1: Subtract camera position (translate to camera space)
  final relative = worldPos - position;

  // Step 2: Apply rotation
  final cos = math.cos(rotation);
  final sin = math.sin(rotation);
  final rotated = Offset(
    relative.dx * cos - relative.dy * sin,
    relative.dx * sin + relative.dy * cos,
  );

  // Step 3: Apply zoom
  final scaled = rotated * zoom;

  // Step 4: Adjust for viewport center (origin is at center of screen)
  return scaled + Offset(viewportSize.width / 2, viewportSize.height / 2);
}