Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Calculating new velocities after ball collision

  1. Dec 20, 2011 #1
    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

    their radius

    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. jcsd
  3. Dec 20, 2011 #2

    Simon Bridge

    User Avatar
    Science Advisor
    Homework Helper

    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.
  4. Dec 20, 2011 #3
    Start with something very simple and then increase the complexity. Write a test program with a known outcome to test intermediate results.

    Start with elastic collisions in 1D or 2D:
    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:

    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.
  5. Dec 20, 2011 #4
    Would I use the formula for new velocities on this page?


    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?
  6. Dec 20, 2011 #5

    Simon Bridge

    User Avatar
    Science Advisor
    Homework Helper

    You have received quite a lot of help already - can you be specific?
  7. Dec 20, 2011 #6
    To be more specific, which formulas do I use to calculate the new velocities?
  8. Dec 20, 2011 #7

    Simon Bridge

    User Avatar
    Science Advisor
    Homework Helper

    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.
  9. Dec 20, 2011 #8
    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?
  10. Dec 21, 2011 #9
    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.
  11. Dec 21, 2011 #10
    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 ([itex]\vec{v_1}[/itex]) and a vector between the two ball centers ([itex]\vec{N}[/itex]) and an angle between these two vectors ([itex]\alpha[/itex]) then the angle of the new velocity vector will be (measured counter clockwise from the horizontal) [itex] 2\alpha -\vec{v_1} [/itex].
  12. Dec 21, 2011 #11

    Simon Bridge

    User Avatar
    Science Advisor
    Homework Helper

    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.
  13. Dec 22, 2011 #12
    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.
  14. Dec 29, 2011 #13
    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.
  15. Dec 30, 2011 #14
    This only works when the collisions can be considered one dimensional.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook