Sign in to follow this  

Accurate Mouse Clicks on a 3D display

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

This topic is 4111 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this