Skip to content

Conversation

@AristurtleDev
Copy link
Collaborator

Description

Fixes coordinate transformation methods (WorldToScreen and ScreenToWorld) in OrthographicCamera to correctly handle viewport offsets based on the viewport adapter type. The previous implementation unconditionally applied viewport offset adjustments, which caused incorrect transformations when using non-scaling viewport adapters like DefaultViewportAdapter.

The root cause was that mouse input from MouseState is always in window coordinates. For non-scaling adapters, window coordinates map 1:1 to virtual coordinates, so viewport offset should not affect the transformation. For scaling adapters, the viewport offset defines where the scaled virtual coordinate system is positioned within the window, requiring offset adjustment before scale transformation.

Related Issues/Tickets

Changes Made

Bug Fix:

  • Modified WorldToScreen and ScreenToWorld in OrthographicCamera to conditionally apply viewport offset adjustments only for ScalingViewportAdapter (and its subclasses like BoxingViewportAdapter)
  • For DefaultViewportAdapter and other non-scaling adapters, viewport offset is now correctly ignored as it represents only the rendering position, not a coordinate transformation
  • For ScalingViewportAdapter, viewport offset is properly applied to convert between window coordinates and viewport coordinates before scale transformations

Test Coverage:

  • Added ScreenToWorld_WithNonZeroViewportOrigin_TransformsCorrectly to verify behavior with non-zero viewport origins
  • Added WorldToScreen_RoundTrip_WithNonZeroViewportOrigin_ReturnsOriginalPosition to ensure round-trip consistency
  • Added ScreenToWorld_WithBoxingViewportAdapter_TransformsCorrectly to verify correct behavior with scaling viewport adapters
  • Added WorldToScreen_RoundTrip_WithBoxingViewportAdapter_ReturnsOriginalPosition to ensure round-trip consistency with boxing adapters

Checklist

Please read and check the following items. Pull requests will not be reviewed if all items are not checked.

  • I have verified that there are no existing pull requests that would overlap with this pull request.
  • I have verified that I am following the guidelines as outlined in this project's contribution policy
  • I have verified that this pull request adheres to this project's code of conduct.
  • I have written a descriptive title for this pull request.
  • I have provided appropriate test coverage were applicable.

Originally a fix was implemented in #124 to resolve this issue for the BoxingViewportAdapter, which broke the methods for non-scaling adapters like the DefaultViewportAdapter.

To resolve this, we need to conditionally check if a scaling adapter is being used and only then offset the position by the viewport when translating.
@AristurtleDev AristurtleDev self-assigned this Nov 5, 2025
@AristurtleDev AristurtleDev added Status: Completed Work finished Type: Bug Something is broken Priority: Critical Drop everything, fix now Area: API API Platform: Cross-platform All platforms labels Nov 5, 2025
@AristurtleDev AristurtleDev merged commit 6e44247 into develop Nov 5, 2025
1 check passed
@AristurtleDev AristurtleDev deleted the issue/orthographic-camera-non-zero-viewport-793 branch November 5, 2025 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: API API Platform: Cross-platform All platforms Priority: Critical Drop everything, fix now Status: Completed Work finished Type: Bug Something is broken

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OrthographicCamera.ScreenToWorld and similar methods interact poorly with window not at (0,0)

2 participants