## Collision two squares

Hey! I am new in this forum! I am trying to learn openGL and did well so far. I want to implement the collision of a square moving in the y and x axis. I tried so many things and I never got it to work. I was wondering if someone could help me with this. The book I am reading to learn this is not really explicit about this topic and all the tutorials I have found are about 3D and not very clear either. I am leaving the code below if someone would want to let me know what I am doing wrong here. Basically the square moves but as soon as it touches the other one my collision doesn't work. I want to make the square go back to the original position after the collision. It is important to mention that this is not a homework or any of the sort, it is a thought I had as a practice of this topic. I am trying to learn this myself. Thanks a lot!. I am sorry for the length of the code!

#include <stdio.h>
#include <math.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <OpenGL/glext.h>
#include <GLUT/glut.h>
#define LENGTH 0.2

float inX =0.0, inY = 0.5;
float inx = 0.0 , iny = 0.0;
float d1;
bool collisionB = false;

void init(){
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(1.0, 1.0, 1.0);
}

void cuad1(){
glTranslatef(inX, inY, 0.0);
glBegin(GL_POLYGON);
glColor3f(0.5, 1.0, 0.5);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, -0.3);
glVertex2f(-0.3,-0.3);
glVertex2f(-0.3,-0.5);
glEnd();
}

void cuad2(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glPushMatrix();
glTranslatef(inx, iny, 0.0);
glBegin(GL_POLYGON);
glColor3f(1.5, 1.0, 0.5);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, -0.3);
glVertex2f(-0.3,-0.3);
glVertex2f(-0.3,-0.5);
glEnd();
glPopMatrix();

}

void reset(){
//Reset position of square???
inx = 0.0;
iny = 0.0;
collisionB = false;

}

void collision(){
d1 = sqrt(static_cast<float>(((inX-inx)*(inX-inx))+((inY-iny)*(inY-iny))));
if (d1 <= 2*LENGTH){
collisionB = true;
}
}

float move_unit = 0.01;
void keyboardown(int key, int x, int y)
{
switch (key){
case GLUT_KEY_UP:
iny += move_unit;
break;

case GLUT_KEY_RIGHT:
inx += move_unit;
break;

case GLUT_KEY_LEFT:
inx -= move_unit;
break;

case GLUT_KEY_DOWN:
iny -= move_unit;
break;

default:
break;
}
glutPostRedisplay();
}

void display(){
//Clear Window
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
cuad1();

if (collisionB == false) {
cuad2();
}
else{
reset();
}
glFlush();
}

int main(int argc, char** argv){

//initialize mode and open a windows in upper left corner of screen
//Windows tittle is name of program

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Collision Practice");
glutSpecialFunc(keyboardown);
glutDisplayFunc(display);
init();
glutMainLoop();

}