Public Group

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

## 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;
}

{
y_vel=-y_vel;
}



##### Share on other sites
my question is the previous code correct for the collision detection.

##### Share on other sites
	//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 on other sites
what is ballposY.

##### 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 on other sites
If I run with this program will it work in WindowsXP. If not so quote me how to run it.

##### 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.

1. 1
Rutin
29
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 13
• ### Forum Statistics

• Total Topics
632960
• Total Posts
3009475
• ### Who's Online (See full list)

There are no registered users currently online

×