当前位置: 首页>>代码示例>>C#>>正文


C# Vector2.LengthSquared方法代码示例

本文整理汇总了C#中Microsoft.Xna.Framework.Vector2.LengthSquared方法的典型用法代码示例。如果您正苦于以下问题:C# Vector2.LengthSquared方法的具体用法?C# Vector2.LengthSquared怎么用?C# Vector2.LengthSquared使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.Xna.Framework.Vector2的用法示例。


在下文中一共展示了Vector2.LengthSquared方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Update

        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;
        }
开发者ID:weimingtom,项目名称:db-diver,代码行数:28,代码来源:Shoal.cs

示例2: Draw

        public void Draw(SpriteBatch spriteBatch, float playerForwardRadians, Vector3 enemyPos, ref Vector3 playerPos)
        {
            // The last parameter of the color determines how transparent the radar circle will be
            spriteBatch.Draw(RadarImage, RadarCenterPos, null, new Color(100, 100, 100, 150), 0.0f, RadarImageCenter, RadarScreenRadius / (RadarImage.Height * 0.5f), SpriteEffects.None, 0.0f);

            // If enemy is in range
            Vector2 diffVect = new Vector2(enemyPos.X - playerPos.X, enemyPos.Z - playerPos.Z);
                float distance = diffVect.LengthSquared();

                // Check if enemy is within RadarRange
                if (distance < RadarRangeSquared)
                {
                    // Scale the distance from world coords to radar coords
                    diffVect *= RadarScreenRadius / RadarRange;

                    // We rotate each point on the radar so that the player is always facing UP on the radar
                    diffVect = Vector2.Transform(diffVect, Matrix.CreateRotationZ(playerForwardRadians));

                    // Offset coords from radar's center
                    diffVect += RadarCenterPos;

                    // We scale each dot so that enemies that are at higher elevations have bigger dots, and enemies
                    // at lower elevations have smaller dots.
                    float scaleHeight = 1.0f - ((enemyPos.Y - playerPos.Y) / 200000.0f);

                    // Draw enemy dot on radar
                    spriteBatch.Draw(EnemyDotImage, diffVect, null, Color.White, 0.0f, new Vector2(0.0f, 0.0f), scaleHeight, SpriteEffects.None, 0.0f);
                }

            // Draw player's dot last
            spriteBatch.Draw(PlayerDotImage, RadarCenterPos, Color.White);
        }
开发者ID:MarcusKhoo,项目名称:FlightOfGlory,代码行数:32,代码来源:Radar.cs

示例3: intersects

        public Vector3 intersects(CollisionCylinder other)
        {
            float yDiff = this.position.Y - other.position.Y;
            float combinedHeight = this.Height + other.Height;
            float combinedRadius = this.Radius + other.Radius;

            //first check if height intersects
            if (Math.Abs(yDiff) < combinedHeight )
            {
                Vector2 xzDiff = new Vector2(this.position.X - other.position.X,this.position.Z - other.position.Z);
                if (xzDiff.LengthSquared() < combinedRadius * combinedRadius)
                {
                    float intersectY = combinedHeight - yDiff;
                    Vector2 intersectXZ = Vector2.Normalize(xzDiff) * (combinedRadius - xzDiff.Length());

                    if (intersectY * intersectY < intersectXZ.LengthSquared())
                    {
                        return  Vector3.Up * (combinedHeight - yDiff);
                    }
                    else
                    {
                        return new Vector3(intersectXZ.X,0f, intersectXZ.Y);
                    }
                }

            }
            return Vector3.Zero;
        }
开发者ID:Jemeyr,项目名称:ELTMC,代码行数:28,代码来源:CollisionCylinder.cs

示例4: createCircleText

        //Guess what I stole from StackOverflow
        public Texture2D createCircleText(GraphicsDevice graphics, int radius, Color circleColor)
        {
            Texture2D texture = new Texture2D(graphics, radius, radius);
            Color[] colorData = new Color[radius * radius];

            float diam = radius / 2f;
            float diamsq = diam * diam;

            for (int x = 0; x < radius; x++)
            {
                for (int y = 0; y < radius; y++)
                {
                    int index = x * radius + y;
                    Vector2 pos = new Vector2(x - diam, y - diam);
                    if (pos.LengthSquared() <= diamsq)
                    {
                        colorData[index] = circleColor;
                    }
                    else
                    {
                        colorData[index] = Color.Transparent;
                    }
                }
            }

            texture.SetData(colorData);
            return texture;
        }
开发者ID:TheGameDevelopmentClub,项目名称:Manic-Shooter,代码行数:29,代码来源:TextureManager.cs

示例5: GetCircle

        public static Shape GetCircle(GraphicsDevice device, int radius)
        {
            Shape texture = new Shape(device, radius, radius, EShapes.Circle);
            Color[] colorData = new Color[radius * radius];

            float diam = radius / 2f;
            float diamsq = diam * diam;

            for (int x = 0; x < radius; x++)
            {
                for (int y = 0; y < radius; y++)
                {
                    int index = x * radius + y;
                    Vector2 pos = new Vector2(x - diam, y - diam);
                    if (pos.LengthSquared() <= diamsq)
                    {
                        colorData[index] = Color.White;
                    }
                    else
                    {
                        colorData[index] = Color.Transparent;
                    }
                }
            }

            texture.SetData(colorData);
            return texture;
        }
开发者ID:FaceHunter,项目名称:SomeGame,代码行数:28,代码来源:Shapes.cs

示例6: correctOverlap

        public static void correctOverlap(Agent agent, List<Flockable> neighbors)
        {
            double distSq;
                double overlap;
                double newX, newY;

                foreach (Flockable other in neighbors)
                {
                    if (other is Agent == true)
                    {
                        Vector2 toOther = new Vector2((float)(other.getLocation().X - agent.getLocation().X), (float)(other.getLocation().Y - agent.getLocation().Y));
                        distSq = toOther.LengthSquared();
                        //distSq = (Math.Pow((agent.getLocation().X - other.getLocation().X), 2) + Math.Pow((agent.getLocation().Y - other.getLocation().Y), 2));
                        overlap = agent.getAgentRadiusSq() + other.getAgentRadiusSq() - distSq;
                        if (agent.Equals(other) == false && overlap >= 0)
                        {
                            double scale = overlap / Math.Sqrt(distSq);
                            Vector2 move = new Vector2((float)(toOther.X * scale), (float)(toOther.Y * scale));
                            //newX = agent.getLocation().X + (((other.getLocation().X - agent.getLocation().X) / Math.Sqrt(distSq)) * overlap);
                            //newY = agent.getLocation().Y + (((other.getLocation().Y - agent.getLocation().Y) / Math.Sqrt(distSq)) * overlap);
                            newX = agent.getLocation().X + move.X;
                            newY = agent.getLocation().Y + move.Y;
                            agent.setLocation(new DotNET.Point(newX, newY));
                        }
                    }
                } // end foreach
        }
开发者ID:EveryUserNameIsTaken,项目名称:Null_and_Boid,代码行数:27,代码来源:FlockingEngine.cs

示例7: GetSweptAngle

        public static float GetSweptAngle(Vector2 start, Vector2 stop)
        {
            float lengthSquared = start.LengthSquared();
            if (lengthSquared == 0.0f)
                throw new ArgumentException("start");

            if (Math.Abs(lengthSquared - 1.0f) > 0.00005)
                start.Normalize();

            lengthSquared = stop.LengthSquared();
            if (lengthSquared == 0.0f)
                throw new ArgumentException("stop");

            if (Math.Abs(lengthSquared - 1.0f) > 0.00005)
                stop.Normalize();

            float dot = Vector2.Dot(start, stop);
            Vector3 start3D = new Vector3(start, 0.0f);
            Vector3 stop3D = new Vector3(stop, 0.0f);
            Vector3 cross = Vector3.Cross(start3D, stop3D);
            float theta;
            if (cross.Z == 0.0f)
            {
                if (dot < 0.0f)
                    return MathHelper.Pi;
                else
                    return 0.0f;
            }
            theta = (float)(Math.Acos(MathHelper.Clamp(dot, -1.0f, 1.0f)));

            return Math.Sign(cross.Z) == 1 ? theta : -theta;
        }
开发者ID:Tengato,项目名称:Mechadrone1,代码行数:32,代码来源:SpaceUtils.cs

示例8: GenerateContact

        public override GraphicsToolkit.Physics._3D.Contact3D GenerateContact(RigidBody3D rb, float dt)
        {
            if (rb as SphereBody != null)
            {
                SphereBody c = rb as SphereBody; //sphere that this body is colliding with
                Vector3 pa = c.Pos; //point on this body closest to the sphere

                Vector2 XYVec = new Vector2(pa.X - 0.5f, pa.Y - 0.5f);
                if (XYVec.LengthSquared() < 1) //if inside bounding box but outside actual block
                {
                    pa.X = ((pa.X - 0.5f) / XYVec.Length()) + 0.5f;
                    pa.Y = ((pa.Y - 0.5f) / XYVec.Length()) + 0.5f;
                }

                pa.X = MathHelper.Clamp(pa.X, -0.5f, 0.5f);
                pa.Y = MathHelper.Clamp(pa.Y, -0.5f, 0.5f);
                pa.Z = MathHelper.Clamp(pa.Z, -0.5f, 0.5f);

                Vector3 normal = rb.Pos - pa;
                float normLen = normal.Length();
                float dist = normLen - c.Radius; //distance from block to sphere
                normal /= normLen; //normalize normal
                Vector3 pb = rb.Pos - normal * c.Radius; //closest point on sphere

                return new Contact3D(normal, dist, this, rb, pa, pb);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
开发者ID:bschwind,项目名称:Sky-Slider,代码行数:31,代码来源:ConcaveRampBody.cs

示例9: createCircle

        static public Texture2D createCircle(GraphicsDevice graphics, int durchmesser)
        {
            if (durchmesser == 0)
                durchmesser = 1;
            Texture2D texture = new Texture2D(graphics, durchmesser, durchmesser);
            Color[] colorData = new Color[durchmesser * durchmesser];

            float diam = durchmesser / 2;
            float diamsq = diam * diam;

            for (int x = 0; x < durchmesser; x++)
            {
                for (int y = 0; y < durchmesser; y++)
                {
                    int index = x * durchmesser + y;
                    Vector2 pos = new Vector2(x - diam, y - diam);
                    if (pos.LengthSquared() <= diamsq)
                    {
                        colorData[index] = Color.White;
                    }
                    else
                    {
                        colorData[index] = Color.Transparent;
                    }
                }
            }

            texture.SetData(colorData);
            return texture;
        }
开发者ID:jrichter42,项目名称:TVR_DevelopmentRelease_v1.0,代码行数:30,代码来源:Circle.cs

示例10: init

        // Methods
        public static void init(GraphicsDevice graphDev, SpriteBatch spriteBatch)
        {
            // init pointTexture
            pointTexture = new Texture2D(graphDev, 1, 1, false, SurfaceFormat.Color);
            pointTexture.SetData<Color>(new Color[] { defaultColor });

            // init circleTexture
            circleTexture = new Texture2D(graphDev, circleRadius, circleRadius);
            Color[] colorData = new Color[circleRadius * circleRadius];

            float diam = circleRadius / 2f;
            float diamsq = diam * diam;

            for (int x = 0; x < circleRadius; x++)
            {
                for (int y = 0; y < circleRadius; y++)
                {
                    int index = x * circleRadius + y;
                    Vector2 pos = new Vector2(x - diam, y - diam);
                    if (pos.LengthSquared() <= diamsq)
                    {
                        colorData[index] = defaultColor;
                    }
                    else
                    {
                        colorData[index] = Color.Transparent;
                    }
                }
            }
            circleTexture.SetData(colorData);

            // spriteBatch reference
            sb = spriteBatch;
        }
开发者ID:GennaroC,项目名称:CSharp-TheNatureOfCode,代码行数:35,代码来源:drawing.cs

示例11: Walk

 public void Walk(Vector2 direction)
 {
     if(direction.LengthSquared() > 0)
     {
         direction.Normalize();
         body.Move(_walkSpeed * direction);
     }
 }
开发者ID:axis7818,项目名称:AxisEngineTestBed,代码行数:8,代码来源:SmileyWalkDude.cs

示例12: ProjectVector2D

 public static void ProjectVector2D(ref Vector2 vec, ref Vector2 projectOn, out Vector2 result)
 {
     float dp;
     Vector2.Dot(ref vec, ref projectOn, out dp);
     float oneOnLenSqr = 1.0f / projectOn.LengthSquared();
     result = new Vector2(
                 dp * oneOnLenSqr * projectOn.X,
                 dp * oneOnLenSqr * projectOn.Y);
 }
开发者ID:gnomicstudios,项目名称:GGJ13,代码行数:9,代码来源:VectorUtils.cs

示例13: Contains

        /// <summary> 
        /// Determines if a circle contains a Point
        /// </summary> 
        /// <returns>True if the circle and Point overlap. False otherwise.</returns> 
        public bool Contains(Point p)
        {
            this.v = new Vector2(p.X,p.Y);

            this.direction = Center - v;
            this.distanceSquared = direction.LengthSquared();

            return ((distanceSquared > 0) && (distanceSquared < Radius * Radius));
        }
开发者ID:Cyral,项目名称:Cyral.Extensions,代码行数:13,代码来源:Circle.cs

示例14: Intersects

        /// <summary> 
        /// Determines if a circle intersects a rectangle. 
        /// </summary> 
        /// <returns>True if the circle and rectangle overlap. False otherwise.</returns> 
        public bool Intersects(Rectangle rectangle)
        {
            this.v = new Vector2(MathHelper.Clamp(Center.X, rectangle.Left, rectangle.Right),
                                    MathHelper.Clamp(Center.Y, rectangle.Top, rectangle.Bottom));

            this.direction = Center - v;
            this.distanceSquared = direction.LengthSquared();

            return ((distanceSquared > 0) && (distanceSquared < Radius * Radius));
        }
开发者ID:Stran6,项目名称:TankGame,代码行数:14,代码来源:Circle.cs

示例15: DrawDot

 public void DrawDot(SpriteBatch spriteBatch, Vector2 locationDelta, Color color)
 {
     if (locationDelta.LengthSquared() < detectionRadius * detectionRadius)
     {
         spriteBatch.Draw(dotTexture,
                 new Rectangle(
                     rect.Center.X + (int)(locationDelta.X * 0.88f * textureRadius / detectionRadius),
                     rect.Center.Y + (int)(locationDelta.Y * 0.88f * textureRadius / detectionRadius),
                     (int)(rect.Width * 0.05f), (int)(rect.Height * 0.05f)), color);
     }
 }
开发者ID:zanagi,项目名称:MelSpaceHunter,代码行数:11,代码来源:Radar.cs


注:本文中的Microsoft.Xna.Framework.Vector2.LengthSquared方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。