# Accurate Mouse Clicks on a 3D display

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

## 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 on other sites
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 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 on other sites
Thanks for the terminology (Agony) and the example (__filip)!

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

Neil

1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 14
• 9
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
632912
• Total Posts
3009197
• ### Who's Online (See full list)

There are no registered users currently online

×