278

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!

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
mmcdole
  • 91,488
  • 60
  • 186
  • 222
  • 19
    I don't think this algorithm is good enough because if your balls are moving too fast (ex: faster then 2*radius per frame, one ball can pass through another ball without any collisions. – Benji Mizrahi Sep 10 '09 at 22:58
  • @Simulcal could you upload your source code again (all the filedropper.com links appear to be broken). Also could you put up the pdf file you got from [geocities.com/vobarian/2dcollisions/2dcollisions.pdf] as geocities has gone offline recently – bguiz Nov 04 '09 at 09:58
  • 1
    Here is a link to the last version of BallBounce I worked on: http://dl.dropbox.com/u/638285/ballbounce.rar – mmcdole Nov 13 '09 at 05:53
  • @To all who contributed: Can you please shed some light to transform this engine to 3D. How this great engine can also work in Java3D. – static void main May 20 '11 at 14:08
  • 2
    Line `Vector2d impulse = mtd.multiply(i);` should be i * the normalized mtd vector. Something like: `Vector2d impulse = mtd.normalize().multiply(i);` – klenwell Nov 26 '12 at 20:30
  • is there any equation or a system of equations, which can predict if and when two balls will collide? – d.k Oct 09 '17 at 16:37
  • klenwell is right, mtd should be normalized before multiplying to i (other way balls gets crazy after impact). I'll edit the solution given by the OP. – Paco Abato Feb 02 '19 at 19:45
  • As I see here, a better way of implementing it is not mentioned. [![Event driven molecular dynamics](http://i.stack.imgur.com/YnzNk.jpg)](http://i.stack.imgur.com/YnzNk.jpg) I'll refer you to "How to Simulate Billiards and Similar Systems" by Boris D. Lubachevsky, available on arxiv: http://arxiv.org/abs/cond-mat/0503627 In the attached picture is a screenshot of a program I intend to make open source when I'll finish it. Even in an early stage is running with 5000 spheres pretty smoothly. Hopefully will do even better although I don't want to implement sectoring, I want to keep the code easy – Adrian Roman Aug 03 '16 at 20:38
  • And what is "Constants.restitution"? – zezba9000 Jul 26 '21 at 23:39

15 Answers15

123

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.

Community
  • 1
  • 1
Jay Conrod
  • 28,943
  • 19
  • 98
  • 110
  • 2
    Lets say masses of the two balls aren't equal. How does that effect the vector change between balls? – mmcdole Dec 06 '08 at 03:53
  • 3
    It's been a while since grade 12, but I think they get a ratio of the momentum corresponding to the ratio of the masses. – Ryan Fox Dec 06 '08 at 03:59
  • 6
    @Jay, just to point out.. that one equation image you added is for a 1 dimensional collision, not 2 dimensional. – mmcdole Dec 06 '08 at 05:44
  • 1
    @simucal. not true... u and v are vectors in that equation. That is, they have x, y (and z) components. – Andrew Rollings Dec 06 '08 at 14:34
  • 2
    @Simucal, you are right, they are for the one dimensional case. For more dimensions, just use the components of the velocity that are in line with the collision (aci, bci in code). The other components are orthogonal to the collision and will not change, so you don't need to worry about them. – Jay Conrod Dec 06 '08 at 15:40
  • what will be the direction after collision ? – Shohanur Rahaman Jan 23 '16 at 17:23
  • How does the code change if mass is taken into account? – Tomáš Zato Mar 25 '16 at 01:16
  • I took your code and rewrote it to my programming language and made this [web demo](https://nbasic.net/apps/bouncing-balls.html) - with source code. What I had to add was the query in case of a collision if the objects move away from each other. In this case the collision was already handled and there is nothing more to do. @mmcdole's solution has this query. – chkas Sep 30 '20 at 17:40
51

Well, years ago I made the program like you presented here.
There is one hidden problem (or many, depends on point of view):

  • If the speed of the ball is too high, you can miss the collision.

And also, almost in 100% cases your new speeds will be wrong. Well, not speeds, but positions. You have to calculate new speeds precisely in the correct place. Otherwise you just shift balls on some small "error" amount, which is available from the previous discrete step.

The solution is obvious: you have to split the timestep so, that first you shift to correct place, then collide, then shift for the rest of the time you have.

avp
  • 4,895
  • 4
  • 28
  • 40
  • If shift positions on `timeframelength*speed/2`, then positions would be statistically fixed. – Nakilon Oct 07 '11 at 07:02
  • @Nakilon: no, it helps only in some cases, but generally it is possible to miss the collision. And the probability to miss the collision increases with the size of the timeframelength. By the way, it seems that Aleph demonstrated the correct solution (I just skimmed it though). – avp Oct 11 '11 at 21:43
  • 1
    @avp, I was not about *If the speed of the ball is too high, you can miss the collision.*, but about *your new positions will be wrong*. Because of collision is being detected a bit later, than they really collided, if substract `timeframelength*speed/2` from that position, accuracy will increase twice. – Nakilon Oct 12 '11 at 00:05
20

You should use space partitioning to solve this problem.

Read up on Binary Space Partitioning and Quadtrees

grepsedawk
  • 5,959
  • 5
  • 24
  • 22
  • 4
    Instead of space partitioning, wouldn't a [sweep and prune](http://en.wikipedia.org/wiki/Sweep_and_prune) algorithm work better? the balls are moving fast, so any partitioning will have to be updated frequently, incurring overhead. A sweep and prune could find all colliding pairs in O(n log n), without any transient data structures. [Here is a good tutorial for the basics](http://jitter-physics.com/wordpress/?tag=sweep-and-prune) – HugoRune Jun 14 '12 at 22:45
13

As a clarification to the suggestion by Ryan Fox to split the screen into regions, and only checking for collisions within regions...

e.g. split the play area up into a grid of squares (which will will arbitrarily say are of 1 unit length per side), and check for collisions within each grid square.

That's absolutely the correct solution. The only problem with it (as another poster pointed out) is that collisions across boundaries are a problem.

The solution to this is to overlay a second grid at a 0.5 unit vertical and horizontal offset to the first one.

Then, any collisions that would be across boundaries in the first grid (and hence not detected) will be within grid squares in the second grid. As long as you keep track of the collisions you've already handled (as there is likely to be some overlap) you don't have to worry about handling edge cases. All collisions will be within a grid square on one of the grids.

Andrew Rollings
  • 14,340
  • 7
  • 51
  • 50
  • +1 for a more accurate solution, and to counter the cowardly drive-by downvoter – Steven A. Lowe Dec 06 '08 at 06:29
  • 1
    thats a good idea. I did this once and i checked the current cell and all neighboring cells, but your method is more efficient. Another way I just thought of is to check the current cell, and then check to see if it intersects with the current cells boundaries, and if so, check objects in THAT neighboring cell. – LoveMeSomeCode Jul 27 '09 at 22:24
10

A good way of reducing the number of collision checks is to split the screen into different sections. You then only compare each ball to the balls in the same section.

Ryan Fox
  • 10,103
  • 5
  • 38
  • 48
8

One thing I see here to optimize.

While I do agree that the balls hit when the distance is the sum of their radii one should never actually calculate this distance! Rather, calculate it's square and work with it that way. There's no reason for that expensive square root operation.

Also, once you have found a collision you have to continue to evaluate collisions until no more remain. The problem is that the first one might cause others that have to be resolved before you get an accurate picture. Consider what happens if the ball hits a ball at the edge? The second ball hits the edge and immediately rebounds into the first ball. If you bang into a pile of balls in the corner you could have quite a few collisions that have to be resolved before you can iterate the next cycle.

As for the O(n^2), all you can do is minimize the cost of rejecting ones that miss:

1) A ball that is not moving can't hit anything. If there are a reasonable number of balls lying around on the floor this could save a lot of tests. (Note that you must still check if something hit the stationary ball.)

2) Something that might be worth doing: Divide the screen into a number of zones but the lines should be fuzzy--balls at the edge of a zone are listed as being in all the relevant (could be 4) zones. I would use a 4x4 grid, store the zones as bits. If an AND of the zones of two balls zones returns zero, end of test.

3) As I mentioned, don't do the square root.

