Jump to content
  • Advertisement
Sign in to follow this  
Etherstar

Mouse interaction with objects in 3d

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

Back when I was doing work in 2d, it was quite a bit easier to find out what objects on screen the user was clicking on. Now that I'm doing 3d things, this simple task just got a lot harder. Basically my question is how do I detect what the user is clicking on in the 3d space of my game when my mouse coordinates are only in 2d. I'm sure this is answered fairly easily considering most games out now have users interacting with 3d environments. I'm sure I could figure it out if some one could point me in the right direction. Anyone know the simple answer or perhaps would be willing to point me in the direction of where I could find the answer?

Share this post


Link to post
Share on other sites
Advertisement
You have to convert the screen coordinates into world coordinates and cast a ray (or something like that...) and whatever it intersects is what you've clicked on.

If you're using Open GL, use gluUnProject() when the render mode is GL_SELECT and pick the objects... There's tutorials out there somewhere (just Google).

If you're not using Open GL, I'm not sure how you'd go about it, but I'm sure Google'd come in handy.

Share this post


Link to post
Share on other sites
I think the keyword you're looking for is 'picking'. A google search turned up this link, among others. I think OpenGL has some auxiliary functionality to support picking in screenspace, but I don't know much about this feature. An alternative is to do it in world space, which basically involves two steps: creating a ray that originates at the camera and passes through the cursor position, and intersecting this ray with the objects in the scene.

I think the 'creating the ray' part is discussed in the link above. As for intersecting with the objects in the scene, there are many options - overall that part falls into the category of raytracing. If exact accuracy is not required, you could intersect the ray with bounding spheres (easy), AABBs or OBBs (using the separating axis theorem), or some other simple bounding volume such as a capsule. If you need exact results you can perform a ray-mesh intersection on the model itself.

Share this post


Link to post
Share on other sites
The main hurdle is converting from screen co-ordinates to world co-ordinates. Some API's support the direct conversion. So you would not have to break your head over it. But if you want to do it manually, then it requires some thought. I mapped my screen co-ordinates to my camera space and then converted into world co-ordinates. Then I used to pass a ray into the scene and detect for sphere collision. All my objects in the scene has a bounding sphere. This obviously does not give you accuracy, but I did not need that kind of accuracy.

Share this post


Link to post
Share on other sites
Quote:
Original post by sriniatig
The main hurdle is converting from screen co-ordinates to world co-ordinates. Some API's support the direct conversion. So you would not have to break your head over it. But if you want to do it manually, then it requires some thought. I mapped my screen co-ordinates to my camera space and then converted into world co-ordinates. Then I used to pass a ray into the scene and detect for sphere collision. All my objects in the scene has a bounding sphere. This obviously does not give you accuracy, but I did not need that kind of accuracy.


I also use this method. Trace a ray along the (converted) 3d mouse coordinate and the camera's view coord. Then look for collision between the ray and your objects.

If your objects can be one in front of another (not my case) you'll need to check wich one is closer to the camera.

Share this post


Link to post
Share on other sites
I use a ray - bounding box/sphere collision. From some position and dir (camera typically) fire off a ray of some length or infinitely long and see what you hit. It's kind of a pain in the butt and took me a while to find a good algorithm.

Share this post


Link to post
Share on other sites
Thanks for the replies so far. Just FYI I'm using managed directX and c#. AFter some digging I found some suggestions for using Project and Unproject. Unfortunately the managed documentation regarding these guys is barely worth even calling documentation at this point. I've come up bingo on tutorials with managed directX so I might try the ray-bounding box intersect method. The interesection test will be easy, but building the ray will be a bit complicated.

In my mind I'm thinking I will base the rays origin from the camera position and calculate its orientation by mapping screen coordinates to the near plane of my view frustrum. Am I going in the right direction with my reasoning?

Share this post


Link to post
Share on other sites
Quote:
Original post by Etherstar
In my mind I'm thinking I will base the rays origin from the camera position and calculate its orientation by mapping screen coordinates to the near plane of my view frustrum. Am I going in the right direction with my reasoning?


In OpenGL the camera has location coords and a view coords (the place the camera is focusing at), I'm guessing DirectX handles the camera the same way.

So, viewXYZ - MouseXYZ == Direction.

With the direction vector and the mouse coords you have a ray to test collision with.

Share this post


Link to post
Share on other sites
Quote:
Original post by owl

In OpenGL the camera has location coords and a view coords (the place the camera is focusing at), I'm guessing DirectX handles the camera the same way.

So, viewXYZ - MouseXYZ == Direction.

With the direction vector and the mouse coords you have a ray to test collision with.


In directX the camera also has location and "point at" coordinates. My camera is a very simple 60 degree isometric view. The camera travels along a single x-y plane so as the user moves it I only have to update both the camera's position and point at z values (my point at is a simple precomputed normal).

if viewXYZ - MouseXYZ == Direction, how do I get my mouse's xyz coords? Are these numbers generated based on screen space? If so that would only live me with an x,y coordinate. Hope I'm not starting to get to annoying with my ignorance.

Share this post


Link to post
Share on other sites
Quote:
Original post by Etherstar
how do I get my mouse's xyz coords?


This or this may help.

Or keep trying with this google search.

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!