/// <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) { float dt = (float)gameTime.ElapsedGameTime.TotalSeconds; KeyboardState = Keyboard.GetState(); MouseState = Mouse.GetState(); // Allows the game to exit if (KeyboardState.IsKeyDown(Keys.Escape)) { this.Exit(); return; } Camera.Update(dt); if (MouseState.LeftButton == ButtonState.Pressed) { Box toAdd = new Box(Camera.Position, 1, 1, 1, 1); toAdd.LinearVelocity = Camera.WorldMatrix.Forward * 10; Space.Add(toAdd); ModelDrawer.Add(toAdd); } Space.Update(); ModelDrawer.Update(); base.Update(gameTime); }
/// <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) { KeyboardState = Keyboard.GetState(); MouseState = Mouse.GetState(); // Allows the game to exit if (KeyboardState.IsKeyDown(Keys.Escape)) { this.Exit(); return; } camera.Update((float)gameTime.ElapsedGameTime.TotalSeconds); //Calling Space.Add while it is possibly running is a no-no; it will interfere //with the update process and probably crash! Instead, enqueue everything //to a thread-safe buffer which will be flushed in the physics loop. if (MouseState.LeftButton == ButtonState.Pressed) { Box toAdd = new Box(camera.Position, 1, 1, 1, 1); toAdd.LinearVelocity = camera.WorldMatrix.Forward * 10; Space.SpaceObjectBuffer.Add(toAdd); modelDrawer.Add(toAdd); } //Prevent the engine from flipping read buffers while we're reading data out of it. //It's a good idea to hold the LockerMotionStateBuffers as briefly as possible; //This will block if the engine tries to flip its internal buffers. //Technically, Space.BufferedStates.InterpolatedStates.GetStates() would //hold the lock more briefly, but this lock lets us use the BEPUphysicsDrawer //system without significant difficulty or modifications. lock (Space.BufferedStates.InterpolatedStates.FlipLocker) { modelDrawer.Update(); } base.Update(gameTime); }
/// <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) { PreviousKeyboardInput = KeyboardInput; KeyboardInput = Keyboard.GetState(); var dt = (float)gameTime.ElapsedGameTime.TotalSeconds; #if WINDOWS PreviousMouseInput = MouseInput; MouseInput = Mouse.GetState(); //Keep the mouse within the screen if (!IsMouseVisible) { Mouse.SetPosition(200, 200); } #endif PreviousGamePadInput = GamePadInput; for (int i = 0; i < 4; i++) { GamePadInput = GamePad.GetState((PlayerIndex)i); if (GamePadInput.IsConnected) { break; } } // Allows the default game to exit on Xbox 360 and Windows if (KeyboardInput.IsKeyDown(Keys.Escape) || GamePadInput.Buttons.Back == ButtonState.Pressed) { Exit(); } //Toggle mouse control. The camera will look to the IsMouseVisible to determine if it should turn. if (WasKeyPressed(Keys.Tab)) { IsMouseVisible = !IsMouseVisible; } #region UI Toggles #if !WINDOWS if (WasButtonPressed(Buttons.Start)) { displayMenu = !displayMenu; } #else if (WasKeyPressed(Keys.F1)) { displayMenu = !displayMenu; } #endif if (WasKeyPressed(Keys.I)) { if (KeyboardInput.IsKeyDown(Keys.RightShift) || KeyboardInput.IsKeyDown(Keys.LeftShift)) { displayActiveEntityCount = !displayActiveEntityCount; } else { displayUI = !displayUI; } } if (WasKeyPressed(Keys.J)) { displayConstraints = !displayConstraints; } if (WasKeyPressed(Keys.K)) { displayContacts = !displayContacts; } if (WasKeyPressed(Keys.U)) { displayBoundingBoxes = !displayBoundingBoxes; } if (WasKeyPressed(Keys.Y)) { displayEntities = !displayEntities; } if (WasKeyPressed(Keys.H)) { displaySimulationIslands = !displaySimulationIslands; } if (WasKeyPressed(Keys.G)) { ModelDrawer.IsWireframe = !ModelDrawer.IsWireframe; } #endregion #region Simulation Switcharoo #if !WINDOWS int switchTo = -2; if (WasButtonPressed(Buttons.DPadLeft)) { switchTo = currentSimulationIndex - 1; } else if (WasButtonPressed(Buttons.DPadRight)) { switchTo = currentSimulationIndex + 1; } if (switchTo != -2) { if (switchTo < 1) { switchTo += demoTypes.Length; } else if (switchTo > demoTypes.Length) { switchTo = 1; } SwitchSimulation(switchTo); } #else foreach (Keys key in KeyboardInput.GetPressedKeys()) { int code = key.GetHashCode(); if (code >= 48 && code <= 57) { int simNumber; if (code == 48) { simNumber = 10; } else { simNumber = code - 48; } if (KeyboardInput.IsKeyDown(Keys.LeftShift)) { simNumber += 10; } if (KeyboardInput.IsKeyDown(Keys.LeftControl)) { simNumber += 20; } if (simNumber <= demoTypes.Length) { SwitchSimulation(simNumber); } } } #endif #endregion currentSimulation.Update(dt); if (displayConstraints) { ConstraintDrawer.Update(); } if (displayEntities) { ModelDrawer.Update(); } base.Update(gameTime); }
protected override void Update(GameTime gameTime) { Input.Get().Update(); if (Input.Get().IsKeyDown(Keys.Escape, true)) { _paused = !_paused; } if (Input.Get().IsKeyDown(Keys.F2, true)) { _chunkManager.ChunkSystem.Renderer.ToggleDebugMode(ChunkRendererDebugOptions.DEBUG_DRAW_WIREFRAME); wireFrame = !wireFrame; } if (Input.Get().IsKeyDown(Keys.F3, true)) { _chunkManager.ChunkSystem.Renderer.ToggleDebugMode(ChunkRendererDebugOptions.DEBUG_DRAW_NORMALS); } if (Input.Get().IsKeyDown(Keys.F4, true)) { _chunkManager.ChunkSystem.Renderer.ToggleDebugMode(ChunkRendererDebugOptions.DEBUG_DRAW_RENDERTARGETS); } if (Input.Get().IsKeyDown(Keys.F5, true)) { drawPhysics = !drawPhysics; } if (Input.Get().IsKeyDown(Keys.F5, true)) { if (pickedPos != Vector3I.One * -1) { Chunk c = _world.ChunkAt(pickedPos.X, pickedPos.Y, pickedPos.Z); _chunkManager.EnqueueChunkForBuild(c); } } int centerX = GraphicsDevice.Viewport.Width / 2; int centerY = GraphicsDevice.Viewport.Height / 2; if (!_paused) { cam.Yaw += -(Input.Get().MouseXCoordinate() - centerX) * cam.RotateSpeed; cam.Pitch += -(Input.Get().MouseYCoordinate() - centerY) * cam.RotateSpeed; Mouse.SetPosition(centerX, centerY); } Vector3 moveVector = Vector3.Zero; if (Input.Get().IsKeyDown(Keys.LeftControl, true)) { cam.Position = new Vector3((int)cam.Position.X, (int)cam.Position.Y, (int)cam.Position.Z); cam.Yaw = (int)(cam.Yaw / MathHelper.PiOver2) * MathHelper.PiOver2; cam.Pitch = (int)(cam.Pitch / MathHelper.PiOver2) * MathHelper.PiOver2; } bool shouldBeNew = Input.Get().IsKeyDown(Keys.LeftControl); if (Input.Get().IsKeyDown(Keys.W, shouldBeNew)) { moveVector += cam.Forward * 0.5f; } if (Input.Get().IsKeyDown(Keys.S, shouldBeNew)) { moveVector -= cam.Forward * 0.5f; } if (Input.Get().IsKeyDown(Keys.D, shouldBeNew)) { moveVector += cam.Right * 0.5f; } if (Input.Get().IsKeyDown(Keys.A, shouldBeNew)) { moveVector -= cam.Right * 0.5f; } if (Input.Get().IsKeyDown(Keys.E, shouldBeNew)) { moveVector += Vector3.Up * 0.5f; } if (Input.Get().IsKeyDown(Keys.Q, shouldBeNew)) { moveVector -= Vector3.Up * 0.5f; } if (!Input.Get().IsKeyDown(Keys.Tab)) { moveVector *= 0.125f; } moveVector *= (float)gameTime.ElapsedGameTime.TotalSeconds / (1f / 60f); float mult = 1 / 2048f * (float)gameTime.ElapsedGameTime.TotalSeconds / (1f / 1000f); if (Input.Get().IsKeyDown(Keys.LeftShift)) { mult *= 2; } if (Input.Get().IsKeyDown(Keys.Right)) { cam.Yaw -= MathHelper.Pi * mult; } if (Input.Get().IsKeyDown(Keys.Left)) { cam.Yaw += MathHelper.Pi * mult; } if (Input.Get().IsKeyDown(Keys.Up)) { cam.Pitch -= MathHelper.Pi * mult; } if (Input.Get().IsKeyDown(Keys.Down)) { cam.Pitch += MathHelper.Pi * mult; } cam.Position += moveVector; cam.Update(); pickedPos = Vector3I.One * -1; sidePickedPos = Vector3I.One * -1; Vector3I lastEmptyBlock = new Vector3I((int)(cam.Position.X + 0.5f), (int)(cam.Position.Y + 0.5f), (int)(cam.Position.Z + 0.5f)); // block picking for (float f = 0; f < 32f; f += 0.1f) { Vector3 pos = cam.Position + cam.Forward * f; int rx = (int)(pos.X + 0.5f); int ry = (int)(pos.Y + 0.5f); int rz = (int)(pos.Z + 0.5f); GridPoint gridPoint = _world.PointAt(rx, ry, rz); if (gridPoint.Density >= 0.0f) { pickedPos = new Vector3I(rx, ry, rz); sidePickedPos = lastEmptyBlock; break; } else { lastEmptyBlock = new Vector3I(rx, ry, rz); } } if (pickedPos == Vector3I.One * -1) { sidePickedPos = pickedPos; } if ((timeout -= (float)gameTime.ElapsedGameTime.TotalSeconds) <= 0 && Input.Get().IsLeftMouseButtonDown() && pickedPos != Vector3I.One * -1) { int rx = pickedPos.X; int ry = pickedPos.Y; int rz = pickedPos.Z; GridPoint gridPoint = _world.PointAt(rx, ry, rz); if (gridPoint.Density >= 0.0f) { pickedPos = new Vector3I(rx, ry, rz); GridPoint selected = new GridPoint(gridPoint, (int)DualContouringMetadataIndex.Length); // modify the normals and density selected.Density = -1.0f; // make the vectors point towards the point selected.XPositiveNormal = new Vector3(-1, 0, 0); selected.YPositiveNormal = new Vector3(0, -1, 0); selected.ZPositiveNormal = new Vector3(0, 0, -1); _world.SetPoint(rx, ry, rz, selected); gridPoint = _world.PointAt(rx, ry - 1, rz); if (gridPoint.Density >= 0) { selected = new GridPoint(gridPoint, (int)DualContouringMetadataIndex.Length); selected.Density = 1.0f; selected.YPositiveNormal = new Vector3(0, 1, 0); _world.SetPoint(rx, ry - 1, rz, selected); } gridPoint = _world.PointAt(rx - 1, ry, rz); if (gridPoint.Density >= 0) { selected = new GridPoint(gridPoint, (int)DualContouringMetadataIndex.Length); selected.Density = 1.0f; selected.XPositiveNormal = new Vector3(1, 0, 0); _world.SetPoint(rx - 1, ry, rz, selected); } gridPoint = _world.PointAt(rx, ry, rz - 1); if (gridPoint.Density >= 0) { selected = new GridPoint(gridPoint, (int)DualContouringMetadataIndex.Length); selected.Density = 1.0f; selected.ZPositiveNormal = new Vector3(0, 0, 1); _world.SetPoint(rx, ry, rz - 1, selected); } gridPoint = _world.PointAt(rx, ry, rz + 1); if (gridPoint.Density >= 0) { selected = new GridPoint(gridPoint, (int)DualContouringMetadataIndex.Length); selected.Density = 1.0f; _world.SetPoint(rx, ry, rz + 1, selected); } gridPoint = _world.PointAt(rx, ry + 1, rz); if (gridPoint.Density >= 0) { selected = new GridPoint(gridPoint, (int)DualContouringMetadataIndex.Length); selected.Density = 1.0f; _world.SetPoint(rx, ry + 1, rz, selected); } gridPoint = _world.PointAt(rx + 1, ry, rz); if (gridPoint.Density >= 0) { selected = new GridPoint(gridPoint, (int)DualContouringMetadataIndex.Length); selected.Density = 1.0f; _world.SetPoint(rx + 1, ry, rz, selected); } timeout = 0.2f; if (Input.Get().IsKeyDown(Keys.Space)) { timeout = 0.01f; } } } if (timeout <= 0 && Input.Get().IsRightMouseButtonDown() && sidePickedPos != Vector3I.One * -1) { int rx = sidePickedPos.X; int ry = sidePickedPos.Y; int rz = sidePickedPos.Z; GridPoint gridPoint = _world.PointAt(rx, ry, rz); if (gridPoint.Density < 0.0f) { pickedPos = new Vector3I(rx, ry, rz); GridPoint selected = new GridPoint(gridPoint, (int)DualContouringMetadataIndex.Length); // modify the normals and density selected.Density = 1.0f; // make the vectors point away from the point selected.XPositiveNormal = new Vector3(1, 0, 0); selected.YPositiveNormal = new Vector3(0, 1, 0); selected.ZPositiveNormal = new Vector3(0, 0, 1); _world.SetPoint(rx, ry, rz, selected); gridPoint = _world.PointAt(rx, ry - 1, rz); if (gridPoint.Density < 0) { selected = new GridPoint(gridPoint, (int)DualContouringMetadataIndex.Length); selected.Density = -1.0f; selected.YPositiveNormal = new Vector3(0, -1, 0); _world.SetPoint(rx, ry - 1, rz, selected); } gridPoint = _world.PointAt(rx - 1, ry, rz); if (gridPoint.Density < 0) { selected = new GridPoint(gridPoint, (int)DualContouringMetadataIndex.Length); selected.Density = -1.0f; selected.XPositiveNormal = new Vector3(-1, 0, 0); _world.SetPoint(rx - 1, ry, rz, selected); } gridPoint = _world.PointAt(rx, ry, rz - 1); if (gridPoint.Density < 0) { selected = new GridPoint(gridPoint, (int)DualContouringMetadataIndex.Length); selected.Density = -1.0f; selected.ZPositiveNormal = new Vector3(0, 0, -1); _world.SetPoint(rx, ry, rz - 1, selected); } gridPoint = _world.PointAt(rx, ry, rz + 1); if (gridPoint.Density < 0) { selected = new GridPoint(gridPoint, (int)DualContouringMetadataIndex.Length); selected.Density = -1.0f; _world.SetPoint(rx, ry, rz + 1, selected); } gridPoint = _world.PointAt(rx, ry + 1, rz); if (gridPoint.Density < 0) { selected = new GridPoint(gridPoint, (int)DualContouringMetadataIndex.Length); selected.Density = -1.0f; _world.SetPoint(rx, ry + 1, rz, selected); } gridPoint = _world.PointAt(rx + 1, ry, rz); if (gridPoint.Density < 0) { selected = new GridPoint(gridPoint, (int)DualContouringMetadataIndex.Length); selected.Density = -1.0f; _world.SetPoint(rx + 1, ry, rz, selected); } timeout = 0.2f; if (Input.Get().IsKeyDown(Keys.Space)) { timeout = 0.01f; } } } if (Input.Get().IsKeyDown(Keys.B, true)) { Sphere sphere = new Sphere(MathConverter.Convert(cam.Position + cam.Forward * 2), 1.0f, 1); sphere.LinearVelocity = MathConverter.Convert(cam.Forward * 5); lock (_physicsMutex) { _space.Add(sphere); modelDrawer.Add(sphere); } } _world.Update(gameTime); lock (_physicsMutex) { _space.Update(); if (drawPhysics) { modelDrawer.IsWireframe = wireFrame; modelDrawer.Update(); } } _fpsCounter.Update(gameTime); base.Update(gameTime); }
/// <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) { PreviousKeyboardInput = KeyboardInput; KeyboardInput = Keyboard.GetState(); var dt = (float)gameTime.ElapsedGameTime.TotalSeconds; #if WINDOWS MouseInput = Mouse.GetState(); //Keep the mouse within the screen Mouse.SetPosition(200, 200); #endif PreviousGamePadInput = GamePadInput; for (int i = 0; i < 4; i++) { GamePadInput = GamePad.GetState((PlayerIndex)i); if (GamePadInput.IsConnected) break; } // Allows the default game to exit on Xbox 360 and Windows if (KeyboardInput.IsKeyDown(Keys.Escape) || GamePadInput.Buttons.Back == ButtonState.Pressed) Exit(); #region Camera //Update the camera #if !WINDOWS Camera.Update(dt, KeyboardInput, GamePadInput); #else Camera.Update(dt, KeyboardInput, MouseInput, GamePadInput); #endif #endregion #region UI Toggles #if !WINDOWS if (WasButtonPressed(Buttons.Start)) { displayMenu = !displayMenu; } #else if (WasKeyPressed(Keys.F1)) { displayMenu = !displayMenu; } #endif if (WasKeyPressed(Keys.I)) { displayUI = !displayUI; } if (WasKeyPressed(Keys.J)) { displayConstraints = !displayConstraints; } if (WasKeyPressed(Keys.K)) { displayContacts = !displayContacts; } if (WasKeyPressed(Keys.U)) { displayBoundingBoxes = !displayBoundingBoxes; } if (WasKeyPressed(Keys.Y)) { displayEntities = !displayEntities; } if (WasKeyPressed(Keys.H)) { displaySimulationIslands = !displaySimulationIslands; } if (WasKeyPressed(Keys.G)) { ModelDrawer.IsWireframe = !ModelDrawer.IsWireframe; } #endregion #region Simulation Switcharoo #if !WINDOWS int switchTo = -2; if (WasButtonPressed(Buttons.DPadLeft)) switchTo = currentSimulationIndex - 1; else if (WasButtonPressed(Buttons.DPadRight)) switchTo = currentSimulationIndex + 1; if (switchTo != -2) { if (switchTo < 1) switchTo += demoTypes.Length; else if (switchTo > demoTypes.Length) switchTo = 1; SwitchSimulation(switchTo); } #else foreach (Keys key in KeyboardInput.GetPressedKeys()) { int code = key.GetHashCode(); if (code >= 48 && code <= 57) { int simNumber; if (code == 48) simNumber = 10; else simNumber = code - 48; if (KeyboardInput.IsKeyDown(Keys.LeftShift)) simNumber += 10; if (KeyboardInput.IsKeyDown(Keys.LeftControl)) simNumber += 20; if (simNumber <= demoTypes.Length) { currentSimulation.Space.ThreadManager.Dispose(); //While it would clean up by itself, this gets it done a little quicker. SwitchSimulation(simNumber); } } } #endif #endregion currentSimulation.Update(dt); if (displayConstraints) ConstraintDrawer.Update(); if (displayEntities) ModelDrawer.Update(); base.Update(gameTime); }
public void Update(GameTime gameTime) { ModelDrawer.Update(); }