# I have my mouse coords converted to world coords. Now how do I detect objects with it? (C++, OpenGL, FreeGlut)

I found a way to convert my mouse coordinates to world coordinates that I can easily understand but now I need to know how to use it to check if the mouse is currently over any of my rendered objects.

Here is my mouse click code that converts the mouse coordinates to screen coordinates when the left mouse button is clicked.

code:

void mouseClick( int button, int state, int x, int y ){

//if any mouse button was pressed
if (button == GLUT_LEFT && state == GLUT_DOWN) {

GLint viewport[4]; //hold the viewport info
GLdouble modelview[16]; //hold the modelview info
GLdouble projection[16]; //hold the projection matrix info
GLfloat winX, winY, winZ; //hold screen x,y,z coordinates
GLdouble worldX, worldY, worldZ; //hold world x,y,z coordinates

glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); //get the modelview info
glGetDoublev( GL_PROJECTION_MATRIX, projection ); //get the projection matrix info
glGetIntegerv( GL_VIEWPORT, viewport ); //get the viewport info

winX = (float)x;
winY = (float)viewport[3] - (float)y;

//get the world coordinates from the screen coordinates
gluUnProject( winX, winY, 0.0, modelview, projection, viewport, &worldX, &worldY, &worldZ);
gluUnProject( winX, winY, 1.0, modelview, projection, viewport, &worldX, &worldY, &worldZ);

}
}


I've tested it to make sure that the coordinates are converting successfully, and it works fine!!!

As for the near and far planes I did double check that their coordinates are the same as defined in my glorthro.

If you have multiple object, then of course you must check each.

However, you can first get the closest object to the cursor, and then check if it's inside.

Object closestObj = null; // no object selected
float closestDist = 50; // all objects are smaller than that,

Object currentObj;
float currentDist;

for each currentObj in sccne
{
currentDist = dist (mouse, currentObj)
if (currentDist < closestDist)
{
closestObj = currentObj;
closestDist = currentDist;
}
}


Of course, currentDist must be enough that it's true for every mouse cursos position that is inside the object.

• Thank you very much for your response!!! I will keep working on my project with this new information when I can, but I currently don't have access to my computer at the moment. – PocketTNT Jun 3 '16 at 0:53

Now, if the object is a circle, you must use another formula.

Suppose that the circle is centered on (centerX, centerY) and its radius is radius.

The mouse cursor is inside the circle if...

(cursorX - centerX)^2 + (cursorY - centerY)^2 < radius^2

If it's an ellipse where the horizontal radius is radiusX and the vertical radius is radiusY, then the mouse cursor is inside the ellipse if...

((cursorX - centerX) / radiusX)^2 + ((cursorY - centerY) / radiusY)^2 < 1

"I need to know how to use it to check if the mouse is currently over any of my rendered objects."

Suppose that your object is a rectangle with the following attributes:

• posX: horizontal distance from left of the screen to the first pixel of the object.
• posY: vertical distance from top of the screen to the first pixel of the object.
• sizeX: horizontal size.
• sizeY: vertical size.

In other words, the object is a rectangle that goes from (posX) to (posX + sizeX) in the horizontal axis, and from (posY) to (posY + sizeY) in the vertical axis.

Suppose that the mouse cursor position is (cursorX, cursorY). To check if the the cursor is inside the rectangle, four conditions must happen:

• The cursorX is to the right of the rectangle's left border -> (cursorX >= posX).
• The cursorX is to the left of the rectangle's right border -> (cursorX <= posX + sizeX).
• The cursorY is below the rectangle's top border -> (cursorY >= posY).
• The cursorY is above the rectangle's bottom border -> (cursorY <= posY + sizeY).

So connect the four conditions with ands, and voilá!

• What about for multiple objects on the screen? Because this seems like it would get very in-efficient. Thank you so much for answering my question though!!! :D – PocketTNT Jun 3 '16 at 0:39