# Understanding Physics for Coding Collision of 2 Balls

• B
• Zalokin
In summary, you are seeking guidance on how to apply physics to a computer program. You have found a video on YouTube explaining the law of conservation of momentum and have attempted to include motion for both balls, but are struggling to find relevant information. You are unsure of how to calculate the final velocities and angles after collision.f

#### Zalokin

Hi There!

I am quite a beginner at physics and my aim is to apply physics to a computer program which I'm coding.

The program consists of 2 balls of equal mass colliding into each other and demonstrating the trajectories after an elastic collision.

I have already coded a more basic model consisting of a ball in motion striking a static ball - https://zalokin78.github.io/ballCollision3/

I researched and implemented the Law of Conservation of Momentum, after I watched the following video on YouTube: -

I am now at a stage that I want to include motion for both balls (rather than one ball in motion and the other one static as in my current stage).

Therefore, both balls would be situated in random locations and set their trajectories to collide against each other at different velocities.

This means that I suppose that this requires more calculations because the angles of trajectory and velocities would affect both balls, depending on their initial velocities and point of collision.

I have researched online for about a week now, but I am struggling to find videos etc how this works.

There are various variables which are already known :-

Initial angles of trajectory (for BallA and BallB)
Initial velocities of trajectory (for BallA and BallB)
Both balls are of equal mass (for simplification - 1kg)
Angle of the centers of balls at point of collision

I need to calculate: -

Final velocities after collision (for BallA and BallB)
Final angles after collision (for BallA and BallB)

I am aware that this could require 2 laws: -

Law of Conservation of Momentum
Law of Conservation of Kinetic Energy

I would be extremely grateful for some sort of guidance, to help me understand this or perhaps steer me through certain contents online that can help me to complete my programming project.

Thank you!

For both balls in motion, you can transform to the rest frame of one of the balls; solve the problem as before; then transform the final velocities back to the original frame.

• A.T., jbriggs444, topsquark and 1 other person
For both balls in motion, you can transform to the rest frame of one of the balls; solve the problem as before; then transform the final velocities back to the original frame.
You can additionally rotate the frame axes, to align one of them with the contact normal between the balls (line connecting their centers on impact). This simplifies accounting for normal and frictional momentum transfer.

Thanks for the reply. Yes, I do understand what you've explained.

I actually already implemented what you mentioned for my existing code of one moving ball hitting a static one. In the background, the angle of the hitting ball is set for a 0 degree angle trajectory towards the static ball to work out the final velocities and then rotated back for the proper visual representation.

But my question was more to do with the calculations of the velocities and angles themselves, not at a programming standpoint - more of a physics/ mathematical question.

To further explain my dilemma: -