Loren Pechtel
  • 8,945
  • 3
  • 33
  • 45
  • Thank you for the information on the square root tip. Didn't know about its expensive nature when compared to the square. – mmcdole Dec 06 '08 at 06:02
  • Another optimization would be to find balls which are nowhere near any other balls. This would work reliably only if the velocities of the balls are constrained. – Brad Gilbert Dec 06 '08 at 15:50
  • 1
    I disagree on looking for isolated balls. That's just as expensive as detecting the collision. To improve things you need something that's less than O(n) for the ball in question. – Loren Pechtel Dec 07 '08 at 02:25
6

I found an excellent page with information on collision detection and response in 2D.

http://www.metanetsoftware.com/technique.html (web.archive.org)

They try to explain how it's done from an academic point of view. They start with the simple object-to-object collision detection, and move on to collision response and how to scale it up.

Edit: Updated link

Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138
3

I see it hinted here and there, but you could also do a faster calculation first, like, compare the bounding boxes for overlap, and THEN do a radius-based overlap if that first test passes.

The addition/difference math is much faster for a bounding box than all the trig for the radius, and most times, the bounding box test will dismiss the possibility of a collision. But if you then re-test with trig, you're getting the accurate results that you're seeking.

Yes, it's two tests, but it will be faster overall.

Jason Kleban
  • 20,024
  • 18
  • 75
  • 125
  • 6
    You don't need trig. `bool is_overlapping(int x1, int y1, int r1, int x2, int y2, int r2) { return (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)<(r1+r2)*(r1+r2); }` – Ponkadoodle Jul 06 '10 at 03:05
