I didn't spot this bug until I started messing around with the paddles and moving them and such.
Code:
#include "vector2.h"
class Paddle
{
public:
vector2 location;
vector2 d_min;
vector2 d_max;
Paddle()
{
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();
}
};
Currently I get the true 2 dimensional location of one of these by doing this:
Code:
void check_ball_collision()
{
if(ball.velocity == moveRight){
a_min = ball.location += ball.d_min;
a_max = ball.location += ball.d_max;
b_min = paddle1.location += paddle1.d_min;
b_max = paddle1.location += paddle1.d_max ;
if (collision2d(a_min, a_max, b_min, b_max))
{reverse_velocity(ball);}}
else if(ball.velocity == moveLeft){
a_min = ball.location += ball.d_min;
a_max = ball.location += ball.d_max;
b_min = paddle2.location += paddle2.d_min;
b_max = paddle2.location += paddle2.d_max;
if (collision2d(a_min, a_max, b_min, b_max))
{reverse_velocity(ball);}}
}
Unfortunately this simply will not do, after a quick debug step through I realized that.... Well these run time environment variables aught to shed some light:
Code:
‡ vector2::operator+= returned {x=70.000000 y=0.00000000 } vector2 &
+ a_max {x=0.00000000 y=0.00000000 } vector2
+ a_min {x=-1.0000000 y=-1.0000000 } vector2
+ b_max {x=70.000000 y=0.00000000 } vector2
+ b_min {x=69.000000 y=-5.0000000 } vector2
+ paddle1 {location={...} d_min={...} d_max={...} } Paddle
+ paddle1.d_max {x=1.0000000 y=5.0000000 } vector2
+ paddle1.location {x=70.000000 y=0.00000000 } vector2
+ this 0x004bde64 class Mechanics game {a_min={...} a_max={...} b_min={...} ...} Mechanics * const
As you can see a_max is only reaching to the middle of the paddle, this simply won't do! This is the state of things right before we perform our collision test! The top half of the paddle is a GHOST! Oh for goodness sake, the ball too!, both of the paddles do this!
A little more source code to shed some light on the issue.
Code:
#include "vector2.h"
class Paddle
{
public:
vector2 location;
vector2 d_min;
vector2 d_max;
Paddle()
{
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();
}
};
As you can see the paddle is CLEARLY drawn out to be 2x10 in dimensions, but my collision code does not agree with this, OFF WITH THEIR HEADS! >:
I don't think it would be wise to change anything in the actual drawing code accept maybe decoupling my dimension vectors from the actual glVertex3f calls. Maybe I need a function that will calculate the true size of the rectangle relative to it's position to give to the collision function.
Any thoughts?
Side note: I posted here because I didn't feel like this was really too specific to opengl or a graphics language or anything like that, it's in there but is well commented.