# Simple Collision?

My project is simply detecting if there is overlap between a square and the mouse position. I think this is a good start to understand the collision principle. After drawing the stuff in the board to understand the concept, I came up with the following
X_mouse < x_max && X_mouse > x_min && Y_mouse < y_max && Y_mouse > y_min
This means overlapping for sure. But the problem I’m getting is if I move the mouse to the shape I’m getting different vertices. Also, the problem is getting worse if the window is reshaped.
Let say x_min = 20, x_max = 80, y_min = 20, y_max = 80
If I move the mouse to the shape then x_min = 100 and so on for the other vertices. Why this is the case? Please correct me if I’m going in the correct way with collision at least with my case.

``````#ifdef _WIN64
#pragma warning (disable:4996)
#endif

#if defined(WIN32)
# include <windows.h>
#endif

#include <iostream>
#include <GL/glut.h>

int const window_Height = 500, window_Width = 500;

static int x_min = 20.0, y_min = 20.0, x_max = 80.0, y_max = 80.0;
static int X_mouse, Y_mouse;

/* Glut Functions */
void glutDisplay(void);
void glutReshape(int width, int height);
void glutIdle(void);
void Mouse_Movement(int x, int y)
{
X_mouse = x;
Y_mouse = window_Height - y;
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(window_Width, window_Height);
glutInitWindowPosition(100, 100);
glutCreateWindow ("square.cpp");
glutDisplayFunc(glutDisplay);
glutReshapeFunc(glutReshape);
glutIdleFunc(glutIdle);
glutPassiveMotionFunc(Mouse_Movement);
glutMainLoop();
}

void glutDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

if (  X_mouse < x_max && X_mouse > x_min && Y_mouse < y_max && Y_mouse > y_min  )
{
std::cout << "Collision has been detected : " <<  "< " << X_mouse << " , " << Y_mouse << " > " <<std::endl;
}

else
{
std::cout << "No Collision " <<  "< " << X_mouse << " , " << Y_mouse << " > " <<std::endl;
}
glBegin(GL_POLYGON);
glVertex3f(x_min, y_min, 0.0);
glVertex3f(x_max, y_min, 0.0);
glVertex3f(x_max, y_max, 0.0);
glVertex3f(x_min, y_max, 0.0);
glEnd();

glFlush();
}

void glutReshape(int width, int height)
{
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glOrtho(0.0, 100.0, 0.0, 100.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}

void glutIdle()
{
glutPostRedisplay();
}

``````

After a while, I found out the solution for this problem. Unfortunately, the authors of some books mention nothing about the concept of converting the window (viewport) coordinates to OpenGL coordinates in some cases ( my problem is one of these cases). To solve the problem, we need to convert the window coordinates to OpenGL coordinates by using gluUnProject() then we get the new mouse positions according to OpenGL coordinates. Now, it is easily to find the collision between an object in Viewport coordinates and the mouse positions. Hopefully, this may help any one who may encounter this problem at least you know something about gluUnProject().