Ray Casting Issues

Started by
-1 comments, last by Banded 12 years ago
Hey guys so I have been having some issues with my game recently regarding ray casting. I have been working on an rts game and now I have been trying to be able to use my mouse inorder to select units and then tell them where to go. So after researching this I cam to this code.
// This function will find 2 points in world space that are on the line into the screen defined by screen-space( ie. window-space ) point (x,y)
double mvmatrix[16];
double projmatrix[16];
int viewport[4];
double dX, dY, dZ, dClickY; // glUnProject uses doubles, but I'm using floats for these 3D vectors

glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
dClickY = double (stateInfo.windowHeight - y); // OpenGL renders with (0,0) on bottom, mouse reports with (0,0) on top

gluUnProject ((double) x, dClickY, 0.0, mvmatrix, projmatrix, viewport, &dX, &dY, &dZ);
Vector3f ClickRayP1 = Vector3f ( (float) dX, (float) dY, (float) dZ );
gluUnProject ((double) x, dClickY, 1.0, mvmatrix, projmatrix, viewport, &dX, &dY, &dZ);
Vector3f ClickRayP2 = Vector3f ( (float) dX, (float) dY, (float) dZ );
std::cout<<"Ray 1 ("<<ClickRayP1.x<<","<<ClickRayP1.y<<","<<ClickRayP1.z<<")"<<std::endl;
std::cout<<"Ray 2 ("<<ClickRayP2.x<<","<<ClickRayP2.y<<","<<ClickRayP2.z<<")"<<std::endl;



So I understand what is going on here but I am having some issues. To start, the matrixes remain the same even after I move the camera around, so the points I get back are the same when I move the camera, and I cant figure out why. Further more, everything in my game is drawn on the XZ plane, with Y = 0, so how do I get the Z value information for this plane from clicking. I dont know if its useful but I will include the display function too cause it shows the matrix stuff.

void DisplayCallbackFunction(void)
{
/*clear the screen*/
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
drawing3D();
/*draw the game*/
theGame.draw();
GLuint texture;
//draw in 2d
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0, WINDOW_SCREEN_WIDTH,0.0, WINDOW_SCREEN_HEIGHT, -1.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_CULL_FACE);
glClear(GL_DEPTH_BUFFER_BIT);
theGame.currentHUD->draw();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);

/*display buffer*/
glutSwapBuffers();
}


any help will be greatly appreciated as this project is approaching its due date.

This topic is closed to new replies.

Advertisement