Accurate Mouse Clicks on a 3D display

Started by
2 comments, last by NeilGD 17 years, 7 months ago
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
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".
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
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 :)
Thanks for the terminology (Agony) and the example (__filip)!

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

Neil

This topic is closed to new replies.

Advertisement