I''ve been stuck on this problem for too long. I''m making a Breakout clone with 3D graphics but there''s a fundamental difference: there is an arrow sticking out from the middle of the bat whose angle changes as the bat moves, i.e. if you move the bat left, the arrow rotates right. The angle of the arrow determines the angle that the ball will bounce off the bat. Cool eh? However, this causes a problem as the angle of the ball changes in mid-air when moving the bat as it is still dependant on the angle of the arrow. Here is the code:
...
// Set ball position to bat position if ball hasn''t been fired
if (!ball->ball_fired)
{
ball->ball_x = bat->bat_x;
}
else
{
ball->x_velocity = -ball->y_velocity*(float)sin(bat->arrow_angle); // Set horizontal velocity of ball
}
The ball is then drawn by calling this method:
void Ball::DrawBall()
{
if (ball_fired)
{
ball_y += y_velocity; // Increase y position of ball
ball_x += x_velocity; // Increase x position of ball
}
glTranslatef( ball_x, ball_y, 0.0f); // Move the ball
glCallList(ball_list); // Draw the ball
} // End of method DrawBall
Any ideas how I should be doing this? Am I even calculating the angle that the ball is travelling correctly?
The attributes of the ball are:
float ball_x, ball_y; // x/y-coordinates of ball
bool ball_fired; // Indicates whether ball has been fired by spacebar
float x_velocity, y_velocity; // Ball''s velocity in x and y directions