flash on 2009-2-24
package {
import flash.display.Sprite;
import flash.geom.Point;
import flash.events.Event;
public class FlashTest extends Sprite {
private static const POINTS:uint = 500;
private static const LENGTH:Number = 1;
private static const GRAVITY:Number = 1;
private var previous:Vector.<Point>;
private var points:Vector.<Point>;
public function FlashTest() {
points = new Vector.<Point>();
previous = new Vector.<Point>();
points.push( new Point( stage.stageWidth / 2, 0 ) );
while(points.length < POINTS )
points.push( new Point(
stage.stageWidth / 2 - points.length,
points.length ) );
snap();
while( previous.length < points.length )
previous.push( points[previous.length].clone() );
addEventListener( Event.ENTER_FRAME, drawPoints );
}
private function snap():void
{
var p:Point = points[0];
// -- Zero elasticity
for( var i:uint = 1; i < points.length; i++ )
{
var t:Point = points[i];
var dx:Number = t.x - p.x;
var dy:Number = t.y - p.y;
var scale:Number = Math.sqrt( dx*dx + dy*dy );
var tx:Number = (t.x - p.x) * LENGTH / scale + p.x;
var ty:Number = (t.y - p.y) * LENGTH / scale + p.y;
t.x = tx;//((t.x - p.x) + (tx - p.x)) / 2 + p.x;
t.y = ty;((t.y - p.y) + (ty - p.y)) / 2 + p.y;
p = t;
}
}
private function drawPoints(e:Event):void
{
graphics.clear();
graphics.lineStyle( 1, 0, 100 );
graphics.moveTo( points[0].x, points[0].y );
for( var i:uint = 1; i < points.length; i++ )
{
var t:Point = points[i];
var v:Point = previous[i];
// -- Verlet physics (with dampening)
var x:Number = t.x;
var y:Number = t.y;
t.x += (t.x - v.x);
t.y += (t.y - v.y) + GRAVITY;
v.x = x;
v.y = y;
graphics.lineTo( t.x, t.y );
}
snap();
}
}
}