3

You have two easy ways to do this. Jay has covered the accurate way of checking from the center of the ball.

The easier way is to use a rectangle bounding box, set the size of your box to be 80% the size of the ball, and you'll simulate collision pretty well.

Add a method to your ball class:

public Rectangle getBoundingRect()
{
   int ballHeight = (int)Ball.Height * 0.80f;
   int ballWidth = (int)Ball.Width * 0.80f;
   int x = Ball.X - ballWidth / 2;
   int y = Ball.Y - ballHeight / 2;

   return new Rectangle(x,y,ballHeight,ballWidth);
}

Then, in your loop:

// Checks every ball against every other ball. 
// For best results, split it into quadrants like Ryan suggested. 
// I didn't do that for simplicity here.
for (int i = 0; i < balls.count; i++)
{
    Rectangle r1 = balls[i].getBoundingRect();

    for (int k = 0; k < balls.count; k++)
    {

        if (balls[i] != balls[k])
        {
            Rectangle r2 = balls[k].getBoundingRect();

            if (r1.Intersects(r2))
            {
                 // balls[i] collided with balls[k]
            }
        }
    }
}
FlySwat
  • 172,459
  • 74
  • 246
  • 311
  • 1
    This would make the balls go into each other 20% on horizontal and vertical collisions. Might as well use circular bounding boxes, as the efficiency difference negligible. Also, `(x-width)/2` should be `x-width/2`. – Markus Jarderot Dec 06 '08 at 13:05
  • Good call on the precedence typo. You'll find that most 2d games use rectangular bounding boxes on non rectangular shapes because it is fast, and the user almost never notices anyway. – FlySwat Dec 06 '08 at 15:38
  • You could do rectangular bounding box, then if it has a hit check the circular bounding box. – Brad Gilbert Dec 06 '08 at 15:51
  • 1
    @Jonathan Holland, your inner loop should be for(int k = i + 1; ...) This will get rid of all the redundant checks. (ie checking with collision of self and checking collision ball1 with ball2 then ball2 with ball1). – mmcdole Dec 06 '08 at 16:39
  • 4
    Actually, a square bounding box is likely to be _worse_ performance-wise than a circular bounding box (assuming you've optimized the square root away) – Ponkadoodle Jul 06 '10 at 03:01
  • Performance here isn't really different than circular bounding box without square root. There's no point in the significantly lessened efficiency. – adrian May 16 '19 at 03:04
3

This KineticModel is an implementation of the cited approach in Java.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

I implemented this code in JavaScript using the HTML Canvas element, and it produced wonderful simulations at 60 frames per second. I started the simulation off with a collection of a dozen balls at random positions and velocities. I found that at higher velocities, a glancing collision between a small ball and a much larger one caused the small ball to appear to STICK to the edge of the larger ball, and moved up to around 90 degrees around the larger ball before separating. (I wonder if anyone else observed this behavior.)

Some logging of the calculations showed that the Minimum Translation Distance in these cases was not large enough to prevent the same balls from colliding in the very next time step. I did some experimenting and found that I could solve this problem by scaling up the MTD based on the relative velocities:

dot_velocity = ball_1.velocity.dot(ball_2.velocity);
mtd_factor = 1. + 0.5 * Math.abs(dot_velocity * Math.sin(collision_angle));
mtd.multplyScalar(mtd_factor);

I verified that before and after this fix, the total kinetic energy was conserved for every collision. The 0.5 value in the mtd_factor was the approximately the minumum value found to always cause the balls to separate after a collision.

Although this fix introduces a small amount of error in the exact physics of the system, the tradeoff is that now very fast balls can be simulated in a browser without decreasing the time step size.

Stefan Musarra
  • 1,429
  • 14
  • 16
2

Improving the solution to detect circle with circle collision detection given within the question:

float dx = circle1.x - circle2.x,
      dy = circle1.y - circle2.y,
       r = circle1.r + circle2.r;
return (dx * dx + dy * dy <= r * r);

It avoids the unnecessary "if with two returns" and the use of more variables than necessary.

1

After some trial and error, I used this document's method for 2D collisions : https://www.vobarian.com/collisions/2dcollisions2.pdf (that OP linked to)

I applied this within a JavaScript program using p5js, and it works perfectly. I had previously attempted to use trigonometrical equations and while they do work for specific collisions, I could not find one that worked for every collision no matter the angle at the which it happened.

The method explained in this document uses no trigonometrical functions whatsoever, it's just plain vector operations, I recommend this to anyone trying to implement ball to ball collision, trigonometrical functions in my experience are hard to generalize. I asked a Physicist at my university to show me how to do it and he told me not to bother with trigonometrical functions and showed me a method that is analogous to the one linked in the document.

NB : My masses are all equal, but this can be generalised to different masses using the equations presented in the document.

Here's my code for calculating the resulting speed vectors after collision :

    //you just need a ball object with a speed and position vector.
    class TBall {
        constructor(x, y, vx, vy) {
            this.r = [x, y];
            this.v = [0, 0];
        }
    }

    //throw two balls into this function and it'll update their speed vectors
    //if they collide, you need to call this in your main loop for every pair of 
    //balls.
    function collision(ball1, ball2) {
        n = [ (ball1.r)[0] - (ball2.r)[0], (ball1.r)[1] - (ball2.r)[1] ];
        un = [n[0] /  vecNorm(n), n[1] / vecNorm(n) ] ;
        ut = [ -un[1], un[0] ];   
        v1n = dotProd(un, (ball1.v));
        v1t = dotProd(ut, (ball1.v) );
        v2n = dotProd(un, (ball2.v) );
        v2t = dotProd(ut, (ball2.v) );
        v1t_p = v1t; v2t_p = v2t;
        v1n_p = v2n; v2n_p = v1n;
        v1n_pvec = [v1n_p * un[0], v1n_p * un[1] ]; 
        v1t_pvec = [v1t_p * ut[0], v1t_p * ut[1] ]; 
        v2n_pvec = [v2n_p * un[0], v2n_p * un[1] ]; 
        v2t_pvec = [v2t_p * ut[0], v2t_p * ut[1] ];
        ball1.v = vecSum(v1n_pvec, v1t_pvec); ball2.v = vecSum(v2n_pvec, v2t_pvec);
    }

gordon_freeman
  • 366
  • 2
  • 10
  • 2
    It is hard to read with all those single letter abbreviations and no comments. Generally, you don't want to abbreviate, unless you are writing some SIMD or GLSL. There is no optimization, and makes it hard for anyone to understand what is happening. – Alexander Jan 28 '21 at 11:49
  • @Alexander you're right, I added a couple of comments, I hope this clarifies. The thing here is that you don't _need_ to understand what is happening in the function, all that is explained in the document I linked, it's just an implementation of said document which works. – gordon_freeman Jan 30 '21 at 08:55
0

I would consider using a quadtree if you have a large number of balls. For deciding the direction of bounce, just use simple conservation of energy formulas based on the collision normal. Elasticity, weight, and velocity would make it a bit more realistic.

RollerSimmer
  • 119
  • 4
0

Here is a simple example that supports mass.

private void CollideBalls(Transform ball1, Transform ball2, ref Vector3 vel1, ref Vector3 vel2, float radius1, float radius2)
{
    var vec = ball1.position - ball2.position;
    float dis = vec.magnitude;
    if (dis < radius1 + radius2)
    {
        var n = vec.normalized;
        ReflectVelocity(ref vel1, ref vel2, ballMass1, ballMass2, n);

        var c = Vector3.Lerp(ball1.position, ball2.position, radius1 / (radius1 + radius2));
        ball1.position = c + (n * radius1);
        ball2.position = c - (n * radius2);
    }
}

public static void ReflectVelocity(ref Vector3 vel1, ref Vector3 vel2, float mass1, float mass2, Vector3 intersectionNormal)
{
    float velImpact1 = Vector3.Dot(vel1, intersectionNormal);
    float velImpact2 = Vector3.Dot(vel2, intersectionNormal);

    float totalMass = mass1 + mass2;
    float massTransfure1 = mass1 / totalMass;
    float massTransfure2 = mass2 / totalMass;

    vel1 += ((velImpact2 * massTransfure2) - (velImpact1 * massTransfure2)) * intersectionNormal;
    vel2 += ((velImpact1 * massTransfure1) - (velImpact2 * massTransfure1)) * intersectionNormal;
}
zezba9000
  • 3,247
  • 1
  • 29
  • 51