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

Tensegrity

/**
 * Copyright onedayitwillmake ( http://wonderfl.net/user/onedayitwillmake )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/jNca
 */

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix3D;
	import flash.geom.Vector3D;

	[SWF(width = "400", height = "400", backgroundColor = "#ffffff", frameRate = "60")]

	public class Test3D extends Sprite {
		private var container:Sprite;
		private var tensegrity:Tensegrity;
		private var size:int = Tensegrity.SIZE/2;
		private var nX:Number;
		private var nY:Number;

		public function Test3D():void {
			container = new Sprite();
			tensegrity = new Tensegrity();
			nX = container.x = (this.stage.stageWidth/2) - size;
			nY = container.y = (this.stage.stageHeight/2) - size;
			container.z = 0;
			stage.addChild(container);
			container.addChild(tensegrity);
			container.addEventListener(Event.ENTER_FRAME, rotate);
		}
		private function rotate(e:Event):void {
			var rotX:Number = (mouseY - size - nY)*0.02;
			var rotY:Number = (mouseX - size - nX)*0.02;
			var mat:Matrix3D = container.transform.matrix3D;
			mat.appendTranslation(-nX - size, -nY - size, 0);
			mat.appendRotation(rotX, Vector3D.X_AXIS);
			mat.appendRotation(-rotY, Vector3D.Y_AXIS);
			mat.appendTranslation(nX + size, nY + size, 0);
			container.transform.matrix3D = mat;
		}
	}
}

import flash.display.Shape;
import flash.display.Sprite;
import flash.geom.Matrix3D;   
import flash.geom.Vector3D;

class Tensegrity extends Sprite {
	public static const SIZE:int = 240; 
	private var compression:Vector.<Sprite>;
	private var tension:Vector.<Shape>;
	private var commands:Vector.<int>;
	private var data:Vector.<Number>;

	public function Tensegrity():void {
		compression = new Vector.<Sprite>(3, true);
		tension = new Vector.<Shape>(12, true);
		commands = Vector.<int>([1, 2, 1, 2]); // moveTo -> lineTo -> moveTo -> lineTo
		setCompression();
		setTension();
	}
	private function setCompression():void {
		var color:Vector.<uint> = Vector.<uint>([0xff7fbf, 0x7fffbf, 0x7fbfff]);
		data = Vector.<Number>([SIZE/3, 0, SIZE/3, SIZE, SIZE/3*2, 0, SIZE/3*2, SIZE]);

		for(var i:int=0; i<3; i++) {
			compression[i] = new Sprite();
			compression[i].z = 0;
			with(compression[i].graphics) {
				lineStyle(5, color[i]);
				drawPath(commands, data);
				lineStyle(5, color[i]);
				drawCircle(SIZE/3, 0, 3);
				drawCircle(SIZE/3*2, 0, 3);
				drawCircle(SIZE/3, SIZE, 3);
				drawCircle(SIZE/3*2, SIZE, 3);
			}
			addChild(compression[i]);
		}
		compression[1].transform.matrix3D.appendRotation(90, Vector3D.X_AXIS);
		compression[1].transform.matrix3D.appendRotation(90, Vector3D.Z_AXIS);
		compression[1].transform.matrix3D.appendTranslation(SIZE/2, 0, -SIZE/2);
		compression[2].transform.matrix3D.appendRotation(90, Vector3D.Y_AXIS);
		compression[2].transform.matrix3D.appendRotation(90, Vector3D.Z_AXIS);
		compression[2].transform.matrix3D.appendTranslation(SIZE, SIZE/2, SIZE/2);
	}
	private function setTension():void {
		var len:Number = Math.sqrt(5/18*SIZE*SIZE); // == SIZE/3*SIZE/3 + SIZE/6*SIZE/6
		var angle:Number = Math.atan(1/3)*180/Math.PI; // == (SIZE/6)/(SIZE/2)
		var k:int = 0;
                var i:int = 0;
		for(i = 0; i<12; i++) {
			tension[i] = new Shape();
			tension[i].z = 0;
			tension[i].graphics.lineStyle(0, 0xdddddd);
		}
		for(i = 0; i<3; i++) {
			k = i << 2;
			data = Vector.<Number>([SIZE/3, 0, 0, len, SIZE/3<<1, 0, SIZE, len]);
			tension[k].graphics.drawPath(commands, data);
			tension[k].transform.matrix3D.appendRotation(angle, Vector3D.X_AXIS);
			tension[k+1].graphics.drawPath(commands, data);
			tension[k+1].transform.matrix3D.appendRotation(-angle, Vector3D.X_AXIS);
			data = Vector.<Number>([SIZE/3, SIZE, 0, SIZE - len, SIZE/3<<1, SIZE, SIZE, SIZE - len]);
			tension[k+2].graphics.drawPath(commands, data);
			rotationAtOrigin(tension[k+2], SIZE, angle, Vector3D.X_AXIS);
			tension[k+3].graphics.drawPath(commands, data);
			rotationAtOrigin(tension[k+3], SIZE, -angle, Vector3D.X_AXIS);
			for(var j:int=k; j<=k+3; j++) compression[i].addChild(tension[j]);
		}
	}
	private function rotationAtOrigin(object:Shape, size:int, angle:Number, axis:Vector3D):void {
		var mat:Matrix3D = object.transform.matrix3D;
		mat.appendTranslation(-size, -size, 0);
		mat.appendRotation(angle, axis);
		mat.appendTranslation(size, size, 0);
		object.transform.matrix3D = mat;
	}
}