Exemplo n.º 1
0
        public void Update(TimeSpan elapsedTime)
        {
            Vector2 delta         = GetMouseDelta();
            Vector2 deltaDampened = delta * 0.0005f;

            CenterCursor();

            this.MouseDelta = delta;

            // Should perhaps extract the yaw and pitch from the current direction of the camera
            _cameraYaw   += deltaDampened.X;
            _cameraPitch += deltaDampened.Y;

            //

            Vector3 forward  = Mathematics.DirectionFromViewMatrix(Matrix.Invert(this.View));
            Vector3 position = Mathematics.DecomposeToPosition(Matrix.Invert(this.View));

            Matrix  cameraRotation = Matrix.RotationYawPitchRoll(_cameraYaw, _cameraPitch, 0);
            Vector3 newForward     = Vector3.TransformNormal(Vector3.UnitZ, cameraRotation);

            double elapsed         = elapsedTime.TotalSeconds; // Elapsed time since last frame in seconds
            double speedMultiplier = (_engine.Keyboard.IsKeyDown(Key.LeftShift) ? 2 : 1);
            double speed           = 40.0 * speedMultiplier;   // 40 distance units per second
            float  distance        = (float)(speed * elapsed); // d = vt

            // The amount of movement * the direction of movement, then rotate that along the direction we are looking
            Vector3 translateDirection = Vector3.Zero;

            if (_engine.Keyboard.IsKeyDown(Key.W))             // Forwards
            {
                translateDirection += Vector3.TransformNormal(Vector3.UnitZ, cameraRotation);
            }

            if (_engine.Keyboard.IsKeyDown(Key.S))             // Backwards
            {
                translateDirection += Vector3.TransformNormal(-Vector3.UnitZ, cameraRotation);
            }

            if (_engine.Keyboard.IsKeyDown(Key.A))             // Left
            {
                translateDirection += Vector3.TransformNormal(-Vector3.UnitX, cameraRotation);
            }

            if (_engine.Keyboard.IsKeyDown(Key.D))             // Right
            {
                translateDirection += Vector3.TransformNormal(Vector3.UnitX, cameraRotation);
            }

            Vector3 newPosition = position;

            if (translateDirection.LengthSquared() > 0)
            {
                newPosition += Vector3.Normalize(translateDirection) * distance;
            }

            this.View = Matrix.LookAtLH(newPosition, newPosition + newForward, Vector3.UnitY);
        }