PDA

View Full Version : Simple Collision Detection Problem



10-01-2003, 01:59 PM
Hi guys,
I know there's something wrong my code and I need some help with it. The ball bounces off the top wall and makes its way straight out of the screen. Anyone care to help?
TY
The ball is square and its center is ball_x_pos.


void move_ball ()
{
ball_x_pos += BALL_STEP * x_direction;
ball_y_pos += BALL_STEP * y_direction;

if (ball_x_pos + 2.5 >= RIGHT_MARGIN);
{
int paddle2_top;
int paddle2_bottom;
int right_goal_scored=0;

paddle2_bottom = paddle2_y_position;
paddle2_top = paddle2_bottom + 20;

if ((ball_y_pos+2.5 > paddle2_bottom)&&(ball_y_pos-2.5 < paddle2_top))
x_direction = -1;
else
right_goal_scored++;
/* RIGHT_GOAL = right_goal_scored;*/
}

if (ball_x_pos - 2.5 <= LEFT_MARGIN);
{
int paddle_top;
int paddle_bottom;
int left_goal_scored=0;


paddle_bottom = paddle_y_position;
paddle_top = paddle_bottom + 20;

if ((ball_y_pos+2.5 > paddle_bottom)&&(ball_y_pos-2.5 < paddle_top ))
x_direction = -1;
else
left_goal_scored++;
/* LEFT_GOAL=left_goal_scored;*/
}

if (ball_y_pos+2.5 >= TOP_MARGIN&#0124; &#0124; ball_y_pos-2.5 <= BOTTOM_MARGIN)
{
y_direction = -1;
}

glutTimerFunc(50, move_ball, 1);
glutPostRedisplay();
}

10-01-2003, 02:26 PM
After a quick glance, I believe you should be using "x_direction *= -1" instead of "x_direction = -1" etc.

10-06-2003, 06:58 AM
Thanks cfmdobbie2 for your contribution. The problem has taken a new form.Once the ball reaches the vicinity of the paddle it does wierd things like bounces off the top and bottom walls orthogonaly. Would you have an idea what the cause may be?