Screen Point to Screen Edge

Started by
1 comment, last by AussieBacom 10 years, 11 months ago

I am having trouble getting a poiny X,Y to the edge of my screen. This point is from a reversed screenspace co-ordinate, I am using it to point someone in the correct direction.

Here is a diagram of what I am trying to do:

problem.png

The co-ordinates of the 2D point work from the center of the screen to the point of the object.

Advertisement

In normalized device coordinates, where the center of the screen is at (0,0) and the screen ranges from -1 to 1 in both directions, the problem is trivial; you normalize the point with the absolute of the coordinate with largest magnitude.

norm = max(abs(object_pos.x), abs(object_pos.y));
target_pos.x /= norm;
target_pos.y /= norm;

You can also easily transform between your screen space coordinates by scaling and translation. To go to normalized device coordinates, subtract the coordinate of the center of the screen from the point to project and divide the x and y coordinates by half the width and height of the screen, respectively. To to to screen space coordinates you do the reverse; multiply by half the size of the window, and add the center of the screen.

object_pos -= center of screen;
object_pos.x /= .5*width;
object_pos.y /= .5*height;
 
// do the projection above here
 
target_pos.x *= .5*width;
target_pos.y *= .5*height;
target_pos += center of screen;

Only had to change the part for my depth duffer doing this when it was negative and not facing an object.

That worked well, thanks.

This topic is closed to new replies.

Advertisement