# Handling collision with LWJGL rectangles

I'm testing collision with other rectangles so I can implement it into my current project. The problem is the rectangle starts at the right x and y, but I'm not sure where exactly they are. I'm pretty sure they're starting from the x and y point and the height is going either up and down. My current ortho makes the y axis start from the bottom of the screen but I'm not sure how their rectangle calculates. How can I improve this class so for each side the bottom rectangle touches, it turns a certain color to identify collision.

public class a {

static int playerX = 400;
static int playerY = 400;

static int enemyX = 100;
static int enemyY = 100;

public static void main(String[] args) {
try {
Display.setDisplayMode(new DisplayMode(640, 480));
Display.setTitle("collision");
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
Display.destroy();
System.exit(1);
}

glMatrixMode(GL_PROJECTION);
glOrtho(0, 640, 0, 480, 1, -1);
glMatrixMode(GL_MODELVIEW);

Rectangle rl = new Rectangle(playerX, playerY, 10, 20);

Rectangle r2 = new Rectangle(enemyX, enemyY, 200, 10);

float c2 = 0;//color

while (!Display.isCloseRequested()) {
glClear(GL_COLOR_BUFFER_BIT);
rl.setX(playerX);
rl.setY(playerY);

r2.setX(enemyX);
r2.setY(enemyY);

if(rl.intersects(r2))
{
c2 = 1f;
} else if(!rl.intersects(r2))
{
c2 = 0f;
}

if(Keyboard.isKeyDown(Keyboard.KEY_A))
playerX -= 1;
if(Keyboard.isKeyDown(Keyboard.KEY_D))
playerX += 1;
if(Keyboard.isKeyDown(Keyboard.KEY_W))
playerY += 1;
if(Keyboard.isKeyDown(Keyboard.KEY_S))
playerY -= 1;

System.out.println(playerY-100);

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

glColor3f(0, 1, 0);

//PLAYER

//l
glVertex2f(playerX, playerY     -10      );
glVertex2f(playerX + 10, playerY    -10  );
glVertex2f(playerX + 10, playerY - 100    );
glVertex2f(playerX, playerY      - 100    );
glEnd();
//r
glVertex2f(playerX + 100, playerY   -10           );
glVertex2f(playerX + 110, playerY        -10      );
glVertex2f(playerX + 110, playerY - 100            );
glVertex2f(playerX + 100, playerY      - 100       );
glEnd();
//top
glVertex2f(playerX + 10, playerY     -9               );
glVertex2f(playerX + 100, playerY       -9       );
glVertex2f(playerX + 100, playerY - 20          );
glVertex2f(playerX + 10, playerY      - 20           );
glEnd();
//bot
glVertex2f(playerX + 10, playerY          -90          );
glVertex2f(playerX + 100, playerY    -90          );
glVertex2f(playerX + 100, playerY - 101        );
glVertex2f(playerX + 10, playerY      - 101         );
glEnd();

glColor3f(c2, 1, 0);
//ENEMY
glVertex2f(enemyX, enemyY);
glVertex2f(enemyX + 200, enemyY);
glVertex2f(enemyX + 200, enemyY - 10);
glVertex2f(enemyX, enemyY - 10);
glEnd();

Display.update();
Display.sync(60);
}
Display.destroy();
System.exit(0);
}

}


I just made it so OpenGL set its position to the rectangle instead of the rectangle to OpenGL.

• It is a little unclear what you want answered. Are you asking for a way you can change color of the 'player' rectangle upon collision? In the code, you have instructions to initialize the display, make the rectangles, move them and draw them, but you don't yet have anything to detect collisions. That makes me wonder if you are looking for an answer telling you how to detect collisions... but it is still not clear. – The Light Spark Apr 3 '15 at 7:06
• Typically you want to update the position of your rectangles AFTER moving the player and BEFORE rendering it. So in your case, you should move your rectangle updates and collision testing after all the keyboard inputs. – Alexandre Desbiens Jun 2 '15 at 18:43

If you say that y starts at bottom, I'm going to assume that +y moves upward on the screen. If this is the case, the rectangle would build "upward". So x,y of the rectangle would be in the bottom left corner, and the bounds of the rectangle would extend upward and to the right. x + width; y + height
To find the location of each corner, just need to add the width and height to the player's location. To find the bottom left corner, you just need to check the against playerX/playerY. To find the opposite end, you would do playerX + r1.width; playerY + r1.height;
To do collision detection, compare each corner of both rectangles to see if it's less-than or greater-than the edge to see if they intersect. For example, to check the left edge you would check if the enemy enemyX+r2.width coordinate is bigger than the playerX and less than playerX + rectangleWidth for each corner of the other rectangle. You'll want to implement the loop such that you're not checking each pair twice, and even don't compare rectangles who are too far away from each other. This method would break if the rectangles have different sizes, a fix may be by checking the mid point between two edges.