| public Game1() |
| { |
| graphics = new GraphicsDeviceManager(this); |
| Content.RootDirectory = "Content"; |
| graphics.PreferredDepthStencilFormat = SelectStencilMode(); |
| |
| //create the chase camera |
| camera = new ChaseCamera(); |
| // Set the camera offsets |
| camera.DesiredPositionOffset = new Vector3(0.0f, 10, 15); |
| camera.LookAtOffset = new Vector3(0.0f, 5, 0); |
| // Set camera perspective |
| camera.NearPlaneDistance = 1; |
| camera.FarPlaneDistance = 600; |
| |
| } |
| |
| |
| protected override void Initialize() |
| { |
| aspectRatio = (float)GraphicsDevice.Viewport.Width / GraphicsDevice.Viewport.Height; |
| |
| //Vector for the Light Direction |
| lightDir = new Vector3(50, 200, -1); |
| //Create the Shadow matrix |
| shadow = Matrix.CreateShadow(lightDir, new Plane(0, 10, 0, -1)); |
| modelVelocity = Vector3.Zero; |
| // Set the position of the model in world space, and set the rotation. |
| modelPosition = Vector3.Zero; |
| modelRotation = 0.0f; |
| |
| //Vector3 right = Vector3.Right; |
| //Matrix rotationMatrix = Matrix.CreateFromAxisAngle(right,15)* Matrix.CreateRotationY(0); |
| //modelDirection = Vector3.TransformNormal(modelPosition, rotationMatrix); |
| |
| base.Initialize(); |
| |
| // Set the camera aspect ratio |
| // This must be done after the class to base.Initalize() which will |
| // initialize the graphics device. |
| camera.AspectRatio = aspectRatio; |
| |
| // Perform an inital reset on the camera so that it starts at the resting |
| // position. If we don't do this, the camera will start at the origin and |
| // race across the world to get behind the chased object. |
| // This is performed here because the aspect ratio is needed by Reset. |
| UpdateCameraChaseTarget(); |
| camera.Reset(); |
| |
| |
| } |
| |
| |
| |
| private void UpdateCameraChaseTarget() |
| { |
| camera.ChasePosition = modelPosition; |
| camera.ChaseDirection = modelDirection; |
| camera.Up = Vector3.Up; |
| |
| } |
| |
| protected override void Update(GameTime gameTime) |
| { |
| // Allows the game to exit |
| if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) |
| this.Exit(); |
| |
| // Get some input. |
| UpdateInput(); |
| |
| // Add velocity to the current position. |
| modelPosition += modelVelocity; |
| // Bleed off velocity over time. |
| modelVelocity *= 0.95f; |
| // Create the model direction |
| modelDirection = Vector3.Transform(Vector3.Forward, Matrix.CreateFromAxisAngle(Vector3.Up, modelRotation)); |
| |
| // Update the camera to chase the new target |
| UpdateCameraChaseTarget(); |
| |
| // Update the camera |
| camera.Update(gameTime); |
| |
| |
| base.Update(gameTime); |
| } |
| |
| protected void UpdateInput() |
| { |
| // Get the game pad state. |
| GamePadState currentState = GamePad.GetState(PlayerIndex.One); |
| if (currentState.IsConnected) |
| { |
| |
| if (currentState.Triggers.Right > 0.0f) |
| { |
| |
| // Rotate the model using the left thumbstick, and scale it down. |
| modelRotation -= currentState.ThumbSticks.Left.X * 0.02f; |
| |
| // Find out what direction we should be thrusting, using rotation. |
| modelVelocityAdd.X = -(float)Math.Sin(modelRotation); |
| modelVelocityAdd.Z = -(float)Math.Cos(modelRotation); |
| |
| // Now scale our direction by how hard the trigger is down. |
| modelVelocityAdd *= (currentState.Triggers.Right) / 80; |
| |
| // Finally, add this vector to our velocity. |
| modelVelocity += modelVelocityAdd; |
| } |
| |
| |
| |
| // In case you get lost, press A to warp back to the center. |
| if (currentState.Buttons.A == ButtonState.Pressed) |
| { |
| modelPosition = Vector3.Zero; |
| modelVelocity = Vector3.Zero; |
| modelRotation = 0.0f; |
| } |
| } |
| } |