I'm trying to implement 2D collision detection with two rectangles constructed using a graphics package. Unfortunately, I'm beginning to think I don't understand the logic needed to write a function that will handle this.
Below is my code that draws a small sprite and a couple of other rectangles. My sprite moves with keyboard inputs.
I've used several books and also tried sites like Nehe etc and although they're really good tutorials, they only seems to deal directly with 3D collision.
Can someone please show me an efficient way of implementing collision detection using my rectangles above? I know you need to compare the coordinates of each object. I'm just unsure how to track the position of the objects, checking collision and stopping it moving should it collide.
I am self learning and seem to have come to a stop for days now. I'm totally out of ideas and searched more google pages than I care to remember. I'm sorry for my naivety.
I'd appreciate any constructive comments and example code. Thank you.
void drawSprite (RECT rect){
glBegin(GL_QUADS);
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(rect.x, rect.y, 0.0);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(rect.x, rect.y+rect.h, 0.0);
glColor3f(0.2f, 0.2f, 0.2f);
glVertex3f(rect.x+rect.w, rect.y+rect.h, 0.0);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(rect.x+rect.w, rect.y, 0.0);
glEnd();
}
void drawPlatform (RECT rect){
glBegin(GL_QUADS);
glColor3f(0.2f,0.2f,0.0f);
glVertex3f(rect.x, rect.y, 0.0);
glColor3f(1.0f,1.0f,0.0f);
glVertex3f(rect.x, rect.y+rect.h, 0.0);
glColor3f(0.2f, 0.2f, 0.0f);
glVertex3f(rect.x+rect.w, rect.y+rect.h, 0.0);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(rect.x+rect.w, rect.y, 0.0);
glEnd();
}