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: 3D cube

/* @author Thy
    * 3D Particles. Its supposed to be an galaxy.
    * actually.. i wanted to make an 2 galaxy colision. but this is only 1 galaxy #fail (need setup initial speed)
    * this example is just the old and heavy gravity.
    */
Get Adobe Flash player
by Thy 14 Jan 2011
/**
 * Copyright Thy ( http://wonderfl.net/user/Thy )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/l1oL
 */

// forked from Thy's 3D cube
package 
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.text.TextField;
    import flash.display.Sprite;
    import flash.display.Graphics;
    import flash.events.Event;
    // 
    import com.bit101.components.*;
    /* @author Thy
    */
    public class Main extends Sprite
    {
        
        private var // 3D cene objects
        galaxy:Ob = new Ob(),
        camera:Cam = new Cam();
        
        private var // ui
        slide1:HUISlider,
        slide2:HUISlider,
        slide3:HUISlider,
        slide4:HUISlider;
        
        private const // const degree to radians proportion
        rads:Number = Math.PI / 180;
        
        private var
        data:BitmapData = new BitmapData(stage.stageWidth, 
            stage.stageHeight - 80, false, 0),
        bm:Bitmap = new Bitmap(data, "auto", true);

        public function Main():void
        {
            // camera settings
            camera.z = -100;
            camera.focal = 500;
            camera.offsetX = stage.stageWidth*.5;
            camera.offsetY = stage.stageHeight*.5;
            
            // box
            this.addChild(bm);
            bm.y = 80;
            // (note that I already added the box to the display list)
            var i:int = -1, l:int = 800; // how many stars
            
            while(++i < l)
            {
                galaxy.points.push(new P(
                    Math.cos(i*.1) * i * 300,
                    (Math.random() - .5)*3000, 
                    Math.sin(i*.1) * i * 300 +10));
            }
            galaxy.points[0].mass = 100;

            // event
            stage.addEventListener(Event.ENTER_FRAME, ef);
            
            // ui
            slide1 = new HUISlider(this, 0,0, "pitch");
            slide2 = new HUISlider(this, 0,20,"yaw ");
            slide3 = new HUISlider(this, 0,40,"roll  ");
            slide4 = new HUISlider(this, 0, 60, "nothin"/*"focal length"*/);
            slide1.width = slide2.width = slide3.width = 
                slide4.width = stage.stageWidth;
            slide1.maximum = slide2.maximum = slide3.maximum = 360;
            slide4.maximum = 500;
            slide1.value = 15;
            slide2.value = 50;
            slide4.value = 30;
        }
        
        private function ef(e:Event):void
        {
            // rotate the object box, NOT the camera
            galaxy.pitch = slide1.value * rads;
            galaxy.yaw = slide2.value * rads;
            galaxy.roll = slide3.value * rads;
            // angles were in degree, so we get the radians
            
            // set the camera focal length
            camera.focal = slide4.value;
            
            // move the 3D galaxy
            move3D(galaxy);
            // do 3D to 2D conversion
            get2D(galaxy, camera);
            // draw the especific galaxy
            data.lock();
            data.fillRect(data.rect, 0);
            draw(galaxy, camera, data);
            data.unlock();
        }
        
        private function move3D(ob:Ob = null):void
        {
            var i:int = -1, j:int, l:int = ob.points.length;
            var p:P, p2:P;
            var d:Number, dx:Number, dy:Number, dz:Number;
            while(++i < l)
            {
                p = ob.points[i];
                j = -1;
                while(++j < i)
                {
                    if (j == i) continue;
                    p2 = ob.points[j];
                    d = Math.sqrt((dx = (p.x-p2.x)) * dx + 
                        (dy = (p.y-p2.y)) * dy + 
                        (dz = (p.z-p2.z)) * dz);
                    d /= p2.mass; 
                    p.vx -= dx / d;
                    p.vy -= dy / d;
                    p.vz -= dz / d;
                }

                p.x += p.vx;
                p.y += p.vy;
                p.z += p.vz;
            }
        }

        
        private function get2D(ob:Ob = null, cam:Cam = null):void
        {
            // get the 2D coord of all points, incluing the rotation
            var i:int = -1, l:int = ob.points.length, p:P;
            var a1:Number, a2:Number, b1:Number, b2:Number,
                c1:Number, c2:Number;
            var X:Number, Y:Number, Z:Number,
                r1:Number = ob.pitch, 
                r2:Number = ob.yaw,
                r3:Number = ob.roll;
            var sr1:Number = Math.sin(r1),
                cr1:Number = Math.cos(r1),
                sr2:Number = Math.sin(r2),
                cr2:Number = Math.cos(r2),
                sr3:Number = Math.sin(r3),
                cr3:Number = Math.cos(r3);
                
            while(++i < l)
            {
                p = ob.points[i];
                X = p.x - cam.x;
                Y = p.y - cam.y;
                Z = p.z - cam.z;
                
                a1 = cr1*p.y - sr1*p.z; // pitch (x)
                a2 = sr1*p.y + cr1*p.z;
                
                b1 = cr2*a2 - sr2*p.x; // yaw (y)
                b2 = sr2*a2 + cr2*p.x;
                
                c1 = cr3*b2 - sr3*a1; // roll (z)
                c2 = sr3*b2 + cr3*a1;
                
                if(b1 > 0 || true)
                {
                    p.scale = cam.focal / (cam.focal + b1)
                    p.X = c1 * .0002  /*p.scale*/   + cam.offsetX;
                    p.Y = c2 * .0002  /*p.scale*/   + cam.offsetY;
                } else
                {
                    p.scale = cam.focal / (cam.focal + b1)
                    p.X = 0;
                    p.Y = 0;
                }
            }
        }
        
        private function draw(ob:Ob = null, cam:Cam = null, 
            d:BitmapData = null):void
        {
            // draw the especific galaxy
            var i:int = -1, l:int = ob.points.length;
            var p:P;
            while(++i < l)
            {
                p = ob.points[i];
                data.setPixel(p.X, p.Y, 0xFFFFFF);
            }

        }
    }
}


import flash.display.Sprite;

class P{
    public var // position
    x:Number, y:Number = 0, z:Number = 0,
    X:Number = 0, Y:Number = 0, scale:Number = 0;
    
    public var // velocity
    vx:Number = 0, vy:Number = 0, 
        vz:Number = 0;
    
    public var // each point will be an star
    mass:int = 1;
    
    public function P(x:Number = 0,  y:Number = 0, z:Number = 0)
    {
        this.x = x; this.y = y; this.z = z;
        this.X = X; this.Y = Y;
    }
}

class Ob {
    public var 
    points:Vector.<P> = new Vector.<P>(),
    pitch:Number = 0, yaw:Number = 0, roll:Number = 0; 
}

class Cam{
    
    public var
    x:Number = 0, y:Number = 0, z:Number = 0,
    // I STILL DONT UNDERSTAND THE *CAMERA* ROTATIONS, only the *OBJECT* ones
    //pitch:Number = 0, yaw:Number = 0, roll:Number = 0, //
    focal:Number = 0, offsetX:Number = 0, offsetY:Number = 0;
}