protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } float Elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; cannonCooldown = Math.Max(0, cannonCooldown - Elapsed); ProcessInput(Elapsed); UpdateViewMatrix(); float height; Vector3 temp; LinkedList <Shell> delete = new LinkedList <Shell>(); foreach (Shell shell in shells) { shell.Update(Elapsed); heightMapInfo.GetHeightAndNormal(shell.position, out height, out temp); if (shell.position.Y <= height) { delete.AddLast(shell); } } foreach (Shell shell in delete) { shells.Remove(shell); } base.Update(gameTime); }
/// <summary> /// This function is called when the game is Updating in response to user input. /// It'll move the tank around the heightmap, and update all of the tank's /// necessary state. /// </summary> public void Update(float elapsed, float turnAmount, Vector3 movement, HeightMapInfo heightMapInfo, Vector2 turretVector) { facingDirection += turnAmount * TankTurnSpeed * elapsed; // next, we'll create a rotation matrix from the direction the tank is // facing, and use it to transform the vector. Vector3 velocity = Vector3.Transform(movement, orientation); velocity *= TankVelocity; turretRotationValue = turretVector.X; cannonRotationValue = turretVector.Y; // Now we know how much the user wants to move. We'll construct a temporary // vector, newPosition, which will represent where the user wants to go. If // that value is on the heightmap, we'll allow the move. Vector3 newPosition = position + velocity * elapsed; if (heightMapInfo.IsOnHeightmap(newPosition)) { // now that we know we're on the heightmap, we need to know the correct // height and normal at this position. heightMapInfo.GetHeightAndNormal(newPosition, out newPosition.Y, out normal); // As discussed in the doc, we'll use the normal of the heightmap // and our desired forward direction to recalculate our orientation // matrix. It's important to normalize, as well. // now we need to roll the tank's wheels "forward." to do this, we'll // calculate how far they have rolled, and from there calculate how much // they must have rotated. float distanceMoved = Vector3.Distance(position, newPosition); float theta = distanceMoved / TankWheelRadius; int rollDirection = movement.Z > 0 ? 1 : -1; wheelRotationValue += theta * rollDirection; // once we've finished all computations, we can set our position to the // new position that we calculated. position = newPosition; } orientation = Matrix.CreateRotationY(FacingDirection); orientation.Up = normal; orientation.Right = Vector3.Cross(orientation.Forward, orientation.Up); orientation.Right = Vector3.Normalize(orientation.Right); orientation.Forward = Vector3.Cross(orientation.Up, orientation.Right); orientation.Forward = Vector3.Normalize(orientation.Forward); }