Jump to content
  • Advertisement
Sign in to follow this  
MrBob

mapping mouse clicks to a 3d terrain

This topic is 4310 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am in the process of trying to make a turn-based game along the lines of the old xcom games. Right now I have a simple tiled, 3d world that I can rotate and zoom in and out of in Opengl. The problem is that since I am rotating the playing field i am not sure how to find out which tile I am picking. I look through some old forum posts, but didn't come across anything talking about this (it is a possibility that I just missed it). Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Typically you unproject your mouse point from 2D screen coordinates to a 3D ray, and then intersect this ray with the terrain or other objects. This is because you lose the depth dimension in 2D. NeHe has an article explaining the process. However, only pay attention to everything up to glReadPixels. Get rid of that completely. Replace the single call to glUnProject with the two following calls in his GetOGLPos function:

float start[3], end[3];

gluUnProject(winX, winY, 0.0f, modelview, projection, viewport, &start[0], &start[1], &start[2]);
gluUnProject(winX, winY, 1.0f, modelview, projection, viewport, &end[0], &end[1], &end[2]);


You now have a ray between the vectors described by start and end that you can intersect with whatever you want. Believe me, this is much faster and more flexible than reading back depth information from the hardware.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!