Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

breakout paddle and ball

This topic is 3094 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
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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!