スポンサーサイト

-------- | --:--

上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

[ActionScript 3.0] 3Dでのx/y/z軸に対する回転

2007-08-14 | 21:46

x軸に対する回転行列は

y軸に対する回転行列は

z軸に対する回転行列は

でそれぞれ表される。
Rotate3D.as
package{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.ui.*;
    import flash.utils.*;
    [SWF(width="400", height="300",backgroundColor="0xffffff")]

        public class Rotate3D extends Sprite{
            // camera position
            private var cameraX:Number = 100;
            private var cameraY:Number = 100;
            private var cameraZ:Number = -100;

            // view position
            private var viewX:Number = 0;
            private var viewY:Number = 0;
            private var viewZ:Number = 0;

            private var faceZ:Number = 100;

            private var rad:Number = Math.PI/10;

            private var ballx:Ball = new Ball(0xff0000,50,10,0);
            private var bally:Ball = new Ball(0x00ff00,10,50,0);
            private var ballz:Ball = new Ball(0x0000ff,10,0,50);

            public function Rotate3D(){
                addChild(ballx);
                addChild(bally);
                addChild(ballz);

                addEventListener(Event.ENTER_FRAME, loop);
            }

            private function loop(e:Event):void{
            trace(stage.frameRate);
                //rad += Math.PI/10;
                var sin:Number = Math.sin(rad);
                var cos:Number = Math.cos(rad);
                var x:Number;
                var y:Number;
                var z:Number;

                var view:Point;

                x = ballx.wx;
                y = ballx.wy;
                z = ballx.wz;
                ballx.wx = x;
                ballx.wy = y*cos + z*sin;
                ballx.wz = -y*sin + z*cos;
                view = view2screen(world2view(ballx.wx, ballx.wy, ballx.wz));
                ballx.x = view.x;
                ballx.y = view.y;
                ballx.alpha = (ballx.wz < 0) ? 0.8 : 0.5;

                x = bally.wx;
                y = bally.wy;
                z = bally.wz;
                bally.wx = x*cos -z*sin;
                bally.wy = y;
                bally.wz = x*sin + z*cos;
                view = view2screen(world2view(bally.wx, bally.wy, bally.wz));
                bally.x = view.x;
                bally.y = view.y;
                bally.alpha = (bally.wz < 0) ? 0.8 : 0.5;

                x = ballz.wx;
                y = ballz.wy;
                z = ballz.wz;
                ballz.wx = x*cos + y*sin;
                ballz.wy = -x*sin + y*cos;
                ballz.wz = z;
                view = view2screen(world2view(ballz.wx, ballz.wy, ballz.wz));
                ballz.x = view.x;
                ballz.y = view.y;
                ballz.alpha = (ballz.wx > 0) ? 0.8 : 0.5;

                graphics.clear();

                graphics.lineStyle(1,0,0.3);
                var from:Point;
                var to:Point;
                from = view2screen(world2view(0,0,0));
                to = view2screen(world2view(200,0,0));
                graphics.moveTo(from.x, from.y);
                graphics.lineTo(to.x, to.y);
                from = view2screen(world2view(0,0,0));
                to = view2screen(world2view(0,200,0));
                graphics.moveTo(from.x, from.y);
                graphics.lineTo(to.x, to.y);
                from = view2screen(world2view(0,0,0));
                to = view2screen(world2view(0,0,200));
                graphics.moveTo(from.x, from.y);
                graphics.lineTo(to.x, to.y);

                from = view2screen(world2view(50,0,0));
                graphics.moveTo(from.x, from.y);
                graphics.lineTo(ballx.x, ballx.y);
                from = view2screen(world2view(0,50,0));
                graphics.moveTo(from.x, from.y);
                graphics.lineTo(bally.x, bally.y);
                from = view2screen(world2view(0,0,50));
                graphics.moveTo(from.x, from.y);
                graphics.lineTo(ballz.x, ballz.y);
            }

            private function world2view(x:Number, y:Number, z:Number):Point{
                var xx:Number = (faceZ - cameraZ) * (x - cameraX) / (z - cameraZ) 
                    - (faceZ - cameraZ) * (viewX - cameraX) / (viewZ - cameraZ);
                var yy:Number =  (faceZ - cameraZ) * (y - cameraY) / (z - cameraZ)
                    -(faceZ - cameraZ)* (viewY - cameraY) / (viewZ - cameraZ);
                return new Point(xx,yy);
            }

            private function view2screen(p:Point):Point{
                return new Point(p.x + 200, 200 - p.y);
            }

        }
}
package{
    public class Vector3{
        private var _x:Number;
        private var _y:Number;
        private var _z:Number;

        public function Vector3( x:Number=0.0,y:Number=0.0,z:Number=0.0){
            _x = x ;
            _y = y ;
            _z = z ;
        }

        public function normalize():void{
            var lenv:Number = Math.sqrt(_x*_x + _y*_y + _z*_z);
            _x = _x / lenv;
            _y = _y / lenv;
            _z = _z / lenv;
        }

        public function get x():Number{
            return _x;
        }

        public function get y():Number{
            return _y;
        }

        public function get z():Number{
            return _z;
        }

        public function set x(v:Number):void{
            _x = v;
        }

        public function set y(v:Number):void{
            _y = v;
        }

        public function set z(v:Number):void{
            _z = v;
        }
    }
}
スポンサーサイト

Comment

Post a comment

Secret

上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。