# Simple Collision Detection Problem

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 right_goal_scored=0;

``````  paddle2_bottom = paddle2_y_position;

x_direction = -1;
else
right_goal_scored++;
/*  RIGHT_GOAL = right_goal_scored;*/
``````

}

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

`````` paddle_bottom = paddle_y_position;

x_direction = -1;
else
left_goal_scored++;
/* LEFT_GOAL=left_goal_scored;*/
``````

}

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

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

After a quick glance, I believe you should be using “x_direction *= -1” instead of “x_direction = -1” etc.

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?