Help with ball movement in Breakout variation with a twist

Started by
3 comments, last by Billy Lee 21 years, 7 months ago
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
Advertisement
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.
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]
Multiply by fTimePassed? I''m not using any timing algorithms yet. Could you explain this a bit more please?
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
ball.xi = (float)sin( ((ball.x + ball.radius - paddle.x - paddle.width/2) / (paddle.width/2)) * 65/180 * 3.141592654 );
ball.yi = (float)cos( ((ball.x + ball.radius - paddle.x - paddle.width/2) / (paddle.width/2)) * 65/180 * 3.141592654 );

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.

This topic is closed to new replies.

Advertisement