Original Post
I''m trying to get the users mouse click to see if he selected a mesh. I know that I need to project a ray into the world to achieve this. But i''m having trouble doing just that.
This is the code i''m using:
bool Frost_Engine::Intersect_Mesh(void *pMesh, Frost_Vector &start, Frost_Vector &end)
{
BOOL isHit;
float dist, u, v;
DWORD hitFace;
D3DXVECTOR3 vecDir, vecDif, vecStart = Convert_FrostVector(start), vecEnd = Convert_FrostVector(end);
Frost_Mesh *fMesh = (Frost_Mesh*)pMesh;
vecDif = D3DXVECTOR3(vecEnd.x - vecStart.x, vecEnd.y - vecStart.y, vecEnd.z - vecStart.z);
D3DXVec3Normalize(&vecDir, &vecDif);
D3DXIntersect(fMesh->mesh, &vecStart, &vecDir, &isHit, &hitFace, &u, &v, &dist, NULL, NULL);
if(isHit) return true;
else return false;
}
// Convert a screen coord to world coord.
void Frost_Engine::Screen_To_World(float x, float y, float z, Frost_Vector &vector)
{
D3DXVECTOR3 tmp;
D3DXVec3Unproject(&tmp, &D3DXVECTOR3(x, y, z), &viewport, &proj_matrix, &view_matrix, &camera_matrix);
vector.x = tmp.x;
vector.y = tmp.y;
vector.z = tmp.z;
}
Frost_Vector src, dest; frost->Screen_To_World(mousePos.x, mousePos.y, 0.0f, src); frost->Screen_To_World(mousePos.x, mousePos.y, 1.0f, dest); if(frost->Intersect_Mesh(mymesh, src, dest)) MessageBox(HWND_DESKTOP, "um....works?", "World", MB_OK);Frost_Vector is just a standard D3DXVECTOR3 struct. I first convert the mouse position into 2 world vectors, one source and one destination. I then try to run them through the intersect function. Can anyone tell me what i''m doing wrong?