# Bouncing A Ball - Theory

## Recommended Posts

I have recently decided that to better understand maths / physics, their application and uses within computer games I will attempt to write small programs that simulate the effect. I have decided that I will start by attempting to simulate a ball bouncing. heres what I think I need to know, and I am sure that you guys will help me out ;) I am not doing this graphically at all, simply going to write a program to do the math required, so I suppose I really have a Circle.. not a ball.. but lets pretend. Ball X = Center_Screen Ball Y = Bottom_Screen Ball Radius = 5 Firstly I want to make the ball bounce up and down. I would imagine that this would basicaly involve giving the ball a speed and when that speed reaches 0 reverse it to send the ball in the opposite direction. After acheiving this I want to bounce the ball as if it were thrown up in the air, so it's bounce would gradually decrease as it lost its energy. Then, finally I want to make the ball bounce in a line, as if it were thrown. I have done a search on the forums, most topics I found involve mainly the application of the ball in 3d or collision detection, something I am not interested in at the moment. My language of choice at the moment is C++ btw, incase that is relevant.

##### Share on other sites
OK, sounds like you have a rough idea of the physics but not quite a complete grasp. Essentially you want the equations governing the acceleration, velocity and position of a body to start with:

a = dv/dt
v = dx/dt

where a is acceleration, v is velocity, x is position and t is time. You can juggle these round so that x will come out if you feed in a, v & t. x is the position of the ball at a given time, which is what you want. a is constant (in this case: 9.8 is a good value tochoose, if you're modelling Earth gravity) and v will change with time, but start it at 0.

Set all these up as variables, set t to increment in a loop, and have the body of the loop calculate x for that point in time. Voila, you have a free-falling ball! This step comes just before the three you've listed, but at this point you've established your physical law, and it's easy to extend.

Once you've set this up, add in these details to get your three intnded models.

1) Add a statement to check if the position of the ball x <= 0, and set v = -v if that's the case. This makes the ball kind of bounce off the floor. (It's not quite realistic, but it's a close model.)

2) To model energy being dissipated from the ball, set v = -kv, where k is some proportion of energy between 0 and 1 that remains in the ball. 0.8 or 0.9 is about right. (There is a bug to this, I'll get to it later.)

3) All your positions, velocities and accelerations so far are in the y-axis. You'll need to generate a second set of values in the x-axis, and apply the same laws. a = 0 for this, and v = some value that you want the ball to move at.

The bug in this system comes from this being a time-driven system. Essentially you move time on a little bit, recalculate everything, and so on. In this setup the ball will travel into the floor slightly before it is bounced. Well, if the ball falls very fast and goes a long way into the floor, it may not bounce all the way out again before the next time-step. It's velocity will be reversed again, and essentially it will end up stuck in the floor. I'll leave it for you to iron out.

##### Share on other sites
One quick comment - I may have misunderstood what you said so don't take offence:
Quote:
 Firstly I want to make the ball bounce up and down. I would imagine that this would basicaly involve giving the ball a speed and when that speed reaches 0 reverse it to send the ball in the opposite direction.

This sounds as though you are thinking in terms of speed - it goes 5 miles per hour up, it goes 5 miles per hour down. Don't. Think in terms of velocity instead. It goes 5 miles per hour up. It goes -5 miles per hour up. That last sentance does not contain a typo :)

##### Share on other sites
Represent your ball with a point

This point is a position vector. [x,y]
The ball will have a velocity vector.
The ball may have an acceleration vector.
Each cycle/frame:

vel = vel + accel
pos = pos + vel

The acceleration vector will just keep speeding the whole thing up if you don't regulate it.

You can then add other forces to the acceleration vector such as gravity, wind, slopes etc

Bounce effects can be implemented fairly simply. You can use energy equations to control the acceleration as mentioned above, or you can just manipulate the vectors (for PONG-like effects).

If you bounce of the ceiling/floor just invert the y component of the velocity vector.

If you bounce of the side walls just invert the x component of the velocity vector as appropriate.

This eliminates the need for trig functions.

This has been a very simplistic approach to ball physics, but the same principle can be applied to other physics, such as space craft inertia and car control.

You will have to experiment with values that suit your app. Have fun...

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628334
• Total Posts
2982147

• 9
• 24
• 9
• 9
• 13