Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package {
- import flash.display.StageAlign;
- import flash.display.StageScaleMode;
- import flash.display.MovieClip;
- import flash.events.Event;
- import flash.geom.Point;
- import DriverBubble;
- import TweetBubble;
- public class DocumentMain extends MovieClip {
- public var collisionParticles:Array; // the array of main objects in frame
- public var wallBounce:Number = 0.7; // how much velocity is retained when bouncing off walls
- public var friction:Number = 0.97; // how quickly objects slow down
- public var boundaryWidth:Number = 50; // how far from stage sides the objects should stay
- public var boundaryHeight:Number = 30; // how far from the top and bottom objects should stay
- public var boundaryGravity:Number = 0.8; // how fast objects repel from the boundary
- public function DocumentMain() {
- init();
- }
- private function init() {
- stage.align = StageAlign.TOP_LEFT;
- stage.scaleMode = StageScaleMode.NO_SCALE;
- collisionParticles = new Array();
- // create the driver particles
- for(var i = 0; i < 2; i++) {
- var randX:Number = Math.floor(Math.random() * (stage.stageWidth - 210)) + 105;
- var randY:Number = Math.floor(Math.random() * (stage.stageHeight - 210)) + 105;
- var randSize:Number = Math.random() * 40 + 60;
- var myBubble:DriverBubble = new DriverBubble();
- myBubble.setSize(randSize);
- myBubble.x = randX;
- myBubble.y = randY;
- addChild(myBubble);
- collisionParticles.push(myBubble);
- var numTweets:Number = 20; //Math.floor(Math.random() * 20) +
- for(var j = 0; j < numTweets; j++) {
- var tweetX:Number = Math.floor(Math.random() * (stage.stageWidth - 210)) + 105;
- var tweetY:Number = Math.floor(Math.random() * (stage.stageHeight - 210)) + 105;
- var tBubble:TweetBubble = new TweetBubble();
- tBubble.setSize(4);
- tBubble.x = tweetX;
- tBubble.y = tweetY;
- addChild(tBubble);
- collisionParticles.push(tBubble);
- myBubble.tweets.push(tBubble);
- }
- }
- addEventListener(Event.ENTER_FRAME, onEnterFrame);
- }
- private function onEnterFrame(event:Event) {
- // loop for movement
- for( var i:uint = 0; i < collisionParticles.length; i++ ) {
- var particle = collisionParticles[i];
- stayOnCanvas(particle);
- //friction
- particle.vx *= friction;
- particle.vy *= friction;
- //move
- particle.x += (particle.vx - particle.x) * .2;
- particle.y += (particle.vy - particle.y) * .2;
- if(particle.hasOwnProperty('tweets')) {
- if(particle.tweets.length) {
- // loop for gravity in groups
- for( var k:uint = 0; k < particle.tweets.length; k++) {
- gravitate(particle.tweets[k], particle);
- }
- }
- }
- }
- // loop for collision
- for( i=0; i < collisionParticles.length - 1; i++ ) {
- var partA:Bubble = collisionParticles[i];
- for(var j:uint = i+1; j < collisionParticles.length; j++) {
- var partB:Bubble = collisionParticles[j];
- checkCollision(partA, partB);
- //gravitate(partA, partB);
- }
- }
- }
- private function gravitate(partA:Bubble, partB:Bubble):void {
- var dx:Number = partB.x - partA.x;
- var dy:Number = partB.y - partA.y;
- var distSQ:Number = dx * dx + dy * dy;
- var dist:Number = Math.sqrt(distSQ);
- var force:Number = partA.mass * partB.mass / distSQ;
- var ax:Number = force * dx / dist;
- var ay:Number = force * dy / dist;
- partA.vx += ax / partA.mass;
- partA.vy += ay / partA.mass;
- partB.vx -= ax / partB.mass;
- partB.vy -= ay / partB.mass;
- }
- private function checkCollision(ball0:Bubble, ball1:Bubble):void {
- var dx:Number = ball1.x - ball0.x;
- var dy:Number = ball1.y - ball0.y;
- var dist:Number = Math.sqrt(dx*dx + dy*dy);
- if(dist <= ball0.mySize + ball1.mySize) {
- //calculate angle, sine, and cosine
- var angle:Number = Math.atan2(dy, dx);
- var sin:Number = Math.sin(angle);
- var cos:Number = Math.cos(angle);
- //rotate ball0 position
- var pos0:Point = new Point(0,0);
- //rotate ball1 position
- var pos1:Point = rotate(dx, dy, sin, cos, true);
- //rotate ball0 velocity
- var vel0:Point = rotate(ball0.vx, ball0.vy, sin, cos, true);
- //rotate ball1 velocity
- var vel1:Point = rotate(ball1.vx, ball1.vy, sin, cos, true);
- //collision reaction
- var vxTotal:Number = vel0.x - vel1.x;
- vel0.x = ((ball0.mass - ball1.mass) * vel0.x +
- 2 * ball1.mass * vel1.x) /
- (ball0.mass + ball1.mass);
- vel1.x = vxTotal + vel0.x;
- //update position
- var absV:Number = Math.abs(vel0.x) + Math.abs(vel1.x);
- var overlap:Number = (ball0.mySize + ball1.mySize)
- - Math.abs(pos0.x - pos1.x);
- pos0.x += vel0.x / absV * overlap;
- pos1.x += vel1.x / absV * overlap;
- //rotate positions back
- var pos0F:Object = rotate(pos0.x, pos0.y, sin, cos, false);
- var pos1F:Object = rotate(pos1.x, pos1.y, sin, cos, false);
- //adjust positions to actual screen positions
- ball1.x = ball0.x + pos1F.x;
- ball1.y = ball0.y + pos1F.y;
- ball0.x = ball0.x + pos0F.x;
- ball0.y = ball0.y + pos0F.y;
- //rotate velocities back
- var vel0F:Object = rotate(vel0.x, vel0.y, sin, cos, false);
- var vel1F:Object = rotate(vel1.x, vel1.y, sin, cos, false);
- ball0.vx = vel0F.x;
- ball0.vy = vel0F.y;
- ball1.vx = vel1F.x;
- ball1.vy = vel1F.y;
- }
- }
- private function rotate(x:Number, y:Number, sin:Number, cos:Number, reverse:Boolean):Point {
- var result:Point = new Point();
- if(reverse) {
- result.x = x * cos + y * sin;
- result.y = y * cos - x * sin;
- }
- else {
- result.x = x * cos - y * sin;
- result.y = y * cos + x * sin;
- }
- return result;
- }
- /*
- private function checkCollision(partA:Bubble, partB:Bubble):void {
- var dx:Number = partB.x - partA.x;
- var dy:Number = partB.y - partA.y;
- var dist:Number = Math.sqrt(dx * dx + dy * dy);
- var hitDistance:Number = partA.mySize + partB.mySize;
- if(dist <= hitDistance) {
- var angle:Number = Math.atan2(dy, dx);
- var tx:Number = partA.x + Math.cos(angle) * hitDistance;
- var ty:Number = partA.y + Math.sin(angle) * hitDistance;
- var ax:Number = (tx - partB.x) * 0.5;
- var ay:Number = (ty - partB.y) * 0.5;
- partA.vx -= ax;
- partA.vy -= ay;
- partB.vx += ax;
- partB.vy += ay;
- }
- }
- */
- private function stayOnCanvas(partA:Bubble):void {
- var minx:Number = 0;
- var miny:Number = 0;
- var maxx:Number = stage.stageWidth;
- var maxy:Number = stage.stageHeight;
- if(partA.x + partA.mySize > maxx) {
- partA.x = maxx - partA.mySize;
- partA.vx *= -wallBounce;
- }
- else if(partA.x - partA.mySize < minx) {
- partA.x = minx + partA.mySize;
- partA.vx *= -wallBounce;
- }
- if(partA.y + partA.mySize > maxy) {
- partA.y = maxy - partA.mySize;
- partA.vy *= -wallBounce;
- }
- else if(partA.y - partA.mySize < miny) {
- partA.y = miny + partA.mySize;
- partA.vy *= -wallBounce;
- }
- // boundary gravity
- var mincomfortablex:Number = partA.mySize + boundaryWidth;
- var maxcomfortablex:Number = stage.stageWidth - (partA.mySize + boundaryWidth);
- var mincomfortabley:Number = partA.mySize + boundaryHeight;
- var maxcomfortabley:Number = stage.stageHeight - (partA.mySize + boundaryHeight);
- if(partA.x > maxcomfortablex) {
- partA.vx -= boundaryGravity;
- }
- else if(partA.x < mincomfortablex) {
- partA.vx += boundaryGravity;
- }
- if(partA.y > maxcomfortabley) {
- partA.vy -= boundaryGravity;
- }
- else if(partA.y < mincomfortabley) {
- partA.vy += boundaryGravity;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement