Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bouncing Ball logics

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.

like image 826
Lucas Arrefelt Avatar asked Jan 03 '12 16:01

Lucas Arrefelt


People also ask

What are the physics behind bouncing balls?

The air in the ball acts like a spring—it gets compressed and expands again. During the collision, some of the ball's energy is converted into heat. As a consequence, the ball shoots up with less energy than it had when it reached Earth.

Why does a ball bounce lower each time?

The reason it doesn't bounce higher than where it started is simple: some of the ball's energy is lost as heat when it bounces, so it doesn't have as much going up as it did coming down. Knowing that, you might figure that a ball could never bounce higher than the height from which is was dropped.

Does a bouncing ball have momentum?

conservation of momentum: The amount of momentum in a system remains the same after a collision. elastic collision: A collision in which all of the momentum is conserved. For example, a ball that bounces back up to its original height.


2 Answers

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.

like image 162
Gabriel Avatar answered Oct 11 '22 19:10

Gabriel


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.

like image 22
jbranchaud Avatar answered Oct 11 '22 19:10

jbranchaud