The following script demonstrates colliding superballs. It could also be modified to resemble billiard balls (by setting gravity to zero, and adjusting some of the other constants).

Super Balls

You can model a simple ball as having a center, given by the movie clips's _x and_y, and a radius r (which would typically be close to or identical to the movieclips's _width/2.

The following code will give you a very simple 'pong' style model.

Code:

mc._x; // ball center x
mc._y; // ball center y
mc.rad = mc._width/2; // ball radius
mc.vx = 0; // used to store the ball's velocity X
mc.vy = 0; // used to store the ball's velocity y

You start a ball in motion by modifying it's velocity, like so:

mc.vx = 5;

mc.vy = 2;

The ball can update it's movement each frame like so:

Code:

mc.onEnterFrame = function()
{
// Perform modifications to this.vx, this.vy here - collision checks, wall checks etc.
// ...
this._x += this.vx;
this._y += this.vy;
}

When the ball hits a left or right wall, reverse it's x velocity.

mc.vx *= -1;

When it hits a top or bottom wall, reverse its y velocity.

mc.vy *= -1;

For collision with other balls, you can use hitTest to determine collision, or you can do it manually, like in the following script (taken from the 'superballs' script above), which handles collisions for an array of balls:

Code:

dampCollision = .95;
ballWidth = 100; // standard ball width in this example
MovieClip.prototype.handleCollisions = function()
{
for (var i = mcs.length-1; i >= 1; --i) {
var x1 = mcs[i]._x;
var y1 = mcs[i]._y;
for (j = i-1; j >= 0; --j) {
var dx = mcs[j]._x - x1;
var dy = mcs[j]._y - y1;
var dist = Math.sqrt(dx*dx+dy*dy);
if (dist < ballWidth) {
// collide - trade energy (mag1,mag2), and travel in opposite direction of collision
var mag1 = Math.sqrt(mcs[i].vx*mcs[i].vx+mcs[i].vy*mcs[i].vy);
var mag2 = Math.sqrt(mcs[j].vx*mcs[j].vx+mcs[j].vy*mcs[j].vy);
mcs[j].vx = (mag1*dx/dist)*dampCollision;
mcs[j].vy = (mag1*dy/dist)*dampCollision;
mcs[i].vx = -(mag2*dx/dist)*dampCollision;
mcs[i].vy = -(mag2*dy/dist)*dampCollision;
}
}
}
}

- Jim