5
\$\begingroup\$

I assumed this was a straightforward problem but it has been plaguing me for days.

I am creating a 2D game with an orthographic camera. I am using a 3D camera rather than just hacking it because I want to support rotating, panning, and zooming. Unfortunately the math overwhelms me when I'm trying to figure out how to determine if a clicked point intersects a bounds (let's say rectangular) in the game.

I was under the impression that I could simply transform the screen point (the clicked point) by the inverse of the camera's View * Projection matrix to obtain the world coordinates of the clicked point. Unfortunately this is not the case at all; I get some point that seems to be in some completely different coordinate system.

So then as a sanity check I tried taking an arbitrary world point and transforming it by the camera's View*Projection matrices. Surely this should get me the corresponding screen point, but even that didn't work, and it is quickly shattering any illusion I had that I understood 3D coordinate systems and the math involved.

So, if I could form this into a question: How would I use my camera's state information (view and projection matrices, for instance) to transform a world point to a screen point, and vice versa? I hope the problem will be simpler since I'm using an orthographic camera and can make several assumptions from that.

I very much appreciate any help. If it makes a difference, I'm using XNA Game Studio.

\$\endgroup\$
1
  • \$\begingroup\$ I should add that the camera I'm using works perfectly well for the purposes of drawing. The rotation, panning and zooming works exactly as expected. I'm using SpriteBatch with a custom BasicEffect parameter and setting the BasicEffect's View and Projection matrices to the corresponding camera transforms. \$\endgroup\$
    – vargonian
    Commented Jan 4, 2011 at 9:58

1 Answer 1

2
\$\begingroup\$

Is you screenPoint between values (-1 ; 1)? If you are using ( 0 ; screenResolution ) it will not work.

//conversion from pixel values to unit cube (result of projection)
vec4 screenPoint = ( (clickedX / screenWidth)*2.0 - 1.0, (clickedY / screenHeight)*2.0 - 1.0, 0.5, 1.0);

screenPoint.Z defines distance from camera 0.0 = znear, 1.0 = zfar. As you are making 2D game, you dont care about it propably.

screenPoint.W have to be 1.0

\$\endgroup\$
1
  • \$\begingroup\$ Didn't it help? You can post example values i will try to help you \$\endgroup\$
    – Notabene
    Commented Jan 5, 2011 at 17:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .