# Ray Intersecting with bounding box?

Can anyone either give me a small example on how to do this, or give me a link to a turtorial? I have checked out nehe, gametutorials, and a few other sites and haven’t found anything…

What I want to do is find get a bounding box around some objects on screen and a ray coming out of the cursor all the way into the clipping plane (or infinity) and test to see what object the mouse is on.

hello,

I could—in theory—give you my code to do this, but you wouldn’t exactly learn much about how to design algorithms. So, what I propose instead is a guided way of showing you how to figure out the answer for yourself. Everyone should know how to divide and conquer algorithms.

What I propose you do is work out some pseudo code to implement what you’re after. A simple first step is

``````Psuedo code:

1. find the line parameters
2. find the intersection of the line [1] with the scene
``````

Now, can you elaborate on that framework? Give it a go, and post your attempt. If you include assumptions about steps you think might be possible, but not sure, then that’s ok too.

give it a shot

cheers
John

okay, I now have a line being drawn from my cursor to 0,0,FarClipping Plane, except that the Y is reversed…

case WM_MOUSEMOVE:
{
cm.iPosX = LOWORD(lParam);
cm.iPosY = HIWORD(lParam);
//Make assignments to fRayX and fRayY here…
GLdouble dProjMatrix[16];
GLdouble dModMatrix[16];
GLint iViewPort[4];

``````        glGetDoublev(GL_PROJECTION_MATRIX,dProjMatrix);
glGetDoublev(GL_MODELVIEW_MATRIX,dModMatrix);
glGetIntegerv(GL_VIEWPORT,iViewPort);

gluUnProject(cm.iPosX,cm.iPosY,0,dProjMatrix,dModMatrix,iViewPort,&fRayX,&fRayY,&fRayZ);

//Change the window settings
sprintf(MouseString,"DarkLib Line Intersection Test - %d-%d",cm.iPosX,cm.iPosY);
SetWindowText(hwnd,MouseString);
break;
}
``````

////////////////////