flash on 2013-6-6
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/sNgJ
*/
package {
import flash.events.MouseEvent;
import flash.display.Graphics;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public var cw:Number = 8;
public var ch:Number = 8;
public function FlashTest() {
map = new BitmapData(128,128, false, 0);
pic = new Bitmap(map);
pic.scaleX = cw;
pic.scaleY = ch;
addChild(pic);
var i:int;
var a:Rectangle;
a = new Rectangle();
map.lock();
for (i = 0; i < 256; i++)
{
a.x = Math.random()*128;
a.y = Math.random()*128;
if (Math.random() < 0.5)
{
a.width = 16 * Math.random();
a.height = 1;
}
else
{
a.height = 8 * Math.random();
a.width = 1;
}
map.fillRect(a, 0xFFFFFF);
}//nexti
//make sure we dont start in a wall
a.x = cx/cw -4;
a.y = cy/ch - 4;
a.width =8;
a.height = 8;
map.fillRect(a, 0);
map.unlock();
/*
ps = new Sprite();
ps.graphics.clear();
ps.graphics.lineStyle(2, 0xFF0000);
ps.graphics.drawCircle(0,0, 8);
addChild(ps);
*/
deb = new Sprite();
addChild(deb);
vecNode = new Vector.<xNode>(0, false);
for (i = 0; i < 10; i++)
{
vecNode.push(new xNode());
vecNode[i].cx = cx;
vecNode[i].cy = cy;
}//nexti
vecNode[0].mass = 64;
vecNode[vecNode.length-1].mass = 16;
stage.addEventListener(MouseEvent.MOUSE_DOWN, mdown);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public function mdown(e:MouseEvent):void
{
var a:xNode;
a = vecNode[0];
a.mode = 1;
a.cx = cx;
a.cy = cy;
a.vx = mouseX - cx;
a.vy = mouseY - cy;
a.vx *= 0.4;
a.vy *= 0.4;
// a.mass = 16;
var i:int;
var num:int;
num = vecNode.length;
for (i = 1; i < num; i++)
{
a = vecNode[i];
a.cx = cx;
a.cy = cy;
a.vx = 0;
a.vy = 0;
}//nexti
}//mdown
public var vecNode:Vector.<xNode>;
public var map:BitmapData;
public var pic:Bitmap;
//public var ps:Sprite;
public var deb:Sprite;
public var cx:Number = 100;
public var cy:Number = 100;
public var vx:Number = 0;
public var vy:Number = 0;
public var gx:Number = 100;
public var gy:Number = 100;
public function onEnter(e:Event):void
{
var g:Graphics;
g = deb.graphics;
g.clear();
g.lineStyle(2, 0xFF0000);
var mx:Number;
var my:Number;
mx = mouseX;
my = mouseY;
/*
vy += 0.15;
vx *= 0.99;
vy *= 0.99;
if (vx > 0 && isWall(cx+8,cy)) { vx = 0; }
else if (vx < 0 && isWall(cx-8, cy)) { vx = 0; }
if (vy > 0 && isWall(cx, cy+8)) { vy = 0; }
else if (vy < 0 && isWall(cx, cy-8)) { vy = 0; }
cx += vx;
cy += vy;
*/
var ax:Number;
var ay:Number;
var mag:Number;
var dx:Number;
var dy:Number;
var nx:Number;
var ny:Number;
var dist:Number;
var reld:Number;
var relvel:Number;
var d:Number;
var imp:Number;
var rem:Number;
var i:int;
var num:int;
var a:xNode;
var b:xNode;
num = vecNode.length;
a = vecNode[0];
if (a.mode == 0)
{
a.cx = gx;
a.cy = gy;
a.vx = 0;
a.vy = 0;
a.mass = 2;
}//endif
var k:int;
for (k = 0; k < 2; k++)
{
for (i = 0; i < num-1; i++)
{
a = vecNode[i];
b = vecNode[i+1];
dx = a.cx - b.cx;
dy = a.cy - b.cy;
mag = Math.sqrt(dx*dx + dy*dy);
//if (mag < 2) { continue; }
if (mag <= 0) { continue;}
nx = dx / mag;
ny = dy / mag;
ax = a.vx - b.vx;
ay = a.vy - b.vy;
relvel = (ax*nx) +(ay*ny);
reld = mag - 0.2; //1; //3;
rem = relvel + (reld / d);
rem = relvel + reld;
imp = rem / (1/a.mass + 1/b.mass);
imp *= 0.5;
if (a.mode == 0)
{
a.vx += (imp *(nx*-0.5 )) / a.mass;
a.vy += (imp *(ny*-0.5 )) / a.mass;
}
b.vx += (imp*(nx*0.5 )) / b.mass;
b.vy += (imp*(ny*0.5 )) / b.mass;
}//nexti
}//nextk
var vx2:Number;
var vy2:Number;
for (i = 0; i < num; i++)
{
a = vecNode[i];
if (a.mode == 0)
{
a.vy += 0.1;
a.vx *= 0.97;
a.vy *= 0.97;
if (a.vx > 0 && isWall(a.cx+4,a.cy)) { a.vx *= -0.5; }
else if (a.vx < 0 && isWall(a.cx-4, a.cy)) { a.vx *= -0.5; }
if (a.vy > 0 && isWall(a.cx, a.cy+4)) { a.vy *= -0.5; }
else if (a.vy < 0 && isWall(a.cx, a.cy-4)) { a.vy *= -0.5; }
}
else
{
if (isWall(a.cx, a.cy))
{
gx = a.cx;
gy = a.cy;
a.mode = 0;
// a.mass = 2;
}
}//endif
vx2 = a.vx;
vy2 = a.vy;
mag = Math.sqrt(vx2*vx2+vy2*vy2);
if (mag > 8)
{
vx2 /= mag;
vy2 /= mag;
vx2 *= 8;
vy2 *= 8;
}
/*
if (vx2 > 8) { vx2 = 8;}
else if (vx2 < -8) {vx2 = -8;}
if (vy2 > 8) { vy2 = 8;}
else if (vy2 < -8) { vy2 = -8;}
*/
a.cx += vx2;
a.cy += vy2;
g.beginFill(0xFF0000,0.5);
g.drawCircle(a.cx,a.cy,4);
g.endFill();
}//nexti
a = vecNode[ vecNode.length - 1];
a.vy += 0.1;
ax = (a.cx - cx) * 0.5;
ay = (a.cy - cy) * 0.5;
vx *= 0.99;
vy *= 0.99;
if (isWall(cx+ax, cy+ay) == false)
{
cx += ax; cy += ay;
}//endif
g.drawCircle(cx, cy, 8);
g.moveTo(mx, my-8);
g.lineTo(mx,my+8);
g.moveTo(mx-8,my);
g.lineTo(mx+8,my);
}//onenter
public function isWall(wx:Number, wy:Number):Boolean
{
var tx:int;
var ty:int;
tx = Math.floor(wx / cw);
ty = Math.floor(wy / ch);
if (tx < 0) { return true; }
if (ty < 0) { return true; }
if (tx >= map.width) { return true; }
if (ty >= map.height) { return true; }
return (map.getPixel(tx, ty) > 0);
}//iswall
}//classend
}
class xNode
{
public var cx:Number = 0;
public var cy:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
public var mass:Number = 2;
public var mode:int = 0;
}//xnode