In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

ツインふりこ

Get Adobe Flash player
by neguse 23 Aug 2009
    Embed
package {

	import flash.display.*;
	import flash.geom.*;
	import flash.events.*;
	import flash.text.*;
	import net.hires.debug.Stats;

	[SWF(width = "465", height = "465", frameRate = "60", backgroundColor = "#808080")]

	public class Test extends Sprite {

		private const VMEM_W:int = 465, VMEM_H:int = 465;
		private const BOX_SIZE:int = 10;
		private const LINE_DIV:uint = 100;
		private const BAR_LEN:Number = 100;
		private const GRAVITY:Number = -0.1;
		private const ITER:Number = 10;
		private const PENALTY:Number = 0.98;

		private var bx:Number, by:Number;
		private var px:Number, py:Number;
		private var vx:Number, vy:Number;
		private var pvx:Number, pvy:Number;
		private var vmem:BitmapData = new BitmapData(VMEM_W, VMEM_H, false, 0x808080);

		private function toRect(x:Number, y:Number, size:Number) : Rectangle {
			var vw:Number = VMEM_W / 2;
			var vh:Number = VMEM_H / 2;
			var s:Number = size / 2;
			return new Rectangle(
					-s + x + vw, - s + vh - y,
					size, size);
		}
		private function drawLine(x1:Number, y1:Number, x2:Number, y2:Number, color:Number) : void {
			var dx:Number = x2 - x1;
			var dy:Number = y2 - y1;
			for (var i:uint = 0; i <= LINE_DIV; i++) {
				drawRect(x1 + dx * i / LINE_DIV, y1 + dy * i / LINE_DIV, 4, color);
			}
		}
		private function drawRect(x:Number, y:Number, size:Number, color:Number) : void {
			var r:Rectangle = toRect(x, y, size);
			vmem.fillRect(r, color);
		}

		public function update(event:Event) : void {
			var dt:Number = 1.0 / Number(ITER);
			for (var i:uint = 0; i < ITER; i++) {
				// gravity
				vy += 0.5 * GRAVITY * dt;
                                pvy += GRAVITY * dt;

				// constraint of center to joint
				var d:Number = Math.sqrt(px * px + py * py);
				var a:Number = Math.atan2(py, px);
				var k:Number = (d - BAR_LEN) * PENALTY;
				pvx -= k * Math.cos(a) * dt;
				pvy -= k * Math.sin(a) * dt;

				// constraint of joint to box
				var dx:Number = bx - px;
				var dy:Number = by - py;
				d = Math.sqrt(dx * dx + dy * dy);
				a = Math.atan2(dy, dx);
				k = (d - BAR_LEN) * PENALTY;
				vx -= k * Math.cos(a) * dt * 0.5;
				vy -= k * Math.sin(a) * dt * 0.5;
				pvx += k * Math.cos(a) * dt * 0.5;
				pvy += k * Math.sin(a) * dt * 0.5;

				// apply velocity
				bx += vx * dt;
				by += vy * dt;
				px += pvx * dt;
				py += pvy * dt;
			}

			vmem.lock();
			vmem.fillRect(vmem.rect, 0x808080);
			drawLine(px, py, 0, 0, 0xffffffff);
			drawLine(px, py, bx, by, 0xffffffff);
			drawRect(px, py, BOX_SIZE, 0);
			drawRect(bx, by, BOX_SIZE, 0);
			vmem.unlock();
		}

		public function Test() {
			bx = 200, by = 0;
			px = 100, py = 0;
			vx = 0, vy = 0;
			pvx = 0, pvy = 0;
			var text:TextField = new TextField();
			text.text = "test1 moving box";
			addChild(text);
			addChild(new Bitmap(vmem));
			addEventListener(Event.ENTER_FRAME, update);

			// addChild(new Stats());
		}
	}
}