Jump to content
  • Advertisement
Sign in to follow this  

Screen space to local - "denormalizing"

This topic is 2855 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

Hi there.

I have a simple game where I want to be able to drag tiles around on a board. The game is entirely 2D.

I have it set up so that it takes in a touch (or mouse click), sees if the click is with the boundary of a tile and then moves it accordingly.

However, the engine I use doesn't provide localised touch coordinates, only in screen coords. This means that if I click in the centre of the screen, instead of getting 0,0, I will get half the window size in length/height in pixels. Now, I expect this for screen coordinates, but what I don't know is how to convert this to local coordinates.

I have made a start by normalising the coords and shifting them so they are relative to the centre of the screen:

float width = zGetScreenWidth();
float height = zGetScreenHeight();
float halfwidth = width/2;
float halfheight = height/2;
float aspect = 1.3333f;

float x = touch_coords.x;
float y = touch_coords.y;

float dx = (x/halfwidth-1.0f)/aspect;
float dy = 1.0f-y/halfheight;

return newpos;

This works, when I click dead-centre in the middle of the screen I do indeed get 0,0 but because it is normalized, all my coords are clamped between 1 > -1. I'm not sure how to "up-scale" the coordinates so they match the local coordinates of my game board. Can anyone assist?

Share this post

Link to post
Share on other sites
Do you know the actual boundaries of your screen in local coordinates? Or finding them is the problem?
I'm not sure I understand...

If l,r,t,b are your left,right,top and bottom boundaries of your screen in local coordinates then you can transform the mouse position from screen coordinates to local coordinates in the following fashion :

newCoord.x = (x/screenWidth)*(r-l)+l
newCoord.y = (y/screenHeight)*(b-t)+t

Be careful about the integer division.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!