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);
glLoadIdentity();
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
glBegin(GL_QUADS);
glVertex2f(playerX, playerY -10 );
glVertex2f(playerX + 10, playerY -10 );
glVertex2f(playerX + 10, playerY - 100 );
glVertex2f(playerX, playerY - 100 );
glEnd();
//r
glBegin(GL_QUADS);
glVertex2f(playerX + 100, playerY -10 );
glVertex2f(playerX + 110, playerY -10 );
glVertex2f(playerX + 110, playerY - 100 );
glVertex2f(playerX + 100, playerY - 100 );
glEnd();
//top
glBegin(GL_QUADS);
glVertex2f(playerX + 10, playerY -9 );
glVertex2f(playerX + 100, playerY -9 );
glVertex2f(playerX + 100, playerY - 20 );
glVertex2f(playerX + 10, playerY - 20 );
glEnd();
//bot
glBegin(GL_QUADS);
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
glBegin(GL_QUADS);
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.