Jump to content
  • Advertisement
Sign in to follow this  
vecchia

Picking 3D objects with mouse give false positive

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

Hi all,
I found over the web the code below to pick up 3D Objects. I actually have a 3D model with spheres that is renderized at the centre of the screen and I should be able to pick up a single sphere and change his texture. The problem is that it doesn't work very well, I have a lot of false positive. For example, sometimes I click out of the sphere and I have a positive hit. Why? Could it be a 3D model problem?

D3DXVECTOR3 rayOrigin;
D3DXVECTOR3 rayDirection;
D3DXMATRIX matrixInverse;
BOOL hit;

float xAngle = (((2.0f * mousePosition.x) / this->windowWidth) - 1.0f) / this->matProjection(0, 0);
float yAngle = (((-2.0f * mousePosition.y) / this->windowHeight) + 1.0f) / this->matProjection(1, 1);
rayOrigin = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
rayDirection = D3DXVECTOR3(xAngle, yAngle, 1.0f);

D3DXMatrixInverse(&matrixInverse, NULL, &(this->worldMatrix * this->viewMatrix));
D3DXVec3TransformCoord(&rayOrigin, &rayOrigin, &matrixInverse);
D3DXVec3TransformNormal(&rayDirection, &rayDirection, &matrixInverse);
D3DXVec3Normalize(&rayDirection, &rayDirection);
D3DXIntersect(this->mesh, &rayOrigin, &rayDirection, &hit, NULL, NULL, NULL, NULL, NULL, NULL);

if (hit)
{
...
}

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure but wouldn't you just want to be applying the inverse world matrix to your ray data and not the inverse of a combined world-view matrix? The mesh data shouldn't be in view space at this point as far as I know.

Share this post


Link to post
Share on other sites
You may want to compare your calcs for rayOrigin and rayDirection with what you get from D3DXVec3Unproject. It appears the results you're getting are due to not accounting for the mouse position in the rayOrigin calculation. I.e., rayOrigin is independent of the mouse position.

You say you found the code somewhere. Are you sure the code you posted is intended to give you the results you're expecting?

Share this post


Link to post
Share on other sites

yes the tutorial is about "picking 3d objects"... It works with their teapot example :(

Just because the tutorial is about picking, doesn't mean you're using the code in the same way as the example.

You use a world matrix in your calcs. Is that the world matrix of the mesh you're trying to pick? How is it done in the example code you're using?

And, as suggested, have you compared your results to the results from D3DXVec3Unproject?

Share this post


Link to post
Share on other sites
I changed the code to use GetCursorPos and not the lParam of the WndProc message and it seems to work better, the precision is not really 100% but it's near the perfect... still working on it, anyway that was the problem

Share this post


Link to post
Share on other sites

I changed the code to use GetCursorPos and not the lParam of the WndProc message and it seems to work better, the precision is not really 100% but it's near the perfect... still working on it, anyway that was the problem


lParam is simply used to pass extra data with messages. Do you know what the data was? That could help you figure things out. ohmy.gif

Share this post


Link to post
Share on other sites
lParam is simply used to pass extra data with messages. Do you know what the data was? That could help you figure things out. ohmy.gif

I was using the WM_LBUTTONUP message to find the mouse coordinates. This is what was on MSDN about the lParam argument:

The low-order word specifies the x-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area.
The high-order word specifies the y-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area.[/quote]

Do you transform your mouse coordinates from screen to client? If you don't, this might cause a small offset.

now I'm doing it and the offset is really small, If i click out ot fhe sphere the hit isn't detected, if I hit the sphere the hit is detected and If I hit inside the sphere but really near the border the hit isn't detected. Anyway a big steep away from before

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!