Programming

Ball to Ball Collision - Detection and Handling

25 September 2026 · 9 min read

Ball to Ball Collision - Detection and Handling

Understanding ball-to-ball collision detection and handling is crucial in various applications, from game development and physics simulations to robotics and virtual reality. Whether you’re creating a realistic billiards game, simulating molecular interactions, or designing a robotic system involving spherical objects, accurately detecting and responding to collisions is essential for creating believable and interactive experiences. This article dives deep into the intricacies of ball-to-ball collision, exploring the underlying principles, algorithms, and practical implementation techniques.

Detecting Collisions

The first step in handling ball-to-ball collisions is detecting when they occur. The most common approach involves checking the distance between the centers of the two balls. If the distance is less than or equal to the sum of their radii, a collision has occurred. This simple yet effective method utilizes the fundamental geometric properties of spheres.

More specifically, if we denote the centers of two balls as C1 and C2, and their radii as r1 and r2, respectively, a collision occurs when:

distance(C1, C2) ≤ r1 + r2

This calculation is computationally efficient and forms the basis of collision detection in many systems.

Responding to Collisions: Elastic Collisions

Once a collision is detected, the next step is to determine how the balls will react. In an ideal elastic collision, both momentum and kinetic energy are conserved. This means the balls will bounce off each other without any loss of energy. Calculating the resulting velocities involves applying the principles of conservation of momentum and kinetic energy along the line of collision.

For instance, consider two balls with identical mass. In a head-on collision, they will exchange velocities. If one ball is stationary, the moving ball will stop, and the stationary ball will move with the initial velocity of the first ball.

  • Momentum is always conserved in collisions.
  • Kinetic energy is conserved in perfectly elastic collisions.

Responding to Collisions: Inelastic Collisions

In the real world, collisions are rarely perfectly elastic. Some energy is lost as heat, sound, or deformation. These are known as inelastic collisions. In such cases, the coefficient of restitution (COR), a value between 0 and 1, is used to represent the “bounciness” of the collision. A COR of 1 represents a perfectly elastic collision, while a COR of 0 represents a perfectly inelastic collision, where the balls stick together after impact.

Accurately modeling inelastic collisions adds a layer of realism to simulations. Imagine a tennis ball hitting the ground; the COR dictates how high it will bounce. This principle is fundamental to creating believable physics engines.

  1. Calculate the relative velocity of the two balls.
  2. Multiply the relative velocity by the negative COR.
  3. Apply the resulting change in velocity to each ball.

Advanced Collision Handling: Multiple Balls

Handling collisions involving multiple balls introduces further complexity. A naive approach would involve checking every pair of balls for collisions, which becomes computationally expensive as the number of balls increases. Optimized algorithms, such as spatial partitioning techniques like quadtrees or octrees, can significantly improve performance by dividing the space into smaller regions and only checking for collisions within those regions.

For complex systems involving hundreds or thousands of balls, optimized collision handling is essential for maintaining real-time performance. These techniques minimize unnecessary calculations, leading to more efficient simulations.

According to a study published in the Journal of Computational Physics, spatial partitioning can reduce the computational cost of collision detection by several orders of magnitude in large-scale simulations.

“Efficient collision detection is the cornerstone of realistic physics simulations.” - Dr. Jane Doe, Physics Professor, University of X

For example, consider a pool game simulation. Efficient collision detection algorithms ensure that the game runs smoothly even with many balls on the table. This allows for a realistic and responsive gaming experience.

Learn more about collision detection optimization techniques.Infographic Placeholder: Illustrating different collision scenarios and their corresponding responses.

Frequently Asked Questions (FAQ)

Q: What is the difference between elastic and inelastic collisions?

A: In elastic collisions, both momentum and kinetic energy are conserved, while in inelastic collisions, some kinetic energy is lost.

Implementing robust ball-to-ball collision detection and handling is a cornerstone of creating realistic and engaging interactive experiences. From simple games to complex scientific simulations, understanding the principles and techniques discussed in this article is essential for achieving accurate and efficient collision responses. By leveraging these concepts, developers can unlock the full potential of their applications and build immersive environments that accurately reflect the physical world. Explore further resources and experiment with different approaches to master this crucial aspect of game development and simulation. Delve deeper into specific collision algorithms and consider the nuances of your particular application to optimize your implementation further. Learn more about specific collision algorithms. Explore various physics engines. Deepen your understanding of game physics.

  • Collision Detection
  • Collision Response

Question & Answer :
With the help of the Stack Overflow community I’ve written a pretty basic-but fun physics simulator.

alt text

You click and drag the mouse to launch a ball. It will bounce around and eventually stop on the “floor”.

My next big feature I want to add in is ball to ball collision. The ball’s movement is broken up into a x and y speed vector. I have gravity (small reduction of the y vector each step), I have friction (small reduction of both vectors each collision with a wall). The balls honestly move around in a surprisingly realistic way.

I guess my question has two parts:

  1. What is the best method to detect ball to ball collision?
    Do I just have an O(n^2) loop that iterates over each ball and checks every other ball to see if it’s radius overlaps?
  2. What equations do I use to handle the ball to ball collisions? Physics 101
    How does it effect the two balls speed x/y vectors? What is the resulting direction the two balls head off in? How do I apply this to each ball?

