Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Make a ball bouncing?

This thread is locked; no one can reply to it. rss feed Print
Make a ball bouncing?
New_sun
Member #10,816
March 2009

I'd like to make a ball bouncing when something is touched like in the pong game.
Is there any algorithms?
What is the main idea?
Thank you in advance.

FrankyR
Member #243
April 2000
avatar

Well, the best way to do this is to find the reflection vector for the ball after it hits the wall or paddle. This is really easy to do mathematically once you have the basics set up.

First, you have to think about how the ball's position and velocity is represented. It's easiest to think of these as two vectors, each consisting of an "x" and a "y" component.

//ball's position
float x,y;
//ball's velocity
float dx,dy;

The dx and dy represent the velocity of the ball in the x and y directions (i.e. the amount to change the ball's x and y position each update.

//in the ball's update function (assuming fixed time step):
x+=dx;
y+=dy;

In order to make the ball "bounce" off of a surface you just need to change its velocity (dx and dy) after the collision is resolved. This can be done using a dot product between the ball's velocity vector and the surface's normal, but if you're just doing a pong game you can get away with a simpler solution:

If the ball is bouncing off of a vertical surface, then just reverse the x component of the ball's velocity (i.e. dx=-dx), and if the ball is bouncing off of a horizontal surface, then you just reverse the y component of the ball's velocity (i.e. dy=-dy). If you want to deal with surfaces that are not perfectly horizontal or vertical then you'll need to use a little bit of simple vector math (dot product).

The other half of the problem is figuring out when the ball hits something that it needs to bounce off of. I'd recommend searching for "bounding box" collision detection for that.

New_sun
Member #10,816
March 2009

I know how to create a bounding box,but I didn't understand the dot product for
the not perfectly horizontal and vertical surfaces.
Could you make me an example?
Thank you.

FrankyR
Member #243
April 2000
avatar

Sure.

The way to derive the reflection vector is:
r = u - 2(u.n)n
where:
r is the reflection vector
u is the incident vector
n is the (normalized) normal vector

I'll explain in more detail:

To calculate the dot product of two vectors you multiply and add the components of the vectors with each other:
(x1,y1) dot (x2,y2) = x1*x2 + y1*y2

So, for example if your two vectors are (3,5) and (-1,2) then
(3,5) dot (-1,2) = (3*-1) + (5*2) = -3+10 = 7

Notice that the dot product of two vectors is always a scalar value.

Now, to calculate the reflection vector, we need to know the incident vector (i.e. the velocity vector of the object before it's reflected) and the normal of the surface it is bouncing off of. The normal is simply a (normalized) line that is perpendicular to the surface:
598488

It's important that the normal vector is normalized (that means it has magnitude 1). You can normalize a vector by dividing each of its components by the magnitude of the vector).

So, let's say the the line that you want to reflect off of goes from (1,3) to (4,7). Then you can describe it by the vector (3,4). To get the normal of that, we just flip the components and negate the first one, so the normal of (3,4) is (-4,3). Now, we need to normalize that vector. The magnitude of (-4,3) is:
<math>\sqrt{(-4)^2*3^2}</math>
so that is 5. Therefore, the normalized version of (-4,3) is:
<math>(\frac{-4}{5},\frac{3}{5}) = (-0.8,0.6)</math>

So, now we have the normalized normal of the surface. It's time to calculate the reflection vector.

Let's say that the ball is coming in with a velocity of (3,1)

First, we calculate the dot product between the velocity/incident vector and the normal of the surface, so that's:
(3,1) dot (-0.8,0.6) = -2.4 + 0.6 = -1.8

You can think of this value as representing the magnitude of the incident vector projected onto the normal, so to get the component of the incident vector aligned to the normal we just need to multiply the result of the dot product with the normalized normal vector:
-1.8*(-0.8,0.6) = (1.44,-1.08). This is the projection of the incident vector onto the normal vector.

So, if we were to subtract the above vector from the original velocity vector, it would completely stop moving along the axis of the normal, and it we were to subtract it twice, we get the reflection.

So, the reflection vector is (3,1) - 2*(1.44,-1.08) = (3,1)-(2.88,-2.16) = (.12,3.16).

{"name":"598490","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/e\/ceaadb7d13a29401a571ed7d0e4a1c40.png","w":241,"h":238,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/e\/ceaadb7d13a29401a571ed7d0e4a1c40"}598490

The basic idea behind all of this is you want to get the velocity vector projected onto a different set of axis (i.e the surface and its normal) and use the vectors to calculate the reflection:
{"name":"598491","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/4\/14168ff980402674ea85aac6568062b4.png","w":385,"h":340,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/4\/14168ff980402674ea85aac6568062b4"}598491
This shows the incident vector projected onto the surface and the surface's normal, so we can think of these projections as the portion of incident vector along those new axis. To reflect a vector we went to change the component of the vector along the axis of the reflection, but not along the other.

Go to: