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

LengthSquared() public method

Calculates the length of the vector squared.
public LengthSquared ( ) : float
return float
        public float LengthSquared()
        {
            return this.X * this.X + this.Y * this.Y;
        }
        /// <summary>Calculates the distance between two vectors.</summary>

Usage Example

コード例 #1
0
ファイル: Shoal.cs プロジェクト: weimingtom/db-diver
        public override void Update(State s, Room room)
        {
            base.Update(s, room);
            Vector2 direction = new Vector2((float)((TargetX - x) * (DiverGame.Random.NextDouble() * 0.2f + 0.8f)),
                                            (float)((TargetY - y) * (DiverGame.Random.NextDouble() * 0.2f + 0.8f)));

            if(direction.LengthSquared() > 0)
                direction.Normalize();

            speedX += direction.X * 0.007f;
            speedY += direction.Y * 0.007f;
            speedX *= 0.999f;
            speedY *= 0.999f;

            float speed = (float)Math.Sqrt(speedX * speedX + speedY * speedY);
            animationGridFrame += speed * 0.25f + 0.03f;

            x += speedX;
            y += speedY;
            X = (int)x;
            Y = (int)y;

            float desiredRot = (float)Math.Atan2(speedX, -speedY) - (float)Math.PI / 2f;
            float rotDiff = desiredRot - rotation;
            while (rotDiff > MathHelper.Pi) rotDiff -= MathHelper.TwoPi;
            while (rotDiff < -MathHelper.Pi) rotDiff += MathHelper.TwoPi;
            rotation += rotDiff * 0.1f;
        }
All Usage Examples Of Microsoft.Xna.Framework.Vector2::LengthSquared