Sign in to follow this  

Screen to world transformation breaks down when maximizing window

Recommended Posts

I hastily put together a formula for transalting screen coordinates to world coordinates using orthographic projection. The formula is not mathemaitcally exact, but it works in most cases. It stems from visually estimating the width and height ratio with world coordinates that are ASSUMED to be aligned with the edges of the window. I zoom in, I translate, I change the window size, and it all works fine. Unless I MAXIMIZE the window! I mean, it's just a change in size, all sizes work, even manually extending the window borders to the edges of the screen works, but specifically the act of getting a new size after a maximize window operation makes the coordinate translation procedure show it's rough edges, and the error is maximized. Here's a diagram of how the procedure works.

Share this post

Link to post
Share on other sites
This is the formula I use for screen-to-world conversion:

// Convert window coordinates into camera coordinates
float CameraX, CameraY;
CameraX = ((float)mouse.x / window.Width()) * Viewport.Width + Viewport.Left;
CameraY = ((float)mouse.y / window.Height()) * Viewport.Height + Viewport.Bottom;

// Convert camera coordinates into world coordinates
float WorldX, WorldY;
WorldX = CameraX + Camera.Position.X;
WorldY = CameraY + Camera.Position.Y;

The zoom factor is already taken into account by the camera, which scales the viewport appropriately as the zoom changes. Other than that I'm not sure why you need to estimate anything. Your view frustum should tell you everything you need to know about the world coordinates that are visible.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this