Managing multiple balls collisions with Flash: AS3 version
Filed Under Actionscript 3, Flash, Tutorials, Users contributions •
Some days ago Sunil Changrani sent me a the script to manage multiple balls collisions with Flash, in AS2.
Now, it's time for Andrew Cook to give us the AS3 version.
Hi Emanuele,
I've been coming to your blog for over a year now. I am doing a new game with Ball vs. Ball collision and turn to your tutorials for help. I am programming my game in AS3, so I reprogrammed the tutorial in AS3. I thought you might want to check it out. There are three files. The main .fla, a document class, and a Ball class.
Thank you for starting your great blog that helps developers like me all the time. I really appreciate the way you give back to the development community. Keep it up!
This is the script to solve collisions:
-
package
-
{
-
import flash.display.MovieClip;
-
import flash.display.Stage;
-
import flash.events.Event;
-
import flash.events.KeyboardEvent;
-
-
public class BallCollision extends MovieClip
-
{
-
private var mcBallContainer:MovieClip;
-
private var nNumBalls:Number = 15;
-
-
private var nStageWidth:Number = 500;
-
private var nStageHeight:Number = 400;
-
-
public function BallCollision ( ) : void
-
{
-
mcBallContainer = new MovieClip ( ) ;
-
mcBallContainer.x = mcBallContainer.y = 0;
-
stage.addChild(mcBallContainer);
-
-
this.addEventListener ( Event.ENTER_FRAME, enterFrameHandler );
-
-
for ( var i = 0; i <nNumBalls; i++ )
-
{
-
var mcBall:Ball = new Ball ( Math.floor ( ( Math.random() * 12 ) - 4 ), Math.floor ( ( Math.random() * 12 ) - 4 ) );
-
mcBall.x = Math.random() * nStageWidth;
-
mcBall.y = Math.random() * nStageHeight;
-
mcBallContainer.addChild ( mcBall );
-
}
-
}
-
-
private function enterFrameHandler ( E:Event ) : void
-
{
-
for ( var i = 0; i <mcBallContainer.numChildren; i++ )
-
{
-
var mcBall1:* = mcBallContainer.getChildAt( i );
-
-
for ( var j = i + 1; j <mcBallContainer.numChildren; j++ )
-
{
-
var mcBall2:* = mcBallContainer.getChildAt( j );
-
-
var nDistX:Number = Math.abs ( mcBall1.x - mcBall2.x );
-
var nDistY:Number = Math.abs ( mcBall1.y - mcBall2.y );
-
var nDistance:Number = Math.sqrt ( nDistX * nDistX + nDistY * nDistY );
-
-
if ( nDistance <20 )
-
{
-
solveBalls ( mcBall1, mcBall2 );
-
}
-
}
-
}
-
}
-
-
private function solveBalls ( MCBallA:MovieClip, MCBallB:MovieClip ) : void
-
{
-
var nX1:Number = MCBallA.x;
-
var nY1:Number = MCBallA.y;
-
var nDistX:Number = MCBallB.x - nX1;
-
var nDistY:Number = MCBallB.y - nY1;
-
-
var nDistance:Number = Math.sqrt ( nDistX * nDistX + nDistY * nDistY );
-
var nRadiusA:Number = MCBallA.width/2;
-
var nRadiusB:Number = MCBallB.width/2;
-
//var nRadius:Number = 10;
-
-
var nNormalX:Number = nDistX/nDistance;
-
var nNormalY:Number = nDistY/nDistance;
-
-
var nMidpointX:Number = ( nX1 + MCBallB.x )/2;
-
var nMidpointY:Number = ( nY1 + MCBallB.y )/2;
-
-
MCBallA.x = nMidpointX - nNormalX * nRadiusA;
-
MCBallA.y = nMidpointY - nNormalY * nRadiusA;
-
MCBallB.x = nMidpointX + nNormalX * nRadiusB;
-
MCBallB.y = nMidpointY + nNormalY * nRadiusB;
-
-
var nVector:Number = ( ( MCBallA.nSpeedX - MCBallB.nSpeedX ) * nNormalX )+ ( ( MCBallA.nSpeedY - MCBallB.nSpeedY ) * nNormalY );
-
var nVelX:Number = nVector * nNormalX;
-
var nVelY:Number = nVector * nNormalY;
-
-
MCBallA.nSpeedX -= nVelX;
-
MCBallA.nSpeedY -= nVelY;
-
MCBallB.nSpeedX += nVelX;
-
MCBallB.nSpeedY += nVelY;
-
}
-
}
-
}
And this is the one to manage balls
-
package
-
{
-
import flash.events.Event;
-
import flash.display.MovieClip;
-
-
public class Ball extends MovieClip
-
{
-
public var nSpeedX:Number;
-
public var nSpeedY:Number;
-
-
private var nStageWidth:Number = 500;
-
private var nStageHeight:Number = 400;
-
-
public function Ball ( NSpeedX:Number, NSpeedY:Number ) : void
-
{
-
nSpeedX = NSpeedX;
-
nSpeedY = NSpeedY;
-
-
this.addEventListener ( Event.ENTER_FRAME, onEnterFrameHandler );
-
}
-
-
private function onEnterFrameHandler ( E:Event ) : void
-
{
-
this.x -= nSpeedX;
-
this.y -= nSpeedY;
-
-
if ( this.x>= nStageWidth - 10 )
-
{
-
this.x = nStageWidth - 10;
-
nSpeedX *= -1;
-
}
-
else if ( this.x <= 10 )
-
{
-
this.x = 10;
-
nSpeedX *= -1;
-
}
-
-
if ( this.y>= nStageHeight - 10 )
-
{
-
this.y = nStageHeight - 10;
-
nSpeedY *= -1;
-
}
-
else if ( this.y <= 10 )
-
{
-
this.y = 10;
-
nSpeedY *= -1;
-
}
-
-
}
-
}
-
}
Enjoy the result
Tell me what do you think about this post. I'll write better and better entries.
They can be easily customized to meet the unique requirements of your project.
15 Responses to “Managing multiple balls collisions with Flash: AS3 version”
Leave a Reply

