public Tank(GraphicsDevice device, ContentManager content, ChooseTank tank) { wordlMatrix = Matrix.Identity; r = Matrix.Identity; direction = new Vector3(0f, 0f, -1f); bullets = new List <Bullet>(); tankModel = content.Load <Model>("tank"); scale = 0.005f; if (tank == ChooseTank.tank) { position = new Vector3(15f, -10.0f, 40f); } if (tank == ChooseTank.enemyTank) { position = new Vector3(20f, -10f, 40f); } effect = new BasicEffect(device); // Calcula a aspectRatio, a view matrix e a projeção aspectRatio = (float)device.Viewport.Width / device.Viewport.Height; bsphere = new BoundingSphere(position, 2); // Lê os bones leftBackWheelBone = tankModel.Bones["l_back_wheel_geo"]; rightBackWheelBone = tankModel.Bones["r_back_wheel_geo"]; leftFrontWheelBone = tankModel.Bones["l_front_wheel_geo"]; rightFrontWheelBone = tankModel.Bones["r_front_wheel_geo"]; leftSteerBone = tankModel.Bones["l_steer_geo"]; rightSteerBone = tankModel.Bones["r_steer_geo"]; turretBone = tankModel.Bones["turret_geo"]; cannonBone = tankModel.Bones["canon_geo"]; // Lê as transformações iniciais dos bones leftBackWheelTransform = leftBackWheelBone.Transform; rightBackWheelTransform = rightBackWheelBone.Transform; leftFrontWheelTransform = leftFrontWheelBone.Transform; rightFrontWheelTransform = rightFrontWheelBone.Transform; leftSteerTransform = leftSteerBone.Transform; rightSteerTransform = rightSteerBone.Transform; turretTransform = turretBone.Transform; cannonTransform = cannonBone.Transform; // cria o array que armazenará as transformações em cascata dos bones boneTransforms = new Matrix[tankModel.Bones.Count]; }
public void Update(Field field, ChooseTank tank, Bullet bullet, GameTime gametime) { //posição inicial da direção direction = new Vector3(0f, 0f, -1f); float turnAngle = MathHelper.ToRadians(10f); Vector3 positionBack = position; KeyboardState keys = Keyboard.GetState(); //Tank if (tank == ChooseTank.tank) { //movimento do tank e respetiva rotação das rodas if (keys.IsKeyDown(Keys.A)) { yaw += turnAngle * speed; if (steerRotationValue < 0.5f) { steerRotationValue += 0.1f; } } if (keys.IsKeyDown(Keys.D)) { yaw -= turnAngle * speed; if (steerRotationValue > -0.5f) { steerRotationValue -= 0.1f; } } if (keys.IsKeyUp(Keys.A) && keys.IsKeyUp(Keys.D)) { steerRotationValue = 0f; } //definição da rotationMatrix através do yaw e do pitch rotationMatrix = Matrix.CreateFromYawPitchRoll(yaw, pitch, 0); //transformação da direção através da rotationMatrix direction = Vector3.Transform(direction, rotationMatrix); if (keys.IsKeyDown(Keys.W)) { position -= direction * speed; wheelRotationValue += 0.2f; } if (keys.IsKeyDown(Keys.S)) { position += direction * speed; wheelRotationValue -= 0.2f; } //movimento da torre e do canhão if (keys.IsKeyDown(Keys.Up)) { if (cannonRotationValue > -90f) { cannonRotationValue -= 0.8f; } cannonBone.Transform = Matrix.CreateRotationX(MathHelper.ToRadians(cannonRotationValue)) * cannonTransform; } if (keys.IsKeyDown(Keys.Down)) { if (cannonRotationValue < 2.5f) { cannonRotationValue += 0.8f; } cannonBone.Transform = Matrix.CreateRotationX(MathHelper.ToRadians(cannonRotationValue)) * cannonTransform; } if (keys.IsKeyDown(Keys.Left)) { turretRotationValue += 0.8f; turretBone.Transform = Matrix.CreateRotationY(MathHelper.ToRadians(turretRotationValue)) * turretTransform; } if (keys.IsKeyDown(Keys.Right)) { turretRotationValue -= 0.8f; turretBone.Transform = Matrix.CreateRotationY(MathHelper.ToRadians(turretRotationValue)) * turretTransform; } //Shoot if ((keys.IsKeyDown(Keys.Space) && (bullet.isShooting == false))) { Shoot(gametime, bullet); } //Limitar o movimento aos limites do terreno if (position.X - 1 < 0) { position = positionBack; } if (position.Z - 1 < 0) { position = positionBack; } if (position.X + 2f > field.width) { position = positionBack; } if (position.Z + 2f > field.height) { position = positionBack; } position.Y = field.SurfaceFollow(position) + 0.15f; //chamada da função NormalFollow n = field.NormalFollow(position); right = Vector3.Cross(direction, n); d = Vector3.Cross(n, right); r = Matrix.Identity; r.Forward = d; r.Up = n; r.Right = right; } }