In my last example, the final angles were easy to predict - imagine drawing a line between the centers of both balls at moment of impact (let's name the angle of the line theta).

The hitting ball would travel theta +/- 90 degrees theta. The static ball being hit travels at the angle theta.

Simple as that.

The fact that now both balls are in motion at different angles and velocities introduces another factor - the final angles would not follow the same simple principal as above. The angles would vary depending on the initial velocities/ initial angles of both balls.

I'm just stuck at this point not knowing how to calculate the final angles and final velocities.

P.S. Note that this is a simple elastic system - the system does not cater for frictional forces etc.

I'm just stuck at this point not knowing how to calculate the final angles and final velocities.
See post #2.

I've understood about calculating the velocities, as if the ball being hit it is at rest (this is what I understand from Post #2).

However, my question still remains -

I can't solve the problem as before as you said - the previous example already sets the final angles as known values, and from there separates the x and y components to calculate the final velocities.

I don't have these final angles (just the initial angles before collision).

How do you calculate the final angles from the initial angles and velocities of the balls? Is this possible? Am I seeing it the wrong way?

So I'm in a "chicken and egg" predicament. How can I work out the final velocities if I don't have the final angles? Both need info about the other in order to be calculated.

Perhaps I'm not grasping the full picture. My apologies and thank you for your help.

I'm not sure what you are trying to do. Why can't you transform to and from the rest frame of one of the balls?

The fact that now both balls are in motion at different angles and velocities introduces another factor...
They aren't both in motion in the initial rest frame of one of the balls. Transform your velocity vectors into that working frame, compute there, then transform back the final velocity vectors to your original frame.

... the system does not cater for frictional forces.
Then align the x-axis of your working frame with the contact normal (line between the centers on impact). Without friction there is no momentum transfer along the y-axis in this frame, just along the x-axis, so you have basically a trivial 1D collision with one mass initially at rest to solve.

I strongly recommend working with velocity vectors and their Galilean transformations between reference frames. This allows you to avoid lots of that angles/trigonometry mess and you can choose the most convenient frame that simplifies a problem.

Last edited:
• berkeman and PeroK
They aren't both in motion in the initial rest frame of one of the balls. Transform your velocity vectors into that working frame, compute there, then transform back the final velocity vectors to your original frame.

Then align the x-axis of your working frame with the contact normal (line between the centers on impact). Without friction there is no momentum transfer along the y-axis in this frame, just along the x-axis, so you have basically a trivial 1D collision with one mass initially at rest to solve.

I strongly recommend working with velocity vectors and their Galilean transformations between reference frames. This allows you to avoid lots of that angles/trigonometry mess and you can choose the most convenient frame that simplifies a problem.
Thank you for your replies. I don't really understand the term "rest frame", but I suppose that this is related the Frames of Reference in physics. As this is all new to me, because of your posts, I am currently studying the subject of CM reference frames - not sure if this might lead to an insight on rest frames (not sure what this is yet).

But I suspect that a rest frame means that the observer's position would be "locked" to the position of one of the balls, allowing just for the calculations of the other ball relative to the observer.

But this is quite daunting for me at the moment, but am willing to really study on this.

Would it be possible to direct me to some lectures/ content online about rest frames and how to transform to the rest frame (not sure what that means)? Thanks.

Last edited:
• PeroK
But I suspect that a rest frame means that the observer's velocity would be "locked" into the motion of one of the balls, allowing just for the calculations of velocity of the other ball relative to the observer.
Don't panic. Frames of reference are not that hard.

Yes, "rest frame" refers to a frame of reference where the observer moves along with one of the balls as that ball coasts toward the collision.

I've always thought of a "frame of reference" as being pretty much a synonym for "coordinate system". It is like a piece of graph paper that you lay down over a physical situation so that you can read off (x,y) coordinates for all of the events of interest.

If you rotate the graph paper, all of the events get new coordinates. If you work at it, you can do the trigonometry and calculate the new coordinates based on the old coordinates and the rotation angle. Or you can reverse that calculation to get old coordinates from new coordinates.

You can shift the coordinate system up and across by some offset. Again, all of the coordinate values will change. Again, you can calculate new coordinates from the old ones or old coordinates from the new ones.

You can think about moving the graph paper across the table at a steady pace. Again, the coordinate values will change. They will change in a systematic way that varies over time.

The idea is to move the graph paper so that it keeps pace with the one ball as it coasts toward the collision. After the collision, the graph paper keeps moving at that same steady pace.

[You do not usually want your coordinate system to keep pace with an object that object accelerates or undergoes a collision. You usually want your coordinate system to move smoothly and steadily instead. That is, you want it to be an "inertial" frame of reference. Accelerating frames can be useful, but leave that for another day]

You transform all of your old coordinates to new coordinates relative to the moving graph paper. You do the collision math using the new coordinates. Then you transform all of your new coordinates back to old coordinates and read off the result of the collision.

The required transformations are easy.

Let us assume that the two coordinate systems coincide at t=0. Let us use (x, y) to denote an event in old coordinates and (x', y') to denote the same event in new coordinates. Let us say that the new coordinate system is moving at ##v_x## toward the right and ##v_y## upward.

The transform from old coordinates to new is then:$$x'(t) = x(t) - v_x t$$ $$y'(t) = y(t) - v_y t$$That's just two lines of code.

The transform from new coordinates to old is the reverse:$$x(t) = x'(t) + v_x t$$ $$y(t) = y'(t) + v_y t$$Again, it is just two lines of code.

Last edited:
• PeroK
But I suspect that a rest frame means that the observer's position would be "locked" to the position of one of the balls, allowing just for the calculations of the other ball relative to the observer.
Yes, the frame where one of the balls is at rest, but only initially, before the collision. We still want to have an inertial frame, which doesn't accelerate with the ball.

The transformation of velocities into the rest frame of ball A is done by subtracting the velocity vector of A from all velocities:

Then optionally, for the transformation into coordiantes aligned with the contact normal/tangent you need a rotation matrix, where the rows are the transposed base vectors of your rotated coordinate system (unit contact normal & unit tangent):
https://en.wikipedia.org/wiki/Rotation_matrix#In_two_dimensions

In this rotated frame rest frame of one of the balls you have momentum transfer only along one axis, while the velocities components along the other do not change (assuming no friction). So its a 1D collision with one mass initially at rest.

Once you computed the velocities after the collision, you transform them back to your original frame, by applying the inverse transformations in reverse order: Multiply with the transposed version of the above rotation matrix then add the initial velocity vector of A.

Note that we have not used a single trigonometric function in the whole process, just one square root to get the unit normal vector. This is likely to be more efficient code, despite the many additions and multiplications involved in the vector math.

Last edited:
• PeroK and jbriggs444
After researching into vcm, I have deduced something really interesting which will help me immensely on my journey, and all thanks to all of you for guiding me on my way.

Right now I am exploring the effects of 2 colliding balls at different speeds only on x axis (albeit at different angles at point of collision). At the moment, to help me understand this better, I am sticking with equal masses.

I have just realised that the velocities of the balls after collision deviating from the vcm are equal to the vcm (in opposite directions, obviously).

Also, from the vcm frame, the angles at which the balls deviate are equal to the angle at which the collision occurred.

Using the vcm frame helps simplify everything wonderfully.

I'm still pondering on all of these relationships and trying to connect everything in my mind.

Really interesting stuff.

#### Attachments

• 20230403_115924.jpg
35.2 KB · Views: 29
Using the vcm frame helps simplify everything wonderfully.
You mean the center-of-mass-rest-frame? Yes, that can be another convenient frame.

Also, from the vcm frame, the angles at which the balls deviate are equal to the angle at which the collision occurred.
Note that for the center-of-mass-rest-frame you are still free to orient you axes as you want. A convenient choice is to orient them along the contact normal/tangent, as described above. This eliminates the angles completely.

• jbriggs444
I thought that the rest frame was the centre of mass frame... (according to what I've understood so far).

Yes, I've learnt that using the vcm simplifies everything brilliantly.

That's right.... I would code JavaScript to calculate the vector components between the centres of the balls and use the inverse tangent method to calculate the angle.

Then this angle would be the one which the two balls deviate from relative to the point of centre of mass.

I am really enjoying this interesting exploration of physics.

This recent study has been a huge revelation, cos I've always been interested in physics but never really researched the mathematics of it, until I decided to code a collision model in JavaScript.

Then it's been a rollercoaster ride of problems and questions and discoveries again and again.

Next step is for me to explore the relationship between the vcm and initial velocities to work out the final angles and velocities.

Thank you for your help so far.

That's right.... I would code JavaScript to calculate the vector components between the centres of the balls and use the inverse tangent method to calculate the angle.
What do you need the angle for? You can transform your velocities into the contact-normal/tangent frame using a simple 2x2 matrix, with the base vectors as rows.

Let c1, c2 be the position vectors of the centers of the balls on collision. The unit contact normal is
n = (c1 - c2) / ||(c1 - c2)||
The tangent is perpendicular to the normal:
t = (-n_y, n_x)

The 2x2 rotation matrix R is:
n_x n_y
t_x t_y

You transform your velocity vectors by matrix-vector multiplication:
v' = R * v
https://mathinsight.org/matrix_vector_multiplication

To transform back the final vectors after collision you use the transposed matrix RT:
n_x t_x
n_y t_y

v = RT * v'

Where:
v : a velocity in center of mass frame (or rest frame of a ball), but with the original (global) axes
v' : a velocity in center of mass frame (or rest frame of a ball), but with n as x-axis, and t as y-axis.

Without friction all v_y' remain constant during the collision, while the v_x' follow the rules of 1D collisions:
https://en.wikipedia.org/wiki/Elastic_collision#One-dimensional_Newtonian

• PeroK