Hey everyone I just recently learned the mechanics of AABB collision detection. I have a problem where it seems to be that my collision detection function doesn't seem to detect the thing it was designed to (with the help of a friend).
I expect a true for no collision and a false for a collision, I think this is true?
Code:
#include "vector2.h"
bool collision2d(vector2 a_min, vector2 a_max, vector2 b_min, vector2 b_max) {
return (a_min.x <= b_max.x) && (a_min.y <= b_max.y) && (a_max.x >= b_min.x) && (a_max.y >= b_min.y);}
This is where I use the function:
Code:
#include "Paddle.h"
#include "Ball.h"
#include "Collision2d.h"
#include "vector2.h"
Paddle paddle1, paddle2;
Ball ball;
class Mechanics
{
public:
void move_paddle(vector2 direction, Paddle & gamePaddle){gamePaddle.location += direction;}
void check_and_set_ball(Ball & gameBall, Paddle & gamePaddle)
{
if (collision2d(gameBall.d_min, gameBall.d_max, gamePaddle.d_min, gamePaddle.d_max))
{
ball.location += moveRight;
}
else{ball.location += moveLeft;}
}
};
This is my paddle class:
Code:
#include "vector2.h"
class Paddle
{
public:
vector2 location;
vector2 d_min;
vector2 d_max;
Paddle()
{
location.x = 10;
location.y = 0;
d_min.x = -1;
d_min.y = -5;
d_max.x = 1;
d_max.y = 5;
}
const vector2 & get_position() const { return location; }
void draw(vector2 vec)
{
glPushMatrix();
glTranslatef(vec.x,vec.y,-100.0f);
glBegin(GL_QUADS); // Start Drawing Quads
glVertex3f( d_min.x, d_min.y, 1.0f); // Bottom Left Of The Texture and Quad
glVertex3f( d_max.x, d_min.y, 1.0f); // Bottom Right Of The Texture and Quad
glVertex3f( d_max.x, d_max.y, 1.0f); // Top Right Of The Texture and Quad
glVertex3f( d_min.x, d_max.y, 1.0f); // Top Left Of The Texture and Quad
glEnd();
glPopMatrix();
}
};
And this is my ball
Code:
#include "vector2.h"
class Ball
{
public:
vector2 location;
vector2 d_min;
vector2 d_max;
Ball()
{
location.x = 0;
location.y = 0;
d_min.x = -1;
d_min.y = -1;
d_max.x = 1;
d_max.y = 1;
}
void draw(vector2 vec)
{
glPushMatrix();
glTranslatef(vec.x,vec.y,-50.0f);
glBegin(GL_QUADS); // Start Drawing Quads
glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
glEnd();
glPopMatrix();
}
};
When I compile and run my app the ball seems to go straight through the paddle and on it's merry way off into the depths of virtual reality.
BTW I'm performing the check right here:
Code:
while(!done) // Loop That Runs While done=FALSE
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting?
{
if (msg.message==WM_QUIT) // Have We Received A Quit Message?
{
done=TRUE; // If So done=TRUE
}
else // If Not, Deal With Window Messages
{
TranslateMessage(&msg); // Translate The Message
DispatchMessage(&msg); // Dispatch The Message
}
}
else // If There Are No Messages
{
// Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene()
if (active) // Program Active?
{
if (keys[VK_ESCAPE]) // Was ESC Pressed?
{
done=TRUE; // ESC Signalled A Quit
}
else // Not Time To Quit, Update Screen
{
game.check_and_set_ball(ball, paddle1);
DrawGLScene(); // Draw The Scene
SwapBuffers(hDC); // Swap Buffers (Double Buffering)
}
}
if (keys[VK_F1]) // Is F1 Being Pressed?
{
keys[VK_F1]=FALSE; // If So Make Key FALSE
KillGLWindow(); // Kill Our Current Window
fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode
// Recreate Our OpenGL Window
if (!CreateGLWindow("NeHe's OpenGL Framework",640,480,16,fullscreen))
{
return 0; // Quit If Window Was Not Created
}
}
if (keys[VK_UP]){game.move_paddle(moveUp, paddle1);}
if (keys[VK_DOWN]){game.move_paddle(moveDown, paddle1);}
if (keys[VK_LEFT]){game.move_paddle(moveLeft, paddle1);}
if (keys[VK_RIGHT]){game.move_paddle(moveRight, paddle1);}
}
}
Any pointers in the right direction would be appreciated