# Calculating new velocities after ball collision

1. Dec 20, 2011

### blarp

Hello all, I am making a physics mini game and I would like your help on calculating new velocities after ball collisions. I want the balls to bounce realistically off each other (3 dimensions by the way).

So once I detect a ball collision, these are the variables I have to work with

their x, y, and z velocities

their mass

I could probably calculate a unit vector which points from one ball to the other ball at collision. (Or i could get the 2 angles, yaw and pitch angles, but I think it would be best to keep this in vectors? i don't know)

their positions

And that's it I think, well, any ideas? I know u have to use the law of conservation of momentum but I am clueless on how to start.

2. Dec 20, 2011

### Simon Bridge

Welcome to PF.
You start by looking at how other programmers have solved the same problem and doing what they did. There are libraries for collisions.

For conservation of momentum, you are solving the two-body problem - which is doable in Newtonian mechanics. Look it up. You need to decide how inelastic your collisions will be.

3. Dec 20, 2011

### bigfooted

Hi!
Start with something very simple and then increase the complexity. Write a test program with a known outcome to test intermediate results.

http://en.wikipedia.org/wiki/Elastic_collision
Basically, you are solving conservation of momentum and energy. There is even a code example there.
The extension to 2D or 3D is rather straightforward.
Then continue with inelastic collisions where energy is not conserved:
http://en.wikipedia.org/wiki/Inelastic_collision

To take into account change in rotation, you also need to solve the conservation of angular momentum. This will be important for off-center collisions.

The implementation of 'soft spheres' that deform during collision is more difficult. In games, usually only the bump maps are changed, not the actual mesh.

4. Dec 20, 2011

### blarp

http://http://en.wikipedia.org/wiki/Inelastic_collision

Would I just use this formula for each dimension? Will this work for every case? What if a ball does not hit another ball head on, but it hits it at its side. Don't I need a formula that takes into account angle of impact?

5. Dec 20, 2011

### Simon Bridge

You have received quite a lot of help already - can you be specific?

6. Dec 20, 2011

### blarp

To be more specific, which formulas do I use to calculate the new velocities?

7. Dec 20, 2011

### Simon Bridge

Depends on the specifics of the collision - in general you need the conservation of energy and momentum and angular momentum formulas and some geometry.

Sorry, this is not one of those things where there is a standard formula you plug numbers into. That's why you've been given all those links to look at.

8. Dec 20, 2011

### blarp

I just want it to look realistic... I don't know what to do....

edit: I guess... an inelastic collision is realistic... so, lets go with that?

9. Dec 21, 2011

### FireStorm000

I'm going to help you out by turning this into a simpler problem; I did a very simple program back in the 8th grade, before I ever had any formal physics, that involved a little ball bouncing around a room under the influence of gravity.

I started with the understanding that, in the ideal case, the ball would bounce off a wall the same way light appears to bounce off of a mirror. All I had to do was take the angle of incidence and flip it across the normal of the wall. My implementation was even simpler than that; because all of my walls were either horizontal or vertical, I simply had to invert either the X or Y velocities. When I wanted to make it so that the ball slowed down, I simply reduced the final velocity by about 3% every time a collision occurred. It was a fairly convincing simulation.

Your case is slightly more difficult, but it still shouldn't be too hard to figure something out. You will, however, need to add a few more parameters to your simulation.

The problem is that, as long as total momentum before and after the collision is the same, and the total energy in the system post collision is less than or equal to the total energy before hand, then any solution to the velocity of the balls after the collision is equally valid. In other words, we need to know more about how they collide in order to determine the resulting velocities.

My initial solution, without doing any research, is to start with a change in axises; there would be some component velocity along the line between the two object's center of masses, and some component normal to that. Next, say that the radius of each ball is not fixed, but rather acts as a (spherical) spring. As the two balls collided, the ball would act like a spring as per hook's law, applying a force proportional to the change in radius, and storing some of the kinetic energy of the balls as potential energy in the springs. The springs would dissipate some of the energy that was stored in them (for example as heat). Because the springs would return less than %100(over that would result in a "Flubber" like material) of the energy stored in them, you would ensure conservation of energy, and as long as you ensured that the force acting on the balls was equal and opposite, momentum would be conserved as well. I'm sure there are easier ways out there, but this one should work.

10. Dec 21, 2011

### JHamm

Your first step is figuring out the angles the two balls bounce off at. If at the collision you can take the velocity vector of one ball ($\vec{v_1}$) and a vector between the two ball centers ($\vec{N}$) and an angle between these two vectors ($\alpha$) then the angle of the new velocity vector will be (measured counter clockwise from the horizontal) $2\alpha -\vec{v_1}$.

11. Dec 21, 2011

### Simon Bridge

I'd say, if you are not comfortable with the math and have trouble knowing how to use the information you are getting - go find a physics lib package and another program that does something like what you want. i.e. find out how other programmers did it.

12. Dec 22, 2011

### bigfooted

My advice is to first understand the physics. Read the wiki pages. Understand what is going on. Draw pictures of colliding balls, with the old and the new vectors. Calculate for an elastic collision of two balls the velocity vectors after collision, given the velocity vectors before collision. Then reproduce the answer in your program. If you don't have an 'exact' solution, there is no way to tell if your program is correct or not.

Worry about inelastic collisions and off-center collisions in a later step.

I think the wiki pages already provide you all the information you need. The links given on the wiki page can also be helpful.

13. Dec 29, 2011

### relion65

What u can do is after detection of collisions, reverse the velocities. Ive also seen one instance of swapping the two balls velocities... so that the faster balls velocity gets transferred over and vice versa.

14. Dec 30, 2011

### JHamm

This only works when the collisions can be considered one dimensional.