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.

abyss.7
  • 13,882
  • 11
  • 56
  • 100
Lucas Arrefelt
  • 3,879
  • 5
  • 41
  • 71

4 Answers4

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.

Offirmo
  • 18,962
  • 12
  • 76
  • 97
Gabriel
  • 3,039
  • 6
  • 34
  • 44
  • 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. – Funk247 Feb 17 '14 at 18:01
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.

jbranchaud
  • 5,909
  • 9
  • 45
  • 70
  • Would the resulting angle not be based on the velocity of the "paddle"? – Kekoa Jan 03 '12 at 17:02
  • 4
    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. – hatchet - done with SOverflow Jan 03 '12 at 17:05
  • Thank you for this simple approach, i'll definately do this if gabes answer turns out to be a bit exaggerated for my game. – Lucas Arrefelt Jan 03 '12 at 17:49
  • I'll give you the right answer since this is the solution i ended up using. – Lucas Arrefelt Jan 04 '12 at 13:23
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/

Community
  • 1
  • 1
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;

}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28