|
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. |
FrankyR
Member #243
April 2000
|
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 |
FrankyR
Member #243
April 2000
|
Sure. The way to derive the reflection vector is: 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: So, for example if your two vectors are (3,5) and (-1,2) then 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: 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: 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: 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: 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"} 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: |
|