Example #1
0
        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
        }