Bouncing Ball logics
Asked Answered
A

4

6

I have got a ball which bounces of walls. This bounce is simple, i just do this, ( code snippet )

if ( x - moveSpeed < 0 ) // Ball hit left wall
    xVel *= -1;

However i also got a rectangle which the player moves. The bounce on this practically works as the bounce on walls.

But i figured out that when a ball got similar movement as the picture, its impossible for me to make it go straight up again. I therefore need some kind of calculation regarding the rectangles movement to influence the outcoming angle of the ball. The rectangle always got a constant movement speed when moving. This picture shows a rectangle moving to the left and the ball hitting it during its movement, which results in a 90 degree angle. ( Which shouldn't always be 90 ).

Sorry about my crappy pictures i hope they make sense. My math is rusty thats why i really could need a push in the right direction.

Alpaca answered 3/1, 2012 at 16:47 Comment(3)
Provide some code of your attempt on this ^_^Crackle
Do you want to have accurate physics or just something which "feels better" than the current behaviour?Ichor
Euclidean vectors and Pythagorean theorem should be enough for simple physics like this.Fischer
B
7

No need for any fancy math here. My understanding of these types of games is that the angle the ball comes off of the paddle is determined by where on the paddle it bounces. If it bounces in the middle, then the current angle is preserved. As it bounces closer to the edge of the paddle, the angle is adjusted in the direction of that side of the paddle. Think of the paddle as a rounded surface.

Branson answered 3/1, 2012 at 16:55 Comment(4)
Would the resulting angle not be based on the velocity of the "paddle"?Chalky
Remembering playing pong, even if the paddle is stationary, if the ball hit near the end the angle was greatly affected in that direction. I think @Treebranch is right. I think the movement of the paddle at the time of impact did not affect it - only where the ball hits.Glassworks
Thank you for this simple approach, i'll definately do this if gabes answer turns out to be a bit exaggerated for my game.Alpaca
I'll give you the right answer since this is the solution i ended up using.Alpaca
C
9

Here is a tutorial on some physics (which is what you need to know) and you need to learn about vectors. The tutorial doesn't go over exactly what you are looking for (the reflection of the bounce and angles) but this is a GREAT start for beginning, because you'll need to know all this to finish your project.Game Physics 101

If you want to do it the easy way, here is code in c++ that describes exactly how to do what your looking for.

Edit


You should actually check out the second link first, its a tutorial on exactly what you need to know. But if you are looking to do more than just make the ball bounce around, say include other moving objects or something like that, check out the first link.

Crackle answered 3/1, 2012 at 16:54 Comment(1)
Code has been transposed to JS at time of writing this comment, I havn't been able to find the C++ version as of yet.Iconolatry
B
7

No need for any fancy math here. My understanding of these types of games is that the angle the ball comes off of the paddle is determined by where on the paddle it bounces. If it bounces in the middle, then the current angle is preserved. As it bounces closer to the edge of the paddle, the angle is adjusted in the direction of that side of the paddle. Think of the paddle as a rounded surface.

Branson answered 3/1, 2012 at 16:55 Comment(4)
Would the resulting angle not be based on the velocity of the "paddle"?Chalky
Remembering playing pong, even if the paddle is stationary, if the ball hit near the end the angle was greatly affected in that direction. I think @Treebranch is right. I think the movement of the paddle at the time of impact did not affect it - only where the ball hits.Glassworks
Thank you for this simple approach, i'll definately do this if gabes answer turns out to be a bit exaggerated for my game.Alpaca
I'll give you the right answer since this is the solution i ended up using.Alpaca
G
4

Going the route of simulating actual physics (as opposed to @Treebranche's answer, which is how I think those sort of games really work) can get very complicated. You can consider friction, spin, duration of contact, etc. Here are a couple links that discuss this.

https://physics.stackexchange.com/questions/11686/finding-angular-velocity-and-regular-velocity-when-bouncing-off-a-surface-with-f

https://physics.stackexchange.com/questions/1142/is-there-a-2d-generalization-of-the-coefficient-of-restitution/

Glassworks answered 3/1, 2012 at 17:25 Comment(0)
D
1

This code demonstrates how to bounce the ball back or in another direction by reversing the ball's X or Y heading with ball.headingX=-ball.headingX and ball.headingY=-ball.headingY .

Putting theory to practice :

/* update ball's x heading */
ball.x+=ball.headingX;

/* update ball's y heading */
ball.y+=ball.headingY;

/* if ball at most right of screen then reverse ball's x heading */
if( (ball.x>PONG_SCREEN_RIGHT) )
{
    ball.headingX=-ball.headingX;

}

/* check if ball's location at top or bottom of screen,if true reverse ball's y heading */
if( (ball.y<PONG_SCREEN_TOP) || (ball.y>PONG_SCREEN_BOTTOM-2) ) 
{
   ball.headingY=-ball.headingY;
}



/* check if ball lands on pad, if true bounce back */
if ( (ball.y>= PlayersPad.LEFT) && (ball.y<= PlayersPad.RIGHT) && (ball.x==PlayersPad.x))
{
    ball.headingX=-ball.headingX;
    playersScore+=10;
}

/* let computer track ball's movement */
if (ball.x>PONG_SCREEN_RIGHT-18) computersPad.y=ball.y;


/* check if ball misses pad, if true display you missed */
if (ball.x<PONG_SCREEN_LEFT)
{
    displayYouMissed();
    ball.x=ball_Default_X;
    ball.y=ball_Default_Y;

}
Dumbarton answered 3/1, 2012 at 17:23 Comment(1)
By showing how to reverse the ball's heading with ball.headingX=-ball.headingX and ball.headingY=-ball.headingY.Dumbarton

© 2022 - 2024 — McMap. All rights reserved.