More Complex Animations

We have set up a simple animation in the last lecture. But in many of my classes, students want something more. You are not satisfied with making one ball bounce around, so you want more. (GO figure!)

As it turns out, we can do this. I have found a link with some information we can use to handle more than one ball. Now, I could just translate this code into Python and give it to you, but this is a learning environment, so instead we will do this a bit differently!

Here is a chunk of code (not Python) that says it handles two balls colliding with each other:

//Check collision between two bodies
function collides(b1, b2) {
    //Find the distance between their mid-points
    var dx = b1.x - b2.x,
        dy = b1.y - b2.y,
        dist = Math.round(Math.sqrt(dx*dx + dy*dy));

    //Check if it is a collision
    if(dist <= (b1.r + b2.r)) {
        //Calculate the angles
        var angle = Math.atan2(dy, dx),
            sin = Math.sin(angle),
            cos = Math.cos(angle);

        //Calculate the old velocity components
        var v1x = b1.vx * cos,
            v2x = b2.vx * cos,
            v1y = b1.vy * sin,
            v2y = b2.vy * sin;

        //Calculate the new velocity components
        var vel1x = ((b1.m - b2.m) / (b1.m + b2.m)) * v1x + (2 * b2.m / (b1.m + b2.m)) * v2x,
            vel2x = (2 * b1.m / (b1.m + b2.m)) * v1x + ((b2.m - b1.m) / (b2.m + b1.m)) * v2x,
            vel1y = v1y,
            vel2y = v2y;

        //Set the new velocities
        b1.vx = vel1x;
        b2.vx = vel2x;
        b1.vy = vel1y;
        b2.vy = vel2y;
    }
}

The physics behind all this code are not something we need to worry about in this class. If you are interested, basically what we do is figure out how the two balls hit each other and do a “collision” as if the balls had hit a wall aligned between them and drawn perpendicular to a line drawn from ball center to ball center. Actually, there is an exchange in “momentum” between the two balls. You have probably seen this. If a pool ball is sitting still and is hit squarely by the cue ball, the cue ball screeches to a stop, and the first ball takes off in the direction the cue ball was traveling. That all accounts for the math you see in this code!

Sticky balls

The code shown above was submitted to a discussion group by a programmer who noticed a problem. We are simulating the passing of time by spinning around in a loop. During that interval of time, the balls move along a straight line to a new point. At the end of that move, we check to see if they have hit anything and handle the collision. There is a minor problem that shows up occasionally and needs some thought.

It will turn out that we do not detect the actual moment when the ball collides with something. Actually, it will move “into” the object it collides with!

For the two ball collision, the two balls are actually slightly inside each other, and that causes the problem.

Depending on the direction and speed with which the two balls are moving, the new velocities after a collision may not make the balls move away from each other enough to get completely away. Remember that we have actually let the balls move “into” each other before we detect a collision. Once we set in the new velocities, the balls do start moving away from each other, but do they move far enough away to not still be too close in the next spin around the loop?

Most of the time, things work jut fine, but every once in a while, the balls are still too close, so they bounce again. This time they end up moving closer to each other, and will bounce again. In the end, they will look like they are stuck to each other.

The first time I saw this, I had to think it through, and finally understood what was going on. I liked the look of it so I left it in. Once in a while I saw two stuck together balls grab a third. It was crazy enough to be fun. I still have that code somewhere, although it was written in C++.

Extra credit

If you want some extra credit on your pool-ball lab (like twice normal credit). See if you can adapt this code to get at two balls bouncing off each other.

You will need to move two balls, and set up variables to control each of them independently. It should not be too hard to get two balls bouncing off the walls, but the code to check to see if they hit each other is the big change. You should be able to see through the sample code above and get something working.