Collision Detection

The last thing I want to cover in this introduction to 3D is collision detection. The only feasible way of doing collision detection in 3D in Flash is distance-based . This is not too much different from collision detection in 2D. You find the distance between two objects (using the 3D distance formula), and if that is less than the sum of their radii, you have a hit.

For a 3D collision detection example, I altered one of the earlier 3D bouncing examples, giving it fewer movie clips and more space. I also altered the ball movie clip, giving it two frames . The first has a red ball on it, the second has an identical ball in blue. It has a stop(); action on the first frame to prevent it from looping back and forth between the two.

In the code, I first do the normal 3D motion and perspective, and then do a double for loop to compare all the balls locations. If any are less distance apart than twice their radius, I turn them both blue, by sending them to frame 2. Its pretty simple. Heres the code ( ch15_13.fla ):

 var numBalls:Number = 20; var top:Number = -200; var bottom:Number = 200; var left:Number = -200; var right:Number = 200; var front:Number = 200; var back:Number = -200; var radius:Number = 15; var fl:Number = 250; var vpX:Number = Stage.width / 2; var vpY:Number = Stage.height / 2; init(); function init() {       for (var i:Number = 0; i<numBalls; i++) {             var ball:MovieClip = attachMovie("ball", "ball" + i, i);             ball.x = Math.random() * 500 - 250;             ball.y = Math.random() * 500 - 250;             ball.z = Math.random() * 500 - 250;             ball.vx = Math.random() * 10 - 5;             ball.vy = Math.random() * 10 - 5;             ball.vz = Math.random() * 10 - 5;      } } function onEnterFrame():Void {       for (var i:Number=0;i<numBalls;i++) {             var ball:MovieClip = this["ball" + i];             ball.x += ball.vx;             ball.y += ball.vy;             ball.z += ball.vz;             if (ball.x + radius > right) {                   ball.x = right - radius;                   ball.vx *= -1;             } else if (ball.x - radius < left) {                   ball.x = left + radius;                   ball.vx *= -1;             }             if (ball.y + radius > bottom) {                   ball.y = bottom - radius;                   ball.vy *= -1;             } else if (ball.y - radius < top) {                   ball.y = top + radius;                   ball.vy *= -1;             }             if (ball.z + radius > front) {                   ball.z = front - radius;                   ball.vz *= -1;             } else if (ball.z - radius < back) {                   ball.z = back + radius;                   ball.vz *= -1;             }             if (ball.z <= -fl) {                   ball._visible = false;             } else {                   ball._visible = true;                   var scale:Number = fl / (fl + ball.z);                   ball._xscale = ball._yscale=scale*100;                   ball._x = vpX + ball.x * scale;                   ball._y = vpY + ball.y * scale;                   ball.swapDepths(-ball.z);             }        }  for(var i:Number = 0;i<numBalls-1;i++)   {   var ballA:MovieClip = this["ball" + i];   for(var j:Number = i+1;j<numBalls;j++)   {   var ballB:MovieClip = this["ball" + j];   var dx:Number = ballA.x - ballB.x;   var dy:Number = ballA.y - ballB.y;   var dz:Number = ballA.z - ballB.z;   var dist:Number = Math.sqrt(dx*dx + dy*dy + dz*dz);   if(dist < radius * 2)   {   ballA.gotoAndStop(2);   ballB.gotoAndStop(2);   }   }   }  } 

The key part is in bold. The balls start out all red, and as they collide, they change color . Before long, all are blue.



Foundation ActionScript. Animation. Making Things Move
Foundation Actionscript 3.0 Animation: Making Things Move!
ISBN: 1590597915
EAN: 2147483647
Year: 2005
Pages: 137
Authors: Keith Peters

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net