Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
So in the below code there's collision detection for a player's paddle with a ball. I need the ball to bounce off at a certain angle based on where it hits the paddle. So if it's close to the middle it will bounce roughly horizontally, if it's further away from the middle, the angle increases.

I have commented out what seems to be the formula but I'm having trouble even calculating the sin(angle) part because I can't figure out how to even get the angle.

Here's a link which shows the solution: http://gamedev.stackexchange.com/questions/4253/how-do-you-calculate-where-a-ball-should-go-when-it-bounces-off-the-bar[^]

BUT, in that link, I don't understand what intersectY is. If I can figure that out then I can easily apply the formula. Can anyone have a look at the link and figure out what intersectY actually stores? Because from his explanation in the link, I honestly can't understand. I just need to know what intersectY holds the value of, and how to obtain that value.

C++
if (ball.getPosition().x < (player.getPosition().x) //PLAYER COLLISION DETECTION
		&& (ball.getPosition().y + (diameter)) >= player.getPosition().y
		&& ball.getPosition().y <= (player.getPosition().y + 100))
		{
			//xSpeed = xSpeed*cos(angle);
			//ySpeed = ySpeed*sin(angle);
			ySpeed = -ySpeed;
			xSpeed = -xSpeed;
			ball.move(xSpeed * Time.asMilliseconds(), ySpeed * Time.asMilliseconds());
		}
Posted
Comments
Rick York 17-Apr-17 19:07pm    
The usual algorithm used, in the ideal sense, is the same used for the reflection of light from a surface. Here is one example describing this : http://www.physicsclassroom.com/class/refln/Lesson-1/The-Law-of-Reflection

I wrote "ideal sense" because there are many physical factors involved like the absorption of energy by the ball, spin on the ball, etc.

1 solution

it looks like intersectY is the 'Y' axis component of where the collision of the ball happens with the paddle

it could possibly be
ball.getPosition().y
in your code (Im not sure wether you would use the ball or player co-ordinates)
 
Share this answer
 
Comments
Member 11238660 16-Nov-14 23:01pm    
I think I figured out what the intersectY is. It seems like it's actually the value of the Paddle's Y middlepoint position MINUS Ball's Y Position (if the ball hit anywhere between the middle and the top part of the paddle) or the Paddle's Y middlepoint position PLUS Ball's Y Position. Which means it would always give a value between -50 and 50 depending on where it hits (my paddles are 100 pixels in height).

Unfortunately, I did apply the formula in the link but to no prevail. Despite being inside the IF statement of a collision, sometimes it actually goes through the paddle even though it detected a collision due to this formula, and the angles are all messed up with it flying off at random directions.

So I guess that formula is not the correct way to do it then. Do you have any other method of achieving a way to bounce the ball off at angles?

If all else fails, I'll resort to splitting the paddle into 3 sections and applying an angle based off on that, though that wouldn't replicate the true nature of pong since it bounces at a different angle based on each individual pixel of the paddle.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900