Jump to content
  • Advertisement
Sign in to follow this  
NeilGD

Accurate Mouse Clicks on a 3D display

This topic is 4325 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

My game is currently running on a flat 3D surface that can be rotated around as the user chooses. The surface is currently set up in a grid format (so similar to a 2D game except you get to rotate and choose your viewing angle). I want to be able to map a mouseclick on the screen back to the tile being displayed at that position on the screen. Can anyone direct me to any resources that will help me accomplish this? I'm tearing my hair out right now trying to get it to work (rather unsuccesfully so far!) Thanks! Neil

Share this post


Link to post
Share on other sites
Advertisement
While I've never gotten far enough in any project to implement it myself, I can at least inform you that terminology that will likely get you what you need would be "mouse picking".

Share this post


Link to post
Share on other sites
What you want to do is unproject the clicked position (which is a 2d cordinate) back to 3d at a given distance. Do this 2 times, once for z=0 and once for z=1. If you use OpenGL there's a function called gluUnproject if you use DirectX there's a function called D3DXVec3Unproject (I think). This gives you 2 direction vectors, one for the near clipping plane (z=0) and one for the far clipping plane (z=1). Subtract the far vector from the near vector to get the relative direction. This is a direction vector pointing from the cameras position in the direction the user clicked. Use linear algebra to solve where this line hits the surface (assuming the surface is flat of course):

Direction vector: d
Camera position: p

Assuming the surface has normal (x=0,y=1,z=0) and is located h units above y=0

p.y+t*d.y=h
=> t=(h-p.y)/d.y

The point on the surface is then p+d*t

I hope i haven't made to many mistakes here :)

Share this post


Link to post
Share on other sites
Thanks for the terminology (Agony) and the example (__filip)!

I'll post back once I have any results! :)

Neil

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!