Microsoft.Xna.Framework.Vector2.Length C# (CSharp) Method

Length() public method

Calculates the length of the vector.
public Length ( ) : float
return float
        public float Length()
        {
            float num = this.X * this.X + this.Y * this.Y;
            return (float)Math.Sqrt((double)num);
        }
        /// <summary>Calculates the length of the vector squared.</summary>

Usage Example

コード例 #1
0
        /// <summary>
        /// Update to chase the player.
        /// </summary>
        /// <param name="playerPos">The position of player. Basically the direction it's heading.</param>
        public void Update(Vector2 playerPos, GameTime gameTime)
        {
            distance = new Vector2(playerPos.X - position.X, playerPos.Y - position.Y);
            speed.X = (playerPos.X - position.X) * speedLength / distance.Length();
            speed.Y = (playerPos.Y - position.Y) * speedLength / distance.Length();

            timeSinceLastSwitch += gameTime.ElapsedGameTime.Milliseconds;
            if (timeSinceLastSwitch > millisecondPerSwitch)
            {
                timeSinceLastSwitch -= millisecondPerSwitch;
                isChasing = !isChasing;
            }

            if (isChasing)
            {
                speed *= 1;     // chase
            }
            else
            {
                speed *= -0.5f;       // evade
            }
            // Prevent EvadingEnemies from moving out of window
            if (position.X < 0) position.X = 0;
            if (position.Y < 0) position.Y = 0;
            if (position.X > windowWidth)
                position.X = windowWidth;
            if (position.Y > windowHeight)
                position.Y = windowHeight;
            base.Update();
        }
All Usage Examples Of Microsoft.Xna.Framework.Vector2::Length