Advertisement
Guest User

Untitled

a guest
May 26th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package {
  2.     import flash.display.StageAlign;
  3.     import flash.display.StageScaleMode;
  4.     import flash.display.MovieClip;
  5.     import flash.events.Event;
  6.     import flash.geom.Point;
  7.     import DriverBubble;
  8.     import TweetBubble;
  9.    
  10.     public class DocumentMain extends MovieClip {
  11.         public var collisionParticles:Array; // the array of main objects in frame
  12.         public var wallBounce:Number = 0.7; // how much velocity is retained when bouncing off walls
  13.         public var friction:Number = 0.97; // how quickly objects slow down
  14.         public var boundaryWidth:Number = 50; // how far from stage sides the objects should stay
  15.         public var boundaryHeight:Number = 30; // how far from the top and bottom objects should stay
  16.         public var boundaryGravity:Number = 0.8; // how fast objects repel from the boundary
  17.        
  18.         public function DocumentMain() {
  19.             init();
  20.         }
  21.        
  22.         private function init() {
  23.             stage.align = StageAlign.TOP_LEFT;
  24.             stage.scaleMode = StageScaleMode.NO_SCALE;
  25.  
  26.             collisionParticles = new Array();
  27.            
  28.             // create the driver particles
  29.             for(var i = 0; i < 2; i++) {
  30.                 var randX:Number = Math.floor(Math.random() * (stage.stageWidth - 210)) + 105;
  31.                 var randY:Number = Math.floor(Math.random() * (stage.stageHeight - 210)) + 105;
  32.                 var randSize:Number = Math.random() * 40 + 60;
  33.                 var myBubble:DriverBubble = new DriverBubble();
  34.                 myBubble.setSize(randSize);
  35.                 myBubble.x = randX;
  36.                 myBubble.y = randY;
  37.                 addChild(myBubble);
  38.                 collisionParticles.push(myBubble);
  39.                
  40.                 var numTweets:Number = 20; //Math.floor(Math.random() * 20) +
  41.                 for(var j = 0; j < numTweets; j++) {
  42.                     var tweetX:Number = Math.floor(Math.random() * (stage.stageWidth - 210)) + 105;
  43.                     var tweetY:Number = Math.floor(Math.random() * (stage.stageHeight - 210)) + 105;
  44.                     var tBubble:TweetBubble = new TweetBubble();
  45.                     tBubble.setSize(4);
  46.                     tBubble.x = tweetX;
  47.                     tBubble.y = tweetY;
  48.                     addChild(tBubble);
  49.                     collisionParticles.push(tBubble);
  50.                     myBubble.tweets.push(tBubble);
  51.                 }
  52.             }
  53.             addEventListener(Event.ENTER_FRAME, onEnterFrame);
  54.         }
  55.        
  56.         private function onEnterFrame(event:Event) {
  57.             // loop for movement
  58.             for( var i:uint = 0; i < collisionParticles.length; i++ ) {
  59.                 var particle = collisionParticles[i];
  60.                 stayOnCanvas(particle);
  61.                
  62.                 //friction
  63.                 particle.vx *= friction;
  64.                 particle.vy *= friction;
  65.                
  66.                 //move
  67.                 particle.x += (particle.vx - particle.x) * .2;
  68.                 particle.y += (particle.vy - particle.y) * .2;
  69.                
  70.                 if(particle.hasOwnProperty('tweets')) {
  71.                     if(particle.tweets.length) {
  72.                         // loop for gravity in groups
  73.                         for( var k:uint = 0; k < particle.tweets.length; k++) {
  74.                             gravitate(particle.tweets[k], particle);
  75.                         }
  76.                     }
  77.                 }
  78.             }
  79.            
  80.             // loop for collision
  81.             for( i=0; i < collisionParticles.length - 1; i++ ) {
  82.                 var partA:Bubble = collisionParticles[i];
  83.                 for(var j:uint = i+1; j < collisionParticles.length; j++) {
  84.                     var partB:Bubble = collisionParticles[j];
  85.                     checkCollision(partA, partB);
  86.                     //gravitate(partA, partB);
  87.                 }
  88.             }
  89.         }
  90.         private function gravitate(partA:Bubble, partB:Bubble):void {
  91.             var dx:Number = partB.x - partA.x;
  92.             var dy:Number = partB.y - partA.y;
  93.             var distSQ:Number = dx * dx + dy * dy;
  94.             var dist:Number = Math.sqrt(distSQ);
  95.             var force:Number = partA.mass * partB.mass / distSQ;
  96.             var ax:Number = force * dx / dist;
  97.             var ay:Number = force * dy / dist;
  98.             partA.vx += ax / partA.mass;
  99.             partA.vy += ay / partA.mass;
  100.             partB.vx -= ax / partB.mass;
  101.             partB.vy -= ay / partB.mass;
  102.         }
  103.         private function checkCollision(ball0:Bubble, ball1:Bubble):void {
  104.             var dx:Number = ball1.x - ball0.x;
  105.             var dy:Number = ball1.y - ball0.y;
  106.             var dist:Number = Math.sqrt(dx*dx + dy*dy);
  107.             if(dist <= ball0.mySize + ball1.mySize) {
  108.                 //calculate angle, sine, and cosine
  109.                 var angle:Number = Math.atan2(dy, dx);
  110.                 var sin:Number = Math.sin(angle);
  111.                 var cos:Number = Math.cos(angle);
  112.                
  113.                 //rotate ball0 position
  114.                 var pos0:Point = new Point(0,0);
  115.                
  116.                 //rotate ball1 position
  117.                 var pos1:Point = rotate(dx, dy, sin, cos, true);
  118.                
  119.                 //rotate ball0 velocity
  120.                 var vel0:Point = rotate(ball0.vx, ball0.vy, sin, cos, true);
  121.                
  122.                 //rotate ball1 velocity
  123.                 var vel1:Point = rotate(ball1.vx, ball1.vy, sin, cos, true);
  124.                
  125.                 //collision reaction
  126.                 var vxTotal:Number = vel0.x - vel1.x;
  127.                 vel0.x = ((ball0.mass - ball1.mass) * vel0.x +
  128.                     2 * ball1.mass * vel1.x) /
  129.                     (ball0.mass + ball1.mass);
  130.                 vel1.x = vxTotal + vel0.x;
  131.                
  132.                 //update position
  133.                 var absV:Number = Math.abs(vel0.x) + Math.abs(vel1.x);
  134.                 var overlap:Number = (ball0.mySize + ball1.mySize)
  135.                         - Math.abs(pos0.x - pos1.x);
  136.                 pos0.x += vel0.x / absV * overlap;
  137.                 pos1.x += vel1.x / absV * overlap;
  138.                
  139.                 //rotate positions back
  140.                 var pos0F:Object = rotate(pos0.x, pos0.y, sin, cos, false);
  141.                 var pos1F:Object = rotate(pos1.x, pos1.y, sin, cos, false);
  142.                
  143.                 //adjust positions to actual screen positions
  144.                 ball1.x = ball0.x + pos1F.x;
  145.                 ball1.y = ball0.y + pos1F.y;
  146.                 ball0.x = ball0.x + pos0F.x;
  147.                 ball0.y = ball0.y + pos0F.y;
  148.                
  149.                 //rotate velocities back
  150.                 var vel0F:Object = rotate(vel0.x, vel0.y, sin, cos, false);
  151.                 var vel1F:Object = rotate(vel1.x, vel1.y, sin, cos, false);
  152.                
  153.                 ball0.vx = vel0F.x;
  154.                 ball0.vy = vel0F.y;
  155.                 ball1.vx = vel1F.x;
  156.                 ball1.vy = vel1F.y;
  157.             }
  158.         }
  159.        
  160.         private function rotate(x:Number, y:Number, sin:Number, cos:Number, reverse:Boolean):Point {
  161.             var result:Point = new Point();
  162.            
  163.             if(reverse) {
  164.                 result.x = x * cos + y * sin;
  165.                 result.y = y * cos - x * sin;
  166.             }
  167.             else {
  168.                 result.x = x * cos - y * sin;
  169.                 result.y = y * cos + x * sin;
  170.             }
  171.             return result;
  172.         }
  173.         /*
  174.         private function checkCollision(partA:Bubble, partB:Bubble):void {
  175.             var dx:Number = partB.x - partA.x;
  176.             var dy:Number = partB.y - partA.y;
  177.             var dist:Number = Math.sqrt(dx *  dx + dy * dy);
  178.             var hitDistance:Number = partA.mySize + partB.mySize;
  179.             if(dist <= hitDistance) {
  180.                 var angle:Number = Math.atan2(dy, dx);
  181.                 var tx:Number = partA.x + Math.cos(angle) * hitDistance;
  182.                 var ty:Number = partA.y + Math.sin(angle) * hitDistance;
  183.                 var ax:Number = (tx - partB.x) * 0.5;
  184.                 var ay:Number = (ty - partB.y) * 0.5;
  185.                 partA.vx -= ax;
  186.                 partA.vy -= ay;
  187.                 partB.vx += ax;
  188.                 partB.vy += ay;
  189.             }
  190.         }
  191.         */
  192.        
  193.         private function stayOnCanvas(partA:Bubble):void {
  194.             var minx:Number = 0;
  195.             var miny:Number = 0;
  196.             var maxx:Number = stage.stageWidth;
  197.             var maxy:Number = stage.stageHeight;
  198.            
  199.             if(partA.x + partA.mySize > maxx) {
  200.                 partA.x = maxx - partA.mySize;
  201.                 partA.vx *= -wallBounce;
  202.             }
  203.             else if(partA.x - partA.mySize < minx) {
  204.                 partA.x = minx + partA.mySize;
  205.                 partA.vx *= -wallBounce;
  206.             }
  207.            
  208.             if(partA.y + partA.mySize > maxy) {
  209.                 partA.y = maxy - partA.mySize;
  210.                 partA.vy *= -wallBounce;
  211.             }
  212.             else if(partA.y - partA.mySize < miny) {
  213.                 partA.y = miny + partA.mySize;
  214.                 partA.vy *= -wallBounce;
  215.             }
  216.            
  217.             // boundary gravity
  218.             var mincomfortablex:Number = partA.mySize + boundaryWidth;
  219.             var maxcomfortablex:Number = stage.stageWidth - (partA.mySize + boundaryWidth);
  220.             var mincomfortabley:Number = partA.mySize + boundaryHeight;
  221.             var maxcomfortabley:Number = stage.stageHeight - (partA.mySize + boundaryHeight);
  222.            
  223.             if(partA.x > maxcomfortablex) {
  224.                 partA.vx -= boundaryGravity;
  225.             }
  226.             else if(partA.x < mincomfortablex) {
  227.                 partA.vx += boundaryGravity;
  228.             }
  229.            
  230.             if(partA.y > maxcomfortabley) {
  231.                 partA.vy -= boundaryGravity;
  232.             }
  233.             else if(partA.y < mincomfortabley) {
  234.                 partA.vy += boundaryGravity;
  235.             }
  236.         }
  237.     }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement