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

forked from: QuickBox2D でぽにょぽにょさせてみた

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

package {
    import flash.display.*;
    import com.actionsnippet.qbox.*;
    import flash.geom.*;
    import flash.events.*;
    import flash.net.*;
    import flash.system.*;

    public class FlashTest extends MovieClip {
        private const frequencyHz:Number = 3;
        private const distance:Number = 1;
        private const dotNum:int = 4;

        public static const STAGE_W:uint = 465;
        public static const STAGE_H:uint = 465;
        private var _rect:Rectangle = new Rectangle(0, 0, STAGE_W, STAGE_H);
        private var _point:Point = new Point();
        private var _displayLayer:Bitmap;
        private var _drawShape:Shape = new Shape();
        private var _clearBitmap:BitmapData = new BitmapData(STAGE_W, STAGE_H, true, 0xffffffff);
        private var _displayBitmap:BitmapData = new BitmapData(STAGE_W, STAGE_H);

        private var bmd:BitmapData;
        private var loader:Loader = new Loader();
        Security.loadPolicyFile("http://assets.wonderfl.net/crossdomain.xml");

        private var dots:Array = [];

        public function FlashTest() {
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadDone);
            loader.load(new URLRequest("http://assets.wonderfl.net/images/related_images/e/e1/e1b1/e1b1a2d1ea3db94a33f5eb77daaf9125081062b7"));
        }

        private function loadDone(e:Event):void {
            bmd = new BitmapData(loader.width,loader.height,true,0x000000);
            bmd.draw(loader);
            init();
        }

        private function init():void {
            var sim:QuickBox2D = new QuickBox2D(this, { debug:false, simpleRender:true });
            //_displayBitmap.fillRect(_rect, 0xff0000);
            
            var center:Point = new Point(distance, distance);
            //var dots:Array = [];
            var i:int;
            var j:int;
            var dot:QuickObject;
            for (j = 0; j < dotNum + 1; j++) {
                var line:Array = [];
                dots.push(line);
                for (i = 0; i < dotNum + 1; i++) {
                    dot = sim.addCircle({x:center.x + i*distance,
                                         y:center.y + j*distance,
                                         radius:distance/2 * Math.sqrt(2),
                                         groupIndex:-1});
                    line.push(dot);
                }
            }
            for (j = 0; j < dotNum + 1; j++) {
                for (i = 1; i < dotNum + 1; i++) {
                    sim.addJoint({a:dots[j][i-1].body, b:dots[j][i].body, frequencyHz:frequencyHz});
                    sim.addJoint({a:dots[i-1][j].body, b:dots[i][j].body, frequencyHz:frequencyHz});
                }
                for (i = 2; i < dotNum + 1; i++) {
                    sim.addJoint({a:dots[j][i-2].body, b:dots[j][i].body, frequencyHz:frequencyHz});
                    sim.addJoint({a:dots[i-2][j].body, b:dots[i][j].body, frequencyHz:frequencyHz});
                }
            }

            for (j = 1; j < dotNum + 1; j++) {
                for (i = 1; i < dotNum + 1; i++) {
                    sim.addJoint({a:dots[j-1][i-1].body, b:dots[j][i].body, frequencyHz:frequencyHz});
                    sim.addJoint({a:dots[j-1][i].body, b:dots[j][i-1].body, frequencyHz:frequencyHz});
                }
            }
            for (j = 2; j < dotNum + 1; j++) {
                for (i = 2; i < dotNum + 1; i++) {
                    sim.addJoint({a:dots[j-2][i-2].body, b:dots[j][i].body, frequencyHz:frequencyHz});
                    sim.addJoint({a:dots[j-2][i].body, b:dots[j][i-2].body, frequencyHz:frequencyHz});
                }
            }

            sim.createStageWalls();
            sim.start();
            sim.mouseDrag();

            addChild(_displayLayer = new Bitmap(_displayBitmap));
            
            //drawP(null);
            //var contacts:QuickContacts = sim.addContactListener();
            //contacts.addEventListener(QuickContacts.ADD, drawP);
            addEventListener(Event.ENTER_FRAME, function():void {drawP();} );
        }

        public function drawP():void{
            var g:Graphics = _drawShape.graphics;
            g.clear();
            var i:int, j:int;
            //g.lineStyle(1, 0x000000, 0.1);
            for (j = 0; j < dotNum; j++){
                for (i = 0; i < dotNum; i++){
                    drawSquare(g, bmd, new Point(i*280/4, j*280/4), 280/4,
                               new Point(dots[j][i].body.GetPosition().x*30,     dots[j][i].body.GetPosition().y*30),
                               new Point(dots[j][i+1].body.GetPosition().x*30,   dots[j][i+1].body.GetPosition().y*30),
                               new Point(dots[j+1][i].body.GetPosition().x*30,   dots[j+1][i].body.GetPosition().y*30),
                               new Point(dots[j+1][i+1].body.GetPosition().x*30, dots[j+1][i+1].body.GetPosition().y*30));
                }
            }
            _displayBitmap.copyPixels(_clearBitmap, _rect, _point);
            _displayBitmap.draw(_drawShape);
        }
        private function drawSquare(g:Graphics, bmd:BitmapData, start:Point, len:Number,
                                    p0:Point, p1:Point, p2:Point, p3:Point):void{
            var a:Triangle;
            var b:Triangle;
            a = new Triangle(start,
                             start.add(new Point(len, 0)),
                             start.add(new Point(0, len)));
            b = new Triangle(p0, p1, p2);
            drawTriangle(g, bmd, Triangle.transformMatrix(a, b), b);
    
            a = new Triangle(start.add(new Point(len, 0)),
                             start.add(new Point(len, len)),
                             start.add(new Point(0, len)));
            b = new Triangle(p1, p3, p2);
            drawTriangle(g, bmd, Triangle.transformMatrix(a, b), b);
        }
        private function drawTriangle(g:Graphics, bmd:BitmapData,
                                      m:Matrix, b:Triangle):void{
            g.beginBitmapFill(bmd, m);
            g.moveTo(b.p0.x, b.p0.y);
            g.lineTo(b.p1.x, b.p1.y);
            g.lineTo(b.p2.x, b.p2.y);
            g.lineTo(b.p0.x, b.p0.y);
            g.endFill();
        }
    }
}

import flash.geom.Point;
import flash.geom.Matrix;

class Triangle {
    public var p0:Point;
    public var p1:Point;
    public var p2:Point;
    function Triangle(p0:Point, p1:Point, p2:Point) {
        this.p0 = p0;
        this.p1 = p1;
        this.p2 = p2;
    }
    public function matrix():Matrix {
        return new Matrix(
            p1.x - p0.x, p1.y - p0.y,
            p2.x - p0.x, p2.y - p0.y);
    }
    public static function transformMatrix(a:Triangle, b:Triangle):Matrix {
        var ma : Matrix = a.matrix();
        ma.invert();
        var mb : Matrix = b.matrix();
        var m : Matrix = new Matrix();
        m.translate(-a.p0.x, -a.p0.y);
        m.concat(ma);
        m.concat(mb);
        m.translate(b.p0.x, b.p0.y);
        return m;
    }
}