Gravity
Mouseclick adds ball (same momentum as cursor)
The gravity well in the middle is (now) actually two separate wells. You can move both of them with your mouse.
Balls passing close to the gravity wells caused several bugs, such balls are now removed.
In the beginning 100 balls will spawn, most will be destroyed quickly.
Fading trails added.
[Space] to clear all balls.
/**
* Copyright antalg ( http://wonderfl.net/user/antalg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/5sKn
*/
package {
import flash.events.KeyboardEvent;
import flash.filters.ConvolutionFilter;
import flash.filters.ColorMatrixFilter;
import flash.filters.BlurFilter;
import flash.geom.Matrix;
import flash.ui.Mouse;
import flash.geom.Point;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.AVM1Movie;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
addChild(new Bitmap(_bmd));
for each (var p:Point in start) {
placeWell(p);
}
addChild(_lineSpr);
spawnRandomBalls(100);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseD);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseU);
stage.addEventListener(Event.ENTER_FRAME, onEF);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}
private function placeWell(p:Point) :void {
var spr:Sprite = new Sprite();
spr.graphics.beginFill(0xFFFFFF);
spr.graphics.drawCircle(0, 0, SPRWELLSIZE-RAD);
spr.graphics.endFill();
addChild(spr);
spr.x = p.x;
spr.y = p.y;
wells.push(spr);
spr.addEventListener(MouseEvent.MOUSE_DOWN, dragWell);
spr.addEventListener(MouseEvent.MOUSE_UP, dropWell);
}
private function spawnRandomBalls(count:int) :void{
var i:int;
var v:Number = 10;
for(i=0; i<count; i++) {
var x:Number = RAD+Math.random()*(stage.stageWidth-2*RAD);
var y:Number = RAD+Math.random()*(stage.stageHeight-2*RAD);
var angle:Number = 2*Math.PI*Math.random();
spawnBall(x, y, v*Math.cos(angle), v*Math.sin(angle));
}
}
private function spawnBall(X:Number, Y:Number, VX:Number, VY:Number):void {
var myBall:Ball = new Ball(X, Y, VX, VY);
addChild(myBall);
_balls.push(myBall);
}
private var mouseV:Point = new Point();
private var lastMousePos:Point = new Point();
private var _center:Point = new Point(stage.stageWidth/2, stage.stageHeight/2);
private var _lineSpr:Sprite = new Sprite();
private const RAD:Number = 10;
private const SPRWELLSIZE:Number = 30;
private const WELLSIZE:Number = SPRWELLSIZE*SPRWELLSIZE;
private var start:Array = [_center, _center]; // startpoints
private var wells:Array = new Array(); // sprites
private var _bmd:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x000000);
private var _balls:Array = new Array();
private var mD:Boolean = true;
private function dragWell(e:MouseEvent):void {
(e.target as Sprite).startDrag();
}
private function dropWell(e:MouseEvent):void {
(e.target as Sprite).stopDrag();
}
private function onKeyDown(e:KeyboardEvent):void {
if(e.keyCode == 32) {
clearBalls();
}
if(e.keyCode == 65) {
spawnBall(stage.mouseX, stage.mouseY, mouseV.x, mouseV.y);
}
}
private function mouseD(e:MouseEvent):void {
spawnBall(stage.mouseX, stage.mouseY, mouseV.x, mouseV.y);
//mD = !mD;//true;
}
private function mouseU(e:MouseEvent):void {
//mD = false;
}
private function gravity():void {
for each (var p:Sprite in wells) {
var i:int;
for(i=0; i<_balls.length; i++) {
var dx:Number = p.x-_balls[i].x;
var dy:Number = p.y-_balls[i].y;
var rSq:Number = dx*dx+dy*dy;
var angle:Number = Math.atan2(dy,dx);
var f:Number = 5000*_balls[i].mass/rSq;
_balls[i].vx += Math.cos(angle)*f/_balls[i].mass; // fx = Math.cos(angle)*f
_balls[i].vy += Math.sin(angle)*f/_balls[i].mass; // fy = Math.sin(angle)*f
}
}
}
private function onEF(e:Event):void {
mouseSpeed();
move();
//bounds();
gravity();
bmdFilters();
}
private function mouseSpeed():void {
mouseV.x = (mouseV.x + stage.mouseX-lastMousePos.x)/2;
mouseV.y = (mouseV.y + stage.mouseY-lastMousePos.y)/2;
lastMousePos.x = stage.mouseX;
lastMousePos.y = stage.mouseY;
Mouse.hide();
_lineSpr.x = stage.mouseX;
_lineSpr.y = stage.mouseY;
_lineSpr.graphics.clear();
_lineSpr.graphics.lineStyle(1,0xFFFFFF);
_lineSpr.graphics.moveTo(0,0);
_lineSpr.graphics.lineTo(-mouseV.x<<1, -mouseV.y<<1);
}
private function move():void {
var i:int;
for(i=0; i<_balls.length; i++) {
for each (var p:Sprite in wells){
if(distSq(_balls[i], p)<WELLSIZE) {
removeChild(_balls[i]);
_balls.splice(i,1);
return;
}
}
_balls[i].x += _balls[i].vx;
_balls[i].y += _balls[i].vy;
//_bmd.draw(_balls[i], new Matrix(1,0,
// 0,1,
// _balls[i].x,_balls[i].y));
_bmd.setPixel(_balls[i].x, _balls[i].y, 0xFFFFFF);
}
}
private var bf:BlurFilter = new BlurFilter(4,4,2);
private var cmf:ColorMatrixFilter = new ColorMatrixFilter( [ 1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 0.97, 0]);
private function bmdFilters():void {
//_bmd.applyFilter(_bmd, _bmd.rect, new Point(0,0), bf);
_bmd.applyFilter(_bmd, _bmd.rect, new Point(0,0), cmf);
//_bmd.scroll(0,2);
}
private function bounds():void {
var i:int;
for(i=0; i<_balls.length; i++) {
if(_balls[i].x<RAD || _balls[i].x>stage.stageWidth-RAD) {
_balls[i].vx *= -1;
_balls[i].x += _balls[i].vx;
}
if(_balls[i].y<RAD || _balls[i].y>stage.stageHeight-RAD) {
_balls[i].vy *= -1;
_balls[i].y += _balls[i].vy;
}
}
}
private function distSq(ball:Ball, well:Sprite):Number {
var dx:Number = well.x-(ball.x+ball.vx);
var dy:Number = well.y-(ball.y+ball.vy);
return dx*dx+dy*dy;
}
private function clearBalls():void {
for(var i:int = 0; i<_balls.length; i++) {
removeChild(_balls[i]);
}
_balls = new Array();
}
}
}
import flash.geom.Point;
import flash.display.Sprite;
class Ball extends Sprite{
public function Ball(X:Number, Y:Number, VX:Number, VY:Number) {
createGraphics();
x = X;
y = Y;
vx = VX;
vy = VY;
mass = 100;
}
public var mass:Number;
public var vx:Number;
public var vy:Number;
private const RADIUS:Number = 5;
private const COLOR:uint = 0xAA3300;
private function createGraphics():void {
var r1:int = Math.random()*255;
var r2:int = (255-r1) - Math.random()*(255-r1);
var r3:int = 255-r1-r2;
this.graphics.beginFill((r1<<16)|(r2<<8)|r3);
this.graphics.drawCircle(0,0,RADIUS);
this.graphics.endFill();
}
}