All pastes #2121176 Raw Edit

Stuff

public text v1 · immutable
#2121176 ·published 2012-02-24 02:59 UTC
rendered paste body
float leftrightRot = MathHelper.PiOver2;
        float updownRot = -MathHelper.Pi / 10.0f;
  const float rotationSpeed = 6.0f;
        const float moveSpeed = 30.0f;

   protected override void Update(GameTime gameTime)
        {
 
            Vector3 moveVector = new Vector3(0, 0, 0);
            KeyboardState keyState = Keyboard.GetState();
            if (keyState.IsKeyDown(Keys.Escape))
                this.Exit();

            if (keyState.IsKeyDown(Keys.Up) || keyState.IsKeyDown(Keys.W))
                moveVector += new Vector3(0, 0, -1);
            if (keyState.IsKeyDown(Keys.Down) || keyState.IsKeyDown(Keys.S))
                moveVector += new Vector3(0, 0, 1);
            if (keyState.IsKeyDown(Keys.Right) || keyState.IsKeyDown(Keys.D))
                moveVector += new Vector3(1, 0, 0);
            if (keyState.IsKeyDown(Keys.Left) || keyState.IsKeyDown(Keys.A))
                moveVector += new Vector3(-1, 0, 0);
            if (keyState.IsKeyDown(Keys.Q))
                moveVector += new Vector3(0, 1, 0);
            if (keyState.IsKeyDown(Keys.Z))
                moveVector += new Vector3(0, -1, 0);


             cameraRotationNonFly = Matrix.CreateRotationY(leftrightRot);
          
            rotatedVector = Vector3.Transform(moveVector, cameraRotationNonFly); // fly mode.
        

            cameraPosition += moveSpeed * rotatedVector * (float)(gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f);


            currentMouseState = Mouse.GetState();
            if (currentMouseState != originalMouseState)
            {
                
                float xDifference = currentMouseState.X - originalMouseState.X;
                float yDifference = currentMouseState.Y - originalMouseState.Y;
                Mouse.SetPosition(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2);
                leftrightRot -= rotationSpeed * xDifference * (float)(gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f);
                updownRot -= rotationSpeed * yDifference * (float)(gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f);   
                
            
            if (updownRot < -1)
                updownRot = -1;
            if (updownRot > 2)
                updownRot = 2;

             cameraRotation = Matrix.CreateRotationX(updownRot) * Matrix.CreateRotationY(leftrightRot);
       
             cameraRotatedTarget = Vector3.Transform(cameraOriginalTarget, cameraRotation);
             cameraFinalTarget = cameraPosition + cameraRotatedTarget;
             cameraLookat = cameraPosition + cameraRotatedTarget;

       
             cameraRotatedUpVector = Vector3.Transform(cameraOriginalUpVector, cameraRotation);

          
            }
            view = Matrix.CreateLookAt(cameraPosition, cameraLookat, cameraRotatedUpVector);
            originalMouseState = Mouse.GetState();
          
            base.Update(gameTime);

            
        }