/**
* Copyright J.J ( http://wonderfl.net/user/J.J )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/rgVn
*/
package {
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Sprite;
public class Fluid extends Sprite{
public static var w:Number;
public static var h:Number;
public static var r:int;
public static var c:int;
public var cs:Vector.<Vector.<Cel>>;
private var ps : Vector.<Prt>;
public var m:Vct;
public var m1:Vct;
public var isDown:Boolean = false;
public var a:Number = 3;
public var b : Number = 10;
public function Fluid() {
var n:int = 900;
cs = new Vector.<Vector.<Cel>>();
ps = new Vector.<Prt>();
w = this.stage.stageWidth;
h = this.stage.stageHeight;
r = 50;
c = 50;
b = 25;
a=2;
m = new Vct();
m1 = new Vct();
this.graphics.beginFill(0);
this.graphics.drawRect(0, 0, w, h);
var mc:Sprite = new Sprite();
this.addChild(mc);
Disp.setCanvas(mc.graphics);
for (var i:int;i<n;i++) {
var p:Prt = new Prt(w * Math.random(), h * Math.random());
p.o = new Vct(p.x.x, p.x.y);
ps.push(p);
}
var stw:Number = w / r;
var sth:Number = h / c;
var hi:Number = 0;
var wi:Number = 0;
for (i=0;i<r;i++) {
cs[i] = new Vector.<Cel>();
wi=0;
for (var j:int=0;j<c;j++) {
var ce:Cel = new Cel();
ce.x.x = wi + (stw / 2);
ce.x.y = hi + (sth / 2);
cs[i][j] = ce;
wi += stw;
}
hi += sth;
}//end for
stage.addEventListener(MouseEvent.MOUSE_DOWN, ondown);
stage.addEventListener(MouseEvent.MOUSE_UP, onup);
this.addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(event : Event) : void {
m.x = stage.mouseX;
m.y = stage.mouseY;
var V:Vct = new Vct(m.x-m1.x,m.y-m1.y);
Disp.clear();
for (var i:int=0;i<r;i++) {
for (var j:int=0;j<c;j++) {
var ce:Cel = cs[i][j];
if (isDown) {
var M:Vct = new Vct(stage.mouseX, stage.mouseY);
var Q:Vct = new Vct(M.x - ce.x.x, M.y - ce.x.y);
var l:Number = Math.sqrt(Q.x * Q.x + Q.y * Q.y);
var qdv:Number = (Q.x * V.x + Q.y * V.y)/(b * l * l);
var al:Number = a / l;
ce.v.x += V.x * al + Q.x *qdv ;
ce.v.y += V.y* al + Q.y * qdv;
}
// var targ:Vct = new Vct(ce.x.x + ce.v.x, ce.x.y + ce.v.y);
//Disp.drawDot(targ);
//Disp.drawLine(ce.x, targ);
ce.v.x *= .9;
ce.v.y *= .9;
}
}
for each(var p:Prt in ps) {
i = int(p.x.x / (h / r));
j= int(p.x.y / (w / c));
if (i < r && i >= 0 && j < c && j >= 0) {
ce= cs[j][i];
p.v.x += ce.v.x;
p.v.y += ce.v.y;
p.x.x += p.v.x;
p.x.y += p.v.y;
p.v.x *= .7;
p.v.y *= .7;
}
Disp.drawLine(p.x, new Vct(p.x.x + p.v.x+Math.random(), p.x.y + p.v.y+Math.random()),p.color);
p.update();
}
m1.x = m.x;
m1.y = m.y;
}
private function onup(event : MouseEvent) : void {
isDown=false;
}
private function ondown(event : MouseEvent) : void {
isDown=true;
}
}
}
import flash.display.Graphics;
class Vct {
public var x:Number;
public var y:Number;
public function Vct(x:Number=0,y:Number=0)
{
this.x = x;
this.y = y;
}
}
class Cel {
public var v:Vct;
public var x:Vct;
public function Cel()
{
this.x = new Vct();
this.v = new Vct();
}
}
class Prt {
public var a:Vct;
public var v:Vct;
public var x:Vct;
public var o:Vct;
public var color:uint=0xffffff;
public function Prt(x:Number,y:Number)
{
this.x = new Vct(x,y);
this.v = new Vct();
this.a = new Vct();
}
public function update():void {
if(this.x.x<0) {
this.x.y = Fluid.h- this.x.y;
this.x.x = Fluid.w-10;
}
if(this.x.x>Fluid.h) {
this.x.y = 475 - x.y;
this.x.x = 5;
}
if(this.x.y<0) {
this.x.y = Fluid.h-20;
}
if(this.x.y>Fluid.h) {
this.x.y = 10;
}
}
}
class Disp {
private static var c:Graphics;
public static function setCanvas(g:Graphics):void { c = g; }
public static function drawDot(v:Vct, r:Number = 1):void {
c.beginFill(0);
c.drawCircle(v.x, v.y, r);
c.endFill();
}
public static function clear():void {
c.clear();
}
public static function drawLine(a:Vct, b:Vct, col:int = 0):void {
c.lineStyle(1, col);
c.moveTo(a.x, a.y);
c.lineTo(b.x, b.y);
c.endFill();
}
}