Intersect does not seem to be precise..?
I have a function for testing intersections between a ray and faces on a mesh (D3DXIntersect), but it does not seem to be very precise? It seems as if there is some kind of offset. Can this be because of some math error? I''m trying to project a ray from the mouse click, but the source vector always seem to be in the exact spot of the camera view. Could that have something to do with it?
(I''m new to 3D programming.)
It is as if the mesh was positioned a bit longer in on the z-axis than it really is. If I click a bit infront of the mesh I get hits, but not at the back of the mesh.
Frost_Mesh *mesh = (Frost_Mesh*)pMesh; D3DXMATRIXA16 mxProj, m; float x, y, z; D3DXVECTOR3 vRayStart, vRayEnd; mxProj = cam->Get_Projection_Matrix(); x = (((2.0f * (float)ray.x) / 800.0f) - 1.0f) / mxProj._11; y = -(((2.0f * (float)ray.y) / 600.0f) - 1.0f) / mxProj._22; z = 1.0f; m = cam->Get_World_Matrix() * cam->Get_View_Matrix(); D3DXMatrixInverse(&m, NULL, &m); vRayEnd.x = (x * m._11) + (y * m._21) + (z * m._31); vRayEnd.y = (x * m._12) + (y * m._22) + (z * m._32); vRayEnd.z = (x * m._13) + (y * m._23) + (z * m._33); vRayStart.x = m._41; vRayStart.y = m._42; vRayStart.z = m._43; // check mesh for intersection BOOL bHit; DWORD dwFace; FLOAT fBary1, fBary2, fDist; D3DXIntersect(mesh->mesh, &vRayStart, &vRayEnd, &bHit, &dwFace, &fBary1, &fBary2, &fDist, NULL, NULL); return bHit;
YOu have the backbuffer width as 800 and height as 600 - I bet it is not That will be what you set the window to but because of borders and things the real size will be less. After creating the device call GetViewport to see what the backbuffer size really is. This small difference will probably explain the innacuracy.
um, shouldn't you be adding the start vector onto the end vector as well?. Your calculations for the end vector do not seem to take into account the actual position of the camera in space. The position of the camera will alter the end vector, but you do not handle this in your code. Unless ofcourse you are treating the end vector (vRayEnd) as direction vector and not as the end point of your line.
[edited by - SoulSpectre on May 20, 2004 6:57:09 AM]
[edited by - SoulSpectre on May 20, 2004 6:57:09 AM]
I think you are right, because if I return the clicked 2D coordinates when clicking the bottom right corner of the window I get slightly less than 800x600. But the GetViewport() function only returns 800x600. How can I get the actual backbuffer size?
(vRayEnd is a bad name, because it is the direction of the ray. I should change it)
(vRayEnd is a bad name, because it is the direction of the ray. I should change it)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement