well I think I am having a problem accessing my class
AABB collision detection
pbivens67 said:
well I think I am having a problem accessing my class
That's plenty of information to receive help ; )
Maybe this causes issues:
class paddle
{
// ...
}paddle;
Class and the global object have the same name. Again i wonder this compiles, and now idea if you could create a second paddle object after that.
One common way to avoid this is using differing upper / lower cases:
class Paddle
{
// ...
}paddle;
@pbivens67 So what is BallX when BallY becomes -100? I am assuming the paddle doesn't move.
I'd suggest putting a break point(s) on the IF statement(s) and see where it fails.
if (X_dist*X_dist <= width * width)
If (Y_dist*Y_dist <= height * height))
//Collision detected
I seem to be having problems accessing the classes, when I run this code it outputs only the same output regardless of where the ball is.
float move_paddle = 0.0f;
float ball_x = 0.0f, ball_y = 0.0f;
float xstep = 2.5f, ystep = 2.5f;
class Paddle
{
public:
float p_x = -20.0f + move_paddle;
float p_y = -100.0f;
float p_width=40.0f;
float p_height=15.0f;
}paddle;
class Ball
{
public:
float b_x = -2.5f + ball_x;
float b_y = -2.5f + ball_y;
float b_width=5.0f;
float b_height=5.0f;
}ball;
class Brick
{
public:
float x;
float y;
float x_width = 35;
float y_height = 15;
float x_right;
float y_top;
Brick(float br_x_left, float br_y_bottom, float br_x_right, float br_y_top)
{
x = br_x_left;
y = br_y_bottom;
x_right = br_x_right;
y_top = br_y_top;
}
};
void Timer(int v)
{
if (ball_x >= 145.0f || ball_x <= -135.0f)
{
xstep = -xstep;
}
if (ball_y >= 100.0f || ball_y <= -100.0f)
{
ystep = -ystep;
}
ball_x += xstep;
ball_y += ystep;
glutPostRedisplay();
glutTimerFunc(50, Timer, 0);
}
void collision()
{
float X_dist = (ball.b_x - paddle.p_x);
float Y_dist = (ball.b_y - paddle.p_y);
float width = (paddle.p_width / 2.0 + ball.b_width / 2.0);
float height = (paddle.p_height / 2.0 + ball.b_height / 2.0);
cout << "Xdist: " << X_dist << " width: " << width << " Ydist: " << Y_dist << " height: " << height << endl;
if ((X_dist*X_dist <= width * width) && (Y_dist*Y_dist <= height * height))
{
cout << "Collision Detected" << endl;
}
}
pbivens67 said:
ball_x += xstep; ball_y += ystep;
There you change the global variables, but you forgot to change the members of the ball class.
As said - to avoid such confusion, use only the class. Remove the global variables ball_x/y.