(5 votes, average: 4.8 out of 5)
How many balls can this handle before it lags?
Nice post!
I was able to get to a 1500 ball count before I saw a major chuck of lag. :) AS3 is no joke and it is only getting better :)
Thats quite a leap from the 300 we saw in AS2
I have CS3 and I think I will take the leap to AS3
after today!
How would u make an angle that the ball would hit? like a ball hitting an angled wall.
|
|
O - - -/
okay the lines didnt work out well lol. lets try this again.
…..|
…..|
…..|
O—-/
where the dashes are the balls old places.
Balls are touching. Gay :P
change
var nDistance:Number = Math.sqrt ( nDistX * nDistX + nDistY * nDistY );
if ( nDistance <20 )
{
to
var nDistanceSqr:Number = (nDistX * nDistX) + (nDistY * nDistY);
if ( nDistanceSqr <400 )
{
for extra speed.
Additionally, There is no need to take the absolute value of nDistX and nDistY in these lines…
var nDistX:Number = Math.abs ( mcBall1.x - mcBall2.x );
var nDistY:Number = Math.abs ( mcBall1.y - mcBall2.y );
Once you square them it doesn’t matter if they are negative or not. Shouldn’t be a huge boost but every little bit helps. Also, while we are optimizing there is no need really to declare nDistX and nDistY explicitly but I can see why it would be helpful for the tutorial.
Great work!!
Any thoughts on what happens if a ball has simultanious collisions from both sides?
AKA Newtons cradle?
Does anyone know how to add the functionality of being able to grab, hold and throw these balls? This is a great effort btw.
does anyone know how the collisionBall cobject is created? i cant see it anywhere.
u say 1500????
it laggs with me with 300 balls
150 balls is ok on my pc.
Hey Emanuele,
I’m just started using AS3. I’m creating a game where you drive a bumper car, there’s two variables, speed and rotate. Is there way you can make a realistic bounce off the walls. Thanx
[...] Read more [...]
Andrew, this is great, *however* you have not commented your code, in particular your physics (use of the dot product etc.) so you are assuming everyone knows what this does. I ranked this 4 out of 5, it is otherwise a superbly well structured and relatively fast rehash on the bouncy ball theme.