alt text

Handling the collision detection of the “walls” and the resulting vector changes were easy but I see more complications with ball-ball collisions. With walls I simply had to take the negative of the appropriate x or y vector and off it would go in the correct direction. With balls I don’t think it is that way.

Some quick clarifications: for simplicity I’m ok with a perfectly elastic collision for now, also all my balls have the same mass right now, but I might change that in the future.


Edit: Resources I have found useful

2d Ball physics with vectors: 2-Dimensional Collisions Without Trigonometry.pdf
2d Ball collision detection example: Adding Collision Detection


Success!

I have the ball collision detection and response working great!

Relevant code:

Collision Detection:

for (int i = 0; i < ballCount; i++) { for (int j = i + 1; j < ballCount; j++) { if (balls[i].colliding(balls[j])) { balls[i].resolveCollision(balls[j]); } } } 

This will check for collisions between every ball but skip redundant checks (if you have to check if ball 1 collides with ball 2 then you don’t need to check if ball 2 collides with ball 1. Also, it skips checking for collisions with itself).

Then, in my ball class I have my colliding() and resolveCollision() methods:

public boolean colliding(Ball ball) { float xd = position.getX() - ball.position.getX(); float yd = position.getY() - ball.position.getY(); float sumRadius = getRadius() + ball.getRadius(); float sqrRadius = sumRadius * sumRadius; float distSqr = (xd * xd) + (yd * yd); if (distSqr <= sqrRadius) { return true; } return false; } public void resolveCollision(Ball ball) { // get the mtd Vector2d delta = (position.subtract(ball.position)); float d = delta.getLength(); // minimum translation distance to push balls apart after intersecting Vector2d mtd = delta.multiply(((getRadius() + ball.getRadius())-d)/d); // resolve intersection -- // inverse mass quantities float im1 = 1 / getMass(); float im2 = 1 / ball.getMass(); // push-pull them apart based off their mass position = position.add(mtd.multiply(im1 / (im1 + im2))); ball.position = ball.position.subtract(mtd.multiply(im2 / (im1 + im2))); // impact speed Vector2d v = (this.velocity.subtract(ball.velocity)); float vn = v.dot(mtd.normalize()); // sphere intersecting but moving away from each other already if (vn > 0.0f) return; // collision impulse float i = (-(1.0f + Constants.restitution) * vn) / (im1 + im2); Vector2d impulse = mtd.normalize().multiply(i); // change in momentum this.velocity = this.velocity.add(impulse.multiply(im1)); ball.velocity = ball.velocity.subtract(impulse.multiply(im2)); } 

Source Code: Complete source for ball to ball collider.

If anyone has some suggestions for how to improve this basic physics simulator let me know! One thing I have yet to add is angular momentum so the balls will roll more realistically. Any other suggestions? Leave a comment!

To detect whether two balls collide, just check whether the distance between their centers is less than two times the radius. To do a perfectly elastic collision between the balls, you only need to worry about the component of the velocity that is in the direction of the collision. The other component (tangent to the collision) will stay the same for both balls. You can get the collision components by creating a unit vector pointing in the direction from one ball to the other, then taking the dot product with the velocity vectors of the balls. You can then plug these components into a 1D perfectly elastic collision equation.

Wikipedia has a pretty good summary of the whole process. For balls of any mass, the new velocities can be calculated using the equations (where v1 and v2 are the velocities after the collision, and u1, u2 are from before):

v_{1} = \frac{u_{1}(m_{1}-m_{2})+2m_{2}u_{2}}{m_{1}+m_{2}}

v_{2} = \frac{u_{2}(m_{2}-m_{1})+2m_{1}u_{1}}{m_{1}+m_{2}}

If the balls have the same mass then the velocities are simply switched. Here’s some code I wrote which does something similar:

void Simulation::collide(Storage::Iterator a, Storage::Iterator b) { // Check whether there actually was a collision if (a == b) return; Vector collision = a.position() - b.position(); double distance = collision.length(); if (distance == 0.0) { // hack to avoid div by zero collision = Vector(1.0, 0.0); distance = 1.0; } if (distance > 1.0) return; // Get the components of the velocity vectors which are parallel to the collision. // The perpendicular component remains the same for both fish collision = collision / distance; double aci = a.velocity().dot(collision); double bci = b.velocity().dot(collision); // Solve for the new velocities using the 1-dimensional elastic collision equations. // Turns out it's really simple when the masses are the same. double acf = bci; double bcf = aci; // Replace the collision velocity components with the new ones a.velocity() += (acf - aci) * collision; b.velocity() += (bcf - bci) * collision; } 

As for efficiency, Ryan Fox is right, you should consider dividing up the region into sections, then doing collision detection within each section. Keep in mind that balls can collide with other balls on the boundaries of a section, so this may make your code much more complicated. Efficiency probably won’t matter until you have several hundred balls though. For bonus points, you can run each section on a different core, or split up the processing of collisions within each section.