3Dの回転 線だけ2

by _wonder forked from 3Dの回転 線だけ (diff: 126)
♥0 | Line 92 | Modified 2010-08-04 12:51:02 | MIT License
play

ActionScript3 source code

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

// forked from _wonder's 3Dの回転 線だけ
// forked from _wonder's 3Dの回転 + 線
// forked from _wonder's 3Dの回転
// forked from _wonder's 複数のボール跳ね返り3D
// forked from _wonder's 跳ね返り3D
// forked from _wonder's ボールを動かす3D
// forked from _wonder's 遠ざかるボール
// forked from _wonder's base
package {
    import flash.display.Sprite;
    import flash.events.Event;
    
    public class Line3D2 extends Sprite {
        private var points:Array;
        private var numPoints:Number = 100;
        private var fl:Number = 250;
        private var vpX:Number = stage.stageWidth / 2;
        private var vpY:Number = stage.stageHeight / 2;
        
        public function Line3D2() {
            points = new Array();
            for( var i:uint = 0; i < numPoints; i++ ){
                var point:Point3D = new Point3D( Math.random() * 200 - 100, Math.random() * 200 - 100, Math.random() * 200 - 100 );
                point.setVanishingPoint( vpX, vpY );
                points.push( point );
            }

            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }
        
        private function onEnterFrame(e:Event):void {
            var angleY:Number = ( mouseX - vpX ) * 0.001;
            var angleX:Number = ( mouseY - vpY ) * 0.001;
            for( var i:uint = 0; i < numPoints; i++ ){
                var point:Point3D = points[i];
                point.rotateX( angleX );
                point.rotateY( angleY );
            }
            graphics.clear();
            graphics.lineStyle( 0 );
            graphics.moveTo( points[0].screenX, points[0].screenY );
            for( i = 1; i < numPoints; i++ ){
                graphics.lineTo( points[i].screenX, points[i].screenY );
            }
        }
    }
}

class Point3D {
    public var fl:Number = 250;
    private var vpX:Number = 0;
    private var vpY:Number = 0;
    private var cX:Number = 0;
    private var cY:Number = 0;
    private var cZ:Number = 0;
    public var x:Number = 0;
    public var y:Number = 0;
    public var z:Number = 0;
    
    public function Point3D( x:Number=0, y:Number=0, z:Number=0 ){
        this.x = x;
        this.y = y;
        this.z = z;
    }
    
    public function setVanishingPoint( vpX:Number, vpY:Number ):void {
        this.vpX = vpX;
        this.vpY = vpY;
    }
    
    public function setCenter( cX:Number, cY:Number, cZ:Number=0 ):void {
        this.cX = cX;
        this.cY = cY;
        this.cZ = cZ;
    }
    
    public function get screenX():Number {
        var scale:Number = fl / ( fl + z + cZ );
        return vpX + cX + x * scale;
    }
    
    public function get screenY():Number {
        var scale:Number = fl / ( fl + z + cZ );
        return vpY + cY + y * scale;
    }
    
    public function rotateX( angleX:Number ):void {
        var cosX:Number = Math.cos( angleX );
        var sinX:Number = Math.sin( angleX );
            
        var y1:Number = y * cosX - z * sinX;
        var z1:Number = z * cosX + y * sinX;
            
        y = y1;
        z = z1;
    }
    
    public function rotateY( angleY:Number ):void {
        var cosY:Number = Math.cos( angleY );
        var sinY:Number = Math.sin( angleY );
            
        var x1:Number = x * cosY - z * sinY;
        var z1:Number = z * cosY + x * sinY;
            
        x = x1;
        z = z1;
    }
    
    public function rotateZ( angleZ:Number ):void {
        var cosZ:Number = Math.cos( angleZ );
        var sinZ:Number = Math.sin( angleZ );
            
        var x1:Number = x * cosZ - y * sinZ;
        var y1:Number = y * cosZ + x * sinZ;
            
        x = x1;
        y = y1;
    }
}

Forked