I am creating a 2.5D game with the camera at a slight angle, like so:

enter image description here

The grid is the "playing field," and the positive x, y, and z directions are labelled.

I already know all the details about where the camera is, where the point on the grid that corresponds to the center of the screen is, and what the angle between the positive z axis and the direction the camera is pointing is.

What I want to do is be able to click on the screen, and figure out where on the grid I clicked. There are two ways that I have considered approaching this problem:

  • I could create a vector or ray of some sort that starts at the camera and goes in the direction the mouse is pointing. I could then solve the equation for the ray for x and y at z = 0. The problem with this is I don't know how computationally expensive it would be.

  • I could figure out the transformation matrix for something (I'm not sure exactly what it would have to be), then apply it to the [x y] position of the mouse. The problem with this is I have no idea where to start when finding the necessary transformation matrix.

Is one of these likely to be better than the other? Is there a different way that might be better? If I should use the transformation matrix option, how do I find that matrix?

  • 1
    \$\begingroup\$ What on earth makes you think about 5 basic arithmetic operations is going to be a performance concern? Neither would cause performance issues. Only difference is point 1 only works in this specific case where as point 2 can be made more generalizable. If you want to do point 2 I would reccomend starting here msdn.microsoft.com/en-us/library/bb203905.aspx. \$\endgroup\$ – ClassicThunder Nov 24 '14 at 0:50
  • \$\begingroup\$ Also the transformation matrix you are thinking of is the camera world matrix. This matrix turns the 3d representation of the world and makes it 2d on your screen. Basically for the link I mentioned its taking a ray and moving that from screen space to world space. \$\endgroup\$ – ClassicThunder Nov 24 '14 at 0:52
  • \$\begingroup\$ @ClassicThunder Now that I think about it, finding the ray would actually be easier than I assumed originally. Thanks for the link; it looks very useful! \$\endgroup\$ – The Guy with The Hat Nov 24 '14 at 2:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.