Skip to main content
Participant
January 17, 2012
Question

Walk around in space

  • January 17, 2012
  • 1 reply
  • 732 views

Hi everybody,

I make 3d models with away3d and import them into flash builder.

I can rotate the models like this:

public function update(e:Event):void

{

// re-render viewport

    var cameraSpeed:Number = 0.3; // Approximately same speed as mouse movement.

    if (move)

     {

             cam.panAngle = cameraSpeed*(stage.mouseX - lastMouseX) + lastPanAngle;

             cam.tiltAngle = cameraSpeed*(stage.mouseY - lastMouseY) + lastTiltAngle;

     }

  cam.hover();

  view.render();

}

private function MouseDown(event:MouseEvent):void

{

    lastPanAngle = cam.panAngle;

    lastTiltAngle = cam.tiltAngle;

    lastMouseX = stage.mouseX;

    lastMouseY = stage.mouseY;

    move = true;

}

But now I would like to walk around in space (just as you can see here: http://www.youtube.com/watch?v=AsOlw4iIj8E&feature=player_embedded)

I don't think it's very difficult and I think I have to change the code above but I have no idea how.

Does anybody can give me a hint how to do that?

This topic has been closed for replies.

1 reply

_spoboyle
Inspiring
January 17, 2012

you will need to keyboard event listeners and then translate the camera according to which keys are pressed

note that the camera translation will depend on the panAngle and tiltAngle of the camera

Bertie72Author
Participant
January 17, 2012

Thanks for reply.

It's more difficult than I thought.

I have found an example on the internet (http://polygeek.com/3354_flex_walkabout-away3d) but that's flash xml.

I'm not familiar with xml but is this example easy to translate to actionscript 3.0 (flash builder)?

_spoboyle
Inspiring
January 17, 2012

yep dead easy

package

{

    import away3d.cameras.Camera3D;

    import away3d.containers.View3D;

    import away3d.materials.WireframeMaterial;

    import away3d.modifiers.HeightMapModifier;

    import away3d.primitives.Plane;

    import flash.display.BitmapData;

    import flash.display.Sprite;

    import flash.events.KeyboardEvent;

    import flash.events.MouseEvent;

    import flash.events.TimerEvent;

    import flash.geom.Vector3D;

    import flash.ui.Keyboard;

    import flash.utils.Timer;

   

    [SWF(width="550", height="540", backgroundColor='#000000')]

    public class Main extends Sprite

    {       

        public var surface:Sprite;

        private var _camera3D        : Camera3D;

        private var _view3D            : View3D;

        private var _tic            : Timer;

        private var _lastKey        : uint;

        private var _isMouseDown    : Boolean = false;

        private var _isUpKeyDown    : Boolean = false;

        private var _isDownKeyDown    : Boolean = false;

        private var _isRightKeyDown    : Boolean = false;

        private var _isLeftKeyDown    : Boolean = false;

        private var _isPgUpDown        : Boolean = false;

        private var _isPgDwDown        : Boolean = false;

       

        public function Main()

        {

            surface = new Sprite();

            addChild(surface);

           

            init();

        }

       

        private function init():void {

            _camera3D     = new Camera3D();

            _camera3D.z = 0;

            _camera3D.y = -80;

           

            // app-width/height = 550. Center the view.

            _view3D = new View3D( { camera:_camera3D, x:550/2, y:550/2 } );

            surface.addChild( _view3D );

           

            // add the terrain

            makeTerrain();

           

            // set up timer

            _tic = new Timer( 33 );

            _tic.addEventListener( TimerEvent.TIMER, onTic );

            _tic.start();

           

            // Setup keyboard and mouse events

            this.stage.addEventListener( MouseEvent.MOUSE_DOWN,     onMouseDown );

            this.stage.addEventListener( MouseEvent.MOUSE_UP,         onMouseUp );

            this.stage.addEventListener( KeyboardEvent.KEY_DOWN,     onKeyDown );

            this.stage.addEventListener( KeyboardEvent.KEY_UP,         onKeyUp );

        }

           

        private function onTic( e:TimerEvent ):void {

            // Tilt the camera up/down based on mouse position.

            _camera3D.rotationX += -( mouseY - 275 ) * 0.005;

           

            // Keep the _camera3D.rotationX within a 15 to -30 range.

            if( _camera3D.rotationX > 5 ) {

                _camera3D.rotationX = 5;

            } else if( _camera3D.rotationX < -30 ) {

                _camera3D.rotationX = -30;

            }

           

            // spin the camera around based on mouse position.

            _camera3D.rotationY += ( mouseX - 275 ) * 0.005;

           

            // If the mouse is down then movie in space.

            if( _isMouseDown ) {

                var xComponent:Number = Math.sin( _camera3D.rotationY * Math.PI / 180 ) * 10;

                var zComponent:Number = Math.cos( _camera3D.rotationY * Math.PI / 180 ) * 10;

                _camera3D.position = new Vector3D( _camera3D.x + xComponent, _camera3D.y, _camera3D.z + zComponent );

            }

           

            // Handle the keyboard events to walk around in space.

            walk();

            _camera3D.update();

            _view3D.render();

        }

           

        private function onMouseDown( e:MouseEvent ):void {

            _isMouseDown = true;

        }

       

        private function onMouseUp( e:MouseEvent ):void {

            _isMouseDown = false;

        }

       

        private function onKeyDown( e:KeyboardEvent ):void {

            switch ( e.keyCode ) {

                case Keyboard.UP :

                    _isUpKeyDown     = true;

                    break;

                case Keyboard.DOWN :

                    _isDownKeyDown     = true;

                    break;

                case Keyboard.RIGHT :

                    _isRightKeyDown = true;

                    break;

                case Keyboard.LEFT :

                    _isLeftKeyDown    = true;

                    break;

                case Keyboard.PAGE_UP :

                    _isPgUpDown     = true;

                    break;

                case Keyboard.PAGE_DOWN :

                    _isPgDwDown     = true;

                    break;

            }

        }

       

        private function onKeyUp( e:KeyboardEvent ):void {

            switch ( e.keyCode ) {

                case Keyboard.UP :

                    _isUpKeyDown     = false;

                    break;

                case Keyboard.DOWN :

                    _isDownKeyDown     = false;

                    break;

                case Keyboard.RIGHT :

                    _isRightKeyDown = false;

                    break;

                case Keyboard.LEFT :

                    _isLeftKeyDown    = false;

                    break;

                case Keyboard.PAGE_UP :

                    _isPgUpDown     = false;

                    break;

                case Keyboard.PAGE_DOWN :

                    _isPgDwDown     = false;

                    break;

            }

        }

       

        private function walk():void {

            // These variables help us move forward/backward or left/right based on the direction the _camera3D is pointed.

            // Multiplying by 10 is arbitrary and sets the speed which which we move. It would be simple enough to set

            // the multiplier as a variable to simulate acceleration.

            var xComponent : Number = Math.sin( _camera3D.rotationY * Math.PI / 180 ) * 10;

            var zComponent : Number = Math.cos( _camera3D.rotationY * Math.PI / 180 ) * 10;

           

            if( _isUpKeyDown ) {

                _camera3D.position = new Vector3D(

                    _camera3D.x + xComponent,

                    _camera3D.y,

                    _camera3D.z + zComponent );

               

            }

            if( _isDownKeyDown ) {

                _camera3D.position = new Vector3D(

                    _camera3D.x - xComponent,

                    _camera3D.y,

                    _camera3D.z - zComponent );

            }

            if( _isLeftKeyDown  ) {

                _camera3D.position = new Vector3D(

                    _camera3D.x - zComponent,

                    _camera3D.y,

                    _camera3D.z + xComponent );

            }

            if( _isRightKeyDown ) {

                _camera3D.position = new Vector3D(

                    _camera3D.x + zComponent,

                    _camera3D.y,

                    _camera3D.z - xComponent );

            }

            if( _isPgUpDown ) {

                    _camera3D.position = new Vector3D(

                    _camera3D.x, _camera3D.y + 10, _camera3D.z );

            }

            if( _isPgDwDown ) {

                _camera3D.position = new Vector3D(

                _camera3D.x, _camera3D.y - 10, _camera3D.z );

            }

        }

       

        private function makeTerrain():void {

            // Create a bitmap that is filled with Perlin Noise that will get translated into

            // the HeightMapModifier to add bumps to the plane.

            var bmp : BitmapData = new BitmapData( 4000, 4000, false );

            bmp.perlinNoise( 20, 20, 2, 0, true, true, 7, true );

           

            // Simple WireframeMaterial to give the plane that early 80s video game look.

            var material : WireframeMaterial = new WireframeMaterial( 0xFFFFFF );

            material.wireAlpha     = 0.5;

           

            // Plane

            var plane : Plane     = new Plane();

            plane.width         = 4000;

            plane.height         = 4000;

            plane.segmentsW     = 50;

            plane.segmentsH     = 50;

            plane.material         = material;

           

            // Modifier

            var modifier : HeightMapModifier = new HeightMapModifier( plane, bmp );

            modifier.scale         = 0.9;

            modifier.execute();

           

            _view3D.scene.addChild( plane );

        }

    }

}