Picking 3D objects with mouse give false positive

Started by
10 comments, last by Endurion 13 years, 1 month ago
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)
{
...
}
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.
It's not a bug... it's a feature!
I don't know... If the pointer is at 1 cm from the sphere it says me that I hit it :blink:
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?

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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

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?

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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

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
They hated on Jeezus, so you think I give a f***?!
Do you transform your mouse coordinates from screen to client? If you don't, this might cause a small offset.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

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

This topic is closed to new replies.

Advertisement