void updateCamera(GameTime gameTime) { // Get the new keyboard and mouse state MouseState mouseState = Mouse.GetState(); KeyboardState keyState = Keyboard.GetState(); // Determine how much the camera should turn float deltaX = (float)lastMouseState.X - (float)mouseState.X; float deltaY = (float)lastMouseState.Y - (float)mouseState.Y; // Rotate the camera ((FreeCamera)camera).Rotate(deltaX * .005f, deltaY * .005f); Vector3 translation = Vector3.Zero; // Determine in which direction to move the camera if (keyState.IsKeyDown(Keys.W)) { translation += Vector3.Forward; } if (keyState.IsKeyDown(Keys.S)) { translation += Vector3.Backward; } if (keyState.IsKeyDown(Keys.A)) { translation += Vector3.Left; } if (keyState.IsKeyDown(Keys.D)) { translation += Vector3.Right; } // Move 3 units per millisecond, independent of frame rate translation *= 4 * (float)gameTime.ElapsedGameTime.TotalMilliseconds; // Move the camera ((FreeCamera)camera).Move(translation); // Update the camera camera.Update(); // Update the mouse state lastMouseState = mouseState; }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } quad.Clear(); quad.AddObject(player); // gameObjects.Add(player); //adding all game objects to the quad tree for (int i = 0; i < gameObjects.Count; i++) { quad.AddObject(gameObjects[i]); } // TODO: Add your update logic here prevMouseState = mouseState; mouseState = Mouse.GetState(); foreach (Enemy enemy in enemies) { enemy.Update(gameTime); } player.Update(gameTime); camera.Update(player.Position, map.Width, map.Height); //map.CheckCollision(player); var scaledMousePosition = Vector2.Transform(mousePosition, Matrix.Invert(viewportAdapter.GetScaleMatrix())); base.Update(gameTime); }
// Called when the game should draw itself protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.Black); RasterizerState originalRasterizerState = graphics.GraphicsDevice.RasterizerState; RasterizerState rasterizerState = new RasterizerState(); rasterizerState.CullMode = CullMode.None; graphics.GraphicsDevice.RasterizerState = rasterizerState; skybox.Draw(camera.View, camera.Projection, ((TargetCamera)camera).Position); graphics.GraphicsDevice.RasterizerState = originalRasterizerState; rasterizerState = new RasterizerState(); rasterizerState.CullMode = CullMode.CullCounterClockwiseFace; // rasterizerState.CullMode = CullMode.CullClockwiseFace; // rasterizerState.CullMode = CullMode.None; graphics.GraphicsDevice.RasterizerState = rasterizerState; GraphicsDevice.Viewport = leftViewport; ((TargetCamera)camera).Target = new Microsoft.Xna.Framework.Vector3(tempbus_target_I.X, tempbus_target_I.Y, tempbus_target_I.Z); ((TargetCamera)camera).Position = new Microsoft.Xna.Framework.Vector3(tempbus_Position_I.X, tempbus_Position_I.Y, tempbus_Position_I.Z); camera.Update(); skybox2.Draw(camera.Projection, ((TargetCamera)camera).Target, ((TargetCamera)camera).Position, -1.57f, graphics, GraphicsDevice); foreach (CModel model in models) { if (camera.BoundingVolumeIsInView(model.BoundingSphere)) { model.Draw(camera.View, camera.Projection, ((TargetCamera)camera).Position); } } foreach (BusModel model in Bus_models) { model.Draw(camera.View, camera.Projection, ((TargetCamera)camera).Position); } //render cenetr graphics.GraphicsDevice.RasterizerState = originalRasterizerState; GraphicsDevice.Viewport = centerViewport; ((TargetCamera)camera).Target = new Microsoft.Xna.Framework.Vector3(tempbus_target_C.X, tempbus_target_C.Y, tempbus_target_C.Z); ((TargetCamera)camera).Position = new Microsoft.Xna.Framework.Vector3(tempbus_Position_C.X, tempbus_Position_C.Y, tempbus_Position_C.Z); camera.Update(); skybox2.Draw(camera.Projection, ((TargetCamera)camera).Target, ((TargetCamera)camera).Position, -0.0f, graphics, GraphicsDevice); foreach (CModel model in models) { if (camera.BoundingVolumeIsInView(model.BoundingSphere)) { model.Draw(camera.View, camera.Projection, ((TargetCamera)camera).Position); } } foreach (BusModel model in Bus_models) { model.Draw(camera.View, camera.Projection, ((TargetCamera)camera).Position); } //render Rigth graphics.GraphicsDevice.RasterizerState = originalRasterizerState; GraphicsDevice.Viewport = rightViewport; ((TargetCamera)camera).Target = new Microsoft.Xna.Framework.Vector3(tempbus_target_D.X, tempbus_target_D.Y, tempbus_target_D.Z); ((TargetCamera)camera).Position = new Microsoft.Xna.Framework.Vector3(tempbus_Position_D.X, tempbus_Position_D.Y, tempbus_Position_D.Z); camera.Update(); skybox2.Draw(camera.Projection, ((TargetCamera)camera).Target, ((TargetCamera)camera).Position, +1.57f, graphics, GraphicsDevice); foreach (CModel model in models) { if (camera.BoundingVolumeIsInView(model.BoundingSphere)) { model.Draw(camera.View, camera.Projection, ((TargetCamera)camera).Position); } } foreach (BusModel model in Bus_models) { model.Draw(camera.View, camera.Projection, ((TargetCamera)camera).Position); } graphics.GraphicsDevice.RasterizerState = originalRasterizerState; base.Draw(gameTime); }