I am trying to make a simple game engine but I got stuck at a point when I tried to map a 2D mouse coordinate to a 3D coordinate in my world. The basic game has a plane that serves as the ground as it is going to be (hopefully with time) an RTS gameengine.
My problem is that I can’t really come up with anything useful. The plane is located at the 0,-100,-300 points and is about 1000×1000 in size. My camera is at 0,0,0 and the scene is rotated at 60 degreesto give the impression of a “bird eye” cam.
I was thinking about the trigonometric equations, using that I know the height of my camera and the angle and possibly calculating the distance will give me the right coords but this is just an idea.
Can somebody please give me some advice?
You can do it with a simple ray casting.
First, using gluUnProject, you can obtain the 3D world coordinates
mcorresponding to the 2D window coordinates of the mouse pointer.Given the camera position
e = (0, 0, 0), you can compute the mouse ray directionr = m - e.Now, given a point
pon the plane and the plane normaln, you can compute the intersection of the mouse ray with the plane.