Example #1
0
        void UpdateCameraThirdPerson()
        {
            // Create a vector pointing the direction the camera is facing.
            //var transformedReference = Vector3.Transform(, Matrix.CreateRotationY(avatarYaw));
            //transformedReference = Vector3.Transform(transformedReference, Matrix.CreateRotationX(avatarPitch));

            // Calculate the position the camera is looking from
            var target = avatarPosition + Vector3.Transform(Vector3.UnitZ, Matrix.CreateFromYawPitchRoll(avatarYaw, avatarPitch, 0));

            // Set up the view matrix and projection matrix
            _view = Matrix.CreateLookAt(avatarPosition, target, new Vector3(0.0f, 1.0f, 0.0f));

            var viewport    = _graphics.GraphicsDevice.Viewport;
            var aspectRatio = viewport.Width / (float)viewport.Height;

            _proj = Matrix.CreatePerspectiveFieldOfView(ViewAngle, aspectRatio, NearClip, FarClip);
        }
Example #2
0
        public override void Update(GameTime gameTime)
        {
            if (!Game.IsActive)
            {
                return;
            }

            var mouse    = Mouse.GetState();
            var keyboard = Keyboard.GetState(playerIndex);
            var gamepad  = GamePad.GetState(playerIndex);

            if (MouseEnabled)
            {
                Point mouseDelta = mouseMoveComponent.MouseDelta;
                Yaw   += Angle.Degrees(mouseDelta.X);
                Pitch += Angle.Degrees(mouseDelta.Y);
            }

            /*if(LastMousePosition != null) {
             *  Vector2l last = LastMousePosition.Value, delta = new Vector2l(mousePosition.X - last.X, mousePosition.Y - last.Y);
             *  Yaw += Angle.Degrees(delta.X);
             *  Pitch += Angle.Degrees(delta.Y);
             * }
             *
             * mouse.Position = new Vector2l(Game.Context.Viewport.Width / 2, Game.Context.Viewport.Height / 2);
             * LastMousePosition = new Vector2l(Game.Context.Viewport.Width / 2, Game.Context.Viewport.Height / 2);*/

            bool    run          = keyboard.IsKeyDown(KeyRun);
            float   walkSpeed    = run ? RunSpeed : WalkSpeed;
            Vector3 flyDirection = run ? FlyUpDirection * RunMultiplier : FlyUpDirection;

            if (keyboard.IsKeyDown(KeyFlyUp))
            {
                Position += flyDirection;
            }
            if (keyboard.IsKeyDown(KeyFlyDown))
            {
                Position -= flyDirection;
            }

            Vector2 facing   = new Vector2(0, -1).Rotate(Yaw); // Positive is forward, negative is backwards.
            Vector2 strafing = new Vector2(1, 0).Rotate(Yaw);  // Positive is right, negative is left.
            Vector2 movement = Vector2.Zero;

            if (keyboard.IsKeyDown(KeyForward))
            {
                movement += facing;
            }
            if (keyboard.IsKeyDown(KeyBackward))
            {
                movement -= facing;
            }
            if (keyboard.IsKeyDown(KeyStrafeRight))
            {
                movement += strafing;
            }
            if (keyboard.IsKeyDown(KeyStrafeLeft))
            {
                movement -= strafing;
            }

            if (movement != Vector2.Zero)
            {
                Position += new Vector3(movement.X, 0, movement.Y).Normalized() * walkSpeed;
            }

            Pitch = Pitch.MaxDegrees(-90);
            Pitch = Pitch.MinDegrees(90);

            View = Matrix.CreateTranslation(-Position) *
                   Matrix.CreateFromYawPitchRoll(Yaw.InRadians, 0, 0) *
                   Matrix.CreateFromYawPitchRoll(0, Pitch.InRadians, 0) *
                   Matrix.CreateFromYawPitchRoll(0, 0, Roll.InRadians);

            base.Update(gameTime);
        }