• Advertisement
Sign in to follow this  

breakout paddle and ball

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

the following code is to have a ball bounce off the walls and and the paddle.
	//bounce off sides
	if (ball_left<=0||ball_left>=780)
	{
	x_vel=-x_vel;
	}

	//bounce off paddle
	if (ball_top>=paddle_top && ball_left>=paddle_left && ball_right<=paddle_right) 
	{
	y_vel=-y_vel;
	}

Share this post


Link to post
Share on other sites
Advertisement

//bounce off sides
if (ball_left<=0||ball_left>=780)
{
// TO-DO move ball away from wall by distance of radius to avoid it sticking to the wall
x_vel= *= -1;
}

//bounce off paddle
if (ball_top>=paddle_top && ball_left>=paddle_left && ball_right<=paddle_right)
{
ballposY = paddle_top - ball_radius; // Stop ball getting stuck in bat
y_vel *= -1;
}



Share this post


Link to post
Share on other sites
Well, presumably there's some variable that you're adding y_vel to each frame. Something like this:

ballposY += y_vel;

Share this post


Link to post
Share on other sites
You might also want to add some code to make sure that a collision only happens once.

That is, let's say ball_left is at -20. You switch x_vel, so it starts moving in the right direction. However, let's say during the next frame, ball_left is -5.

It's moving in the right direction, but your code will "bounce" it back the wrong way.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement