D3DXVECTOR3 ForMouse;
ForMouse = MGM_MakeRay(0.78f,4/3,g_Mouse.GetMousePos().x,g_Mouse.GetMousePos().y,100);
float myc[3] = {0,0,0};
float myo[3];
g_Camera.GetPosition(myo[0],myo[1],myo[2]);
float myd[3] = {ForMouse.x,ForMouse.y,ForMouse.z};
float junk[3];
if ( SphereRayCollision(myc,10,myo,myd,junk) ) {
onsun = true;
} else {
onsun = false;
}
And here is the MGM_MakeRay function:
D3DXVECTOR3 MGM_MakeRay(float FOV,float AspectRatio,WORD MouseX,WORD MouseY,float range)
{
D3DXVECTOR3 LineEnd,CameraSpacePos;
float NMouseX,NMouseY,det;
D3DXMATRIX matView;
NMouseX=1.0 - 2.0*MouseX/640;
NMouseY=1.0 - 2.0*MouseY/480;
//is FOV in radians
CameraSpacePos.y=(NMouseY*tan(FOV/2.0));
CameraSpacePos.x=((-NMouseX*AspectRatio)*tan(FOV/2.0));
LineEnd.x=range*CameraSpacePos.x;
LineEnd.y=range*CameraSpacePos.y;
LineEnd.z=range;
g_pDevice->GetTransform(D3DTS_VIEW,(D3DMATRIX *)&matView);
D3DXMatrixInverse(&matView,&det,&matView);
D3DXVec3TransformCoord(&LineEnd,&LineEnd,&matView);
return LineEnd;
}
And here is the definition of SphereRayCollision: (point is the point of intersection it finds)
bool SphereRayCollision(float center[3], float radius,
float origin[3], float direction[3],
float point[3]);
Thanks (I'm using D3D 8 if it matters).
EDIT: Got rid of those lines (see next post).
[edited by - rjahrman on August 6, 2002 3:03:10 PM]
[edited by - rjahrman on August 6, 2002 3:50:26 PM]