Jump to content
  • Advertisement
Sign in to follow this  
Journey

Screen to world transformation breaks down when maximizing window

This topic is 4500 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!