Sign in to follow this  
phil67rpg

breakout paddle and ball

Recommended Posts

phil67rpg    443
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
clearz    157

//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
Kronojuice    100
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this