#### Archived

This topic is now archived and is closed to further replies.

# Help with ball movement in Breakout variation with a twist

This topic is 5949 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
Well I have managed to make the ball independant of the bat by moving the else part to the if part and getting rid of the else .

I''m still having problems with the angle. The ball seems to go off at the correct angle but sometimes in the opposite direction, i.e. left instead of right or vice-versa.

##### Share on other sites
don't use angles, use vectors. basically, it's x and y velocities. add that every frame, and multiply by fTimePassed if you want frame-rate independance.

i'm sorry, didn't notice, but you're already doing that. eek.

---
shurcool
wwdev

[edited by - shurcool on August 26, 2002 2:25:24 PM]

##### Share on other sites
Multiply by fTimePassed? I''m not using any timing algorithms yet. Could you explain this a bit more please?

##### Share on other sites
Hey here is some code for deflecting the ball off at different angle s, depending on where the ball hits the paddle. I''m not sure if this is what you want though.

// This gives a deflection between zero and 65 degrees
// (xi, yi) are vectors

Basically this gets a proportional deflection angle from the center of the paddle, converts to radians and works out the vector with a bit of trigonometry.

1. 1
2. 2
3. 3
Rutin
19
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633659
• Total Posts
3013207
×