スポンサーサイト

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

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

[ActionScript 3.0] 自力で3D(カメラ視点移動)

2007-08-13 | 21:31

前回作成したバージョンにカメラの視点変更移動機能を追加。
(ウィンドウをクリックしてフォーカスを移した後、上下左右キーでカメラが移動。Wで上、Dで右、Xで下、Aで左に視点移動)

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 ThreeD3 extends Sprite{
            // box position
            private var x1:Number = 0 ;
            private var z1:Number = 200;

            // camera position
            private var cameraX:Number = 0;
            private var cameraY:Number = 50;
            private var cameraZ:Number = 0;

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

            private var faceZ:Number = 100;

            private static var HEIGHT:Number =200;
            private static var WIDTH:Number =150;

            private static var poleX:Number = -50;
            private static var poleZ:Number = 250;

            public function ThreeD3(){
                addEventListener(Event.ENTER_FRAME, loop);
                stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
                x1 = 100;
            }

            private function onKeyDown(e:KeyboardEvent):void{
                trace(e.keyCode);
                switch(e.keyCode){
                    case Keyboard.UP:
                        if(cameraZ < 100){
                            cameraZ++;
                            faceZ++;
                        }
                        break;
                    case Keyboard.DOWN:
                        cameraZ--;
                        faceZ--;
                        break;
                    case Keyboard.LEFT:
                        cameraX -= 2;
                        break;
                    case Keyboard.RIGHT:
                        cameraX += 2;
                        break;
                    case 65:
                        viewX -= 5;
                        break;
                    case 68:
                        viewX += 5;
                        break;
                    case 87:
                        viewY += 5;
                        break;
                    case 88:
                        viewY -= 5;
                        break;
                }
                var from:Point = world2view(poleX,0,poleZ);
                trace (from);
                from = world2view(0,0,poleZ);
                trace (from);
            }

            private function loop(e:Event):void{
                graphics.clear();

                var vertex:Array = new Array();
                vertex[0] = view2screen(world2view(x1, HEIGHT, z1));
                vertex[1] = view2screen(world2view(x1+WIDTH, HEIGHT, z1));
                vertex[2] = view2screen(world2view(x1+WIDTH, HEIGHT, z1+WIDTH));
                vertex[3] = view2screen(world2view(x1, HEIGHT, z1+WIDTH));
                vertex[4] = view2screen(world2view(x1, 0, z1));
                vertex[5] = view2screen(world2view(x1+WIDTH, 0, z1));
                vertex[6] = view2screen(world2view(x1+WIDTH, 0, z1+WIDTH));
                vertex[7] = view2screen(world2view(x1, 0, z1+WIDTH));

                graphics.beginFill(0x0000ff,0.8);
                graphics.drawRect(vertex[0].x, vertex[0].y,
                        vertex[1].x - vertex[0].x, vertex[4].y - vertex[0].y);
                graphics.endFill();

                var seq:Array = [1,2,3,0,4,5,6,7,4,5,1,2,6,7,3]
                graphics.lineStyle(4, 0xff, 0.5);
                graphics.moveTo(vertex[0].x, vertex[0].y);
                for each(var ix:int in seq)
                    graphics.lineTo(vertex[ix].x, vertex[ix].y);

                drawGrid();

            }

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

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

            private function drawGrid():void{
                graphics.lineStyle(1, 0, 0.8);
                var from:Point;
                var to:Point;
                from = view2screen(world2view(0, 0, 150));
                to = view2screen(world2view(0, 0, 500));
                graphics.moveTo(from.x, from.y);
                graphics.lineTo(to.x, to.y);
                from = view2screen(world2view(100, 0, 150));
                to = view2screen(world2view(100, 0, 500));
                graphics.moveTo(from.x, from.y);
                graphics.lineTo(to.x, to.y);
                from = view2screen(world2view(200, 0, 150));
                to = view2screen(world2view(200, 0, 500));
                graphics.moveTo(from.x, from.y);
                graphics.lineTo(to.x, to.y);
                from = view2screen(world2view(-400, 0, 200));
                to = view2screen(world2view(400, 0, 200));
                graphics.moveTo(from.x, from.y);
                graphics.lineTo(to.x, to.y);
                from = view2screen(world2view(-400, 0, 300));
                to = view2screen(world2view(400, 0, 300));
                graphics.moveTo(from.x, from.y);
                graphics.lineTo(to.x, to.y);
            }
        }
}
スポンサーサイト

Comment

Post a comment

Secret

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