简体   繁体   中英

2D to 3D map OpenGL

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 1000x1000 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 m corresponding to the 2D window coordinates of the mouse pointer.

Given the camera position e = (0, 0, 0) , you can compute the mouse ray direction r = m - e .

Now, given a point p on the plane and the plane normal n , you can compute the intersection of the mouse ray with the plane .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM