protected override void Initialize() { // TODO: Add your initialization logic here graphics.PreferredBackBufferWidth = 480; graphics.PreferredBackBufferHeight = 800; cam = new ClsCamara(this, GraphicsDevice); eixos = new ClsEixos(GraphicsDevice, cam); terrain = new ClsTerreno(GraphicsDevice, Content, cam); //StartNewRound(); tanks.Add(new ClsTank(GraphicsDevice, Content, new Vector3(64, 30, 64))); base.Initialize(); }
public void Update(KeyboardState kb, ClsTerreno terreno, GameTime gameTime) { #region Movimento // Canhão if (kb.IsKeyDown(Keys.Up) && !kb.IsKeyDown(Keys.Down)) { if (this.cannonRotation >= -Math.PI / 4) { this.cannonRotation -= MathHelper.ToRadians(1f); } } else if (kb.IsKeyDown(Keys.Down) && !kb.IsKeyDown(Keys.Up)) { if (this.cannonRotation <= 0) { this.cannonRotation += MathHelper.ToRadians(1f); } } // turret if (kb.IsKeyDown(Keys.Right) && !kb.IsKeyDown(Keys.Left)) { this.turretRotation += MathHelper.ToRadians(1f); } else if (kb.IsKeyDown(Keys.Left) && !kb.IsKeyDown(Keys.Right)) { this.turretRotation -= MathHelper.ToRadians(1f); } // MOVER TANK if (kb.IsKeyDown(Keys.G) && !kb.IsKeyDown(Keys.J)) { steerRotation = MathHelper.ToRadians(10f); if (kb.IsKeyDown(Keys.Y)) { rotation.X += MathHelper.ToRadians(1f); } else if (kb.IsKeyDown(Keys.H)) { rotation.X -= MathHelper.ToRadians(1f); } } else if (kb.IsKeyDown(Keys.J) && !kb.IsKeyDown(Keys.G)) { steerRotation = MathHelper.ToRadians(-10f); if (kb.IsKeyDown(Keys.Y)) { rotation.X -= MathHelper.ToRadians(1f); } else if (kb.IsKeyDown(Keys.H)) { rotation.X += MathHelper.ToRadians(1f); } } else { steerRotation = MathHelper.ToRadians(0f); } // Mantem a rotação dentro de 2*pi (360º) if (rotation.X > 2 * MathHelper.Pi || rotation.X < -2 * MathHelper.Pi) { rotation.X = 0; } if (kb.IsKeyDown(Keys.Y)) { this.positionTank -= this.relativeForward * (float)gameTime.ElapsedGameTime.TotalSeconds * speed; this.wheelRotation += MathHelper.ToRadians(1f); } if (kb.IsKeyDown(Keys.H)) { this.positionTank += this.relativeForward * (float)gameTime.ElapsedGameTime.TotalSeconds * speed; this.wheelRotation -= MathHelper.ToRadians(1f); } #endregion #region Tank position calculations // trasnformar o relative forward e right relativeForward = Vector3.Transform(Vector3.Forward, Matrix.CreateRotationY(rotation.X)); relativeForward.Normalize(); relativeRight = Vector3.Transform(Vector3.Right, Matrix.CreateRotationY(rotation.X)); relativeRight.Normalize(); this.Up = terreno.GetNormalInterpolation(positionTank); this.Forward = Vector3.Cross(Up, relativeRight); this.Right = Vector3.Cross(relativeForward, Up); // calcular o vector right Right.Normalize(); transformMatrix = Matrix.CreateTranslation(positionTank); rotationMatrix = Matrix.CreateFromYawPitchRoll(yaw, pitch, 45f); // Rotação a dar ao tank rotationMatrix.Forward = Forward; rotationMatrix.Up = Up; rotationMatrix.Right = Right; positionTank.Y = terreno.Bilinear(positionTank).Y; // actualiza a altura do tank em relação ao terreno #endregion #region Apply transformations to the model bones // Applies a transformations to any bone (Root, Turret, Cannon, … model.Root.Transform = Matrix.CreateScale(scale) * rotationMatrix * transformMatrix; Matrix wheel = Matrix.CreateRotationX(wheelRotation); Matrix steer = Matrix.CreateRotationY(steerRotation); Matrix turret = Matrix.CreateRotationY(turretRotation); Matrix cannon = Matrix.CreateRotationX(cannonRotation); turretBone.Transform = turret * turretTransform; cannonBone.Transform = cannon * cannonTransform; leftBackWheel.Transform = wheel * leftBackWheelTransform; rightBackWheel.Transform = wheel * rightBackWheelTransform; leftFrontWheel.Transform = wheel * leftFrontWheelTransform; rightFrontWheel.Transform = wheel * rightFrontWheelTransform; leftSteer.Transform = steer * leftSteerTransform; rightSteer.Transform = steer * rightSteerTrasnform; // Appies transforms to bones in a cascade model.CopyAbsoluteBoneTransformsTo(boneTransforms); #endregion }
public void Update(KeyboardState kb, MouseState ms, ClsTerreno terreno) { if (kb.IsKeyDown(Keys.F1)) { cameraTag = SurfaceFollow; camChange = true; } else if (kb.IsKeyDown(Keys.F2)) { cameraTag = FreeCam; camChange = true; } else if (kb.IsKeyDown(Keys.F3)) { cameraTag = TankFollow; camChange = true; } switch (cameraTag) { case SurfaceFollow: #region Mouse Input if (ms.Position.X > previousMS.Position.X) { yaw -= MathHelper.ToRadians(1.0f); } if (ms.Position.X < previousMS.Position.X) { yaw += MathHelper.ToRadians(1.0f); } if (ms.Position.Y > previousMS.Position.Y) { pitch += MathHelper.ToRadians(1.0f); } if (ms.Position.Y < previousMS.Position.Y) { pitch -= MathHelper.ToRadians(1.0f); } previousMS = ms; #endregion #region Keyboard Input if (kb.IsKeyDown(Keys.D)) { this.yaw -= MathHelper.ToRadians(1.0f); } if (kb.IsKeyDown(Keys.A)) { this.yaw += MathHelper.ToRadians(1.0f); } if (kb.IsKeyDown(Keys.Up)) { this.pitch += MathHelper.ToRadians(1.0f); } if (kb.IsKeyDown(Keys.Down)) { this.pitch -= MathHelper.ToRadians(1.0f); } #endregion cameraPosition.Y = terreno.Bilinear(cameraPosition).Y + 5; camChange = false; break; case FreeCam: #region Mouse Input if (ms.Position.X > previousMS.Position.X) { yaw -= MathHelper.ToRadians(1.0f); } if (ms.Position.X < previousMS.Position.X) { yaw += MathHelper.ToRadians(1.0f); } if (ms.Position.Y > previousMS.Position.Y) { pitch += MathHelper.ToRadians(1.0f); } if (ms.Position.Y < previousMS.Position.Y) { pitch -= MathHelper.ToRadians(1.0f); } previousMS = ms; #endregion #region Keyboard Input if (kb.IsKeyDown(Keys.D)) { this.yaw -= MathHelper.ToRadians(1.0f); } if (kb.IsKeyDown(Keys.A)) { this.yaw += MathHelper.ToRadians(1.0f); } if (kb.IsKeyDown(Keys.Up)) { this.pitch += MathHelper.ToRadians(1.0f); } if (kb.IsKeyDown(Keys.Down)) { this.pitch -= MathHelper.ToRadians(1.0f); } if (kb.IsKeyDown(Keys.Right)) { this.roll += MathHelper.ToRadians(1.0f); } if (kb.IsKeyDown(Keys.Left)) { this.roll -= MathHelper.ToRadians(1.0f); } #endregion if (camChange == true) { cameraPosition = new Vector3(64, 30, 64); } camChange = false; break; case 3: break; } //cameraPosition.Y = terreno.Bilinear(cameraPosition).Y +5; Debug.WriteLine("CamX:" + cameraPosition.X + "CamZ:" + cameraPosition.Z); #region Movement if (kb.IsKeyDown(Keys.W)) { this.cameraPosition += this.cameraVelocity * direction; } if (kb.IsKeyDown(Keys.S)) { this.cameraPosition -= this.cameraVelocity * direction; } // calcula a matrix de rotação e direção rotationMatrix = Matrix.CreateFromYawPitchRoll(yaw, pitch, roll); direction = Vector3.Transform(directionBase, rotationMatrix); this.cameraLookAt = cameraPosition + direction; this.viewMatrix = Matrix.CreateLookAt(cameraPosition, cameraLookAt, Vector3.Up); #endregion // Colission detection v0.000000001 if (cameraPosition.X > 127) { cameraPosition.X = 126; } if (cameraPosition.X < 1) { cameraPosition.X = 1; } if (cameraPosition.Z > 127) { cameraPosition.Z = 126; } if (cameraPosition.Z < 1) { cameraPosition.Z = 1; } }