# Mouse vector from viewer

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

## Recommended Posts

I am working on 3D mouse selection for my current engine and I have decided to use a ray from the viewer through the mouse coordinates to determine a hit detection vector. I think the method I'm implementing should work, but I'm going to throw it out there for anyone to correct me if I'm wrong. I have a viewer which is viewing a scene. The viewing vector originates from the center of the viewing space. I use the X,Y mouse coordinates to determine an angle from the current viewing vector. For example, I have a standard viewing angle/frustum (45 degrees), as I move the mouse around the screen, I use the X,Y mouse coordinates to determine the deviation angle (to a max of 45 degrees in any direction) from the center of the viewing space. So, for example, I am looking down 30 degrees at a scene, I move the mouse to the upper left corner of the screen (0,0 window coordinates), my mouse vector would be the current viewing angle of 30 degrees down (or -30 degrees) and 0 horizontal plus my deviation of 45 degrees vertically and -45 degrees horizontally which would be 15 degrees up and -45 degrees (or 315 degrees if keeping positive units) horizontally. Thus gives me the angle of the vector to use to caculate a hit vector for mouse selection. I'm still trying to get my head around this one, so if there are holes in my master plan, please shine a light at them. Thanks.

##### Share on other sites
Don't know if your method will work, but this problem is often referred (IIRC) as 'picking' and is covered by many tuts on internet. I'm sure that you could find something interesting. If you use opengl, try googling for picking+opengl.

##### Share on other sites
I don't really think that'll work ^^
But here is the method usually used :
   D3DVIEWPORT9	viewPort;   m_pd3dDevice->GetViewport(&viewPort);   D3DXMATRIXA16 id;   D3DXMatrixIdentity(&id);   D3DXVECTOR3 start((float)mousePos.x, (float)mousePos.y, 0.0f);   D3DXVECTOR3 end((float)mousePos.x, (float)mousePos.y, 1.0f);   D3DXVECTOR3 dir;   D3DXVec3Unproject(&start, &start, &viewPort, &projectionMatrix, &viewMatrix, &id);   D3DXVec3Unproject(&end, &end, &viewPort, &projectionMatrix, &viewMatrix, &id);   dir = end - start;   D3DXVec3Normalize(&dir, &dir);

dir is the normalized ray you can use to test collision with objects.

##### Share on other sites
Quote:
 Original post by paicI don't really think that'll work ^^But here is the method usually used :*** Source Snippet Removed ***dir is the normalized ray you can use to test collision with objects.

Thanks, that makes sense. I've been looking through articles over the past couple days on picking and selecting, but most of them cover the OpenGL selection mode, or they just mention rays and that you need to use gluUnproject(), but don't really explain how you should use it. I think you summed up everything I didn't quite understand in 11 lines of the API I'm not even using. Thanks again!

• 10
• 16
• 14
• 18
• 15