Archived

This topic is now archived and is closed to further replies.

DarkZoulz

Intersect does not seem to be precise..?

Recommended Posts

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.)

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
YAY! It works You have no idea how long i''ve been trying to get this to work, and it turns out to be something as simple as changing the window size. =)

Thank you very much for your help!

Share this post


Link to post
Share on other sites