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


C# Vector2.Length方法代码示例

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


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

示例1: Update

        /// <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();
        }
开发者ID:rossmas,项目名称:DualStickShooter,代码行数:34,代码来源:EvadingEnemy.cs

示例2: 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

示例3: Execute

        public override Vector2 Execute(Vector2 source, Vector2 target)
        {
            //return source;

            // Todo: Optimize, remove jitter and avoid walls.
            var circleCenter = velocity;
            circleCenter.Normalize();
            circleCenter = Vector2.Multiply(circleCenter, CircleDistance);

            var displacement = new Vector2(0, -1);
            displacement = Vector2.Multiply(displacement, CircleRadius);

            displacement.X = (float)Math.Cos(wanderAngle) * displacement.Length();
            displacement.Y = (float)Math.Sin(wanderAngle) * displacement.Length();

            wanderAngle += random.Next(0, 359) * AngleChange * 5;

            // steering aka wanderForce
            var steering = Vector2.Add(circleCenter, displacement);

            steering = steering.Truncate(MaxForce);
            steering = Vector2.Divide(steering, Mass);

            this.velocity = Vector2.Add(this.velocity, steering);
            this.velocity = this.velocity.Truncate(MaxVelocity);

            return source + this.velocity;
        }
开发者ID:scenex,项目名称:SpaceFighter,代码行数:28,代码来源:BehaviourStrategyWander.cs

示例4: ThumbMovement

        public static Vector2 ThumbMovement(Vector2 state)
        {
            float xb = state.X;
            float yb = state.Y;

            Vector2 direction = new Vector2(xb, yb);

            float deadZone = 0.3f;
            if (direction.Length() < deadZone)
            {
                direction = Vector2.Zero;
            }
            else
            {
                direction = direction.GetNormalized() * ((direction.Length() - deadZone) / (1 - deadZone));
            }

            //if (direction.Length() > 0.95f)
            //{
            //    float magnitude = 0.95f;
            //    direction.Normalize();
            //    direction.X = direction.X * magnitude;
            //    direction.Y = direction.Y * magnitude;
            //}
            return direction;
        }
开发者ID:njustesen,项目名称:local-rts,代码行数:26,代码来源:InputManager.cs

示例5: Update

 /// <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)
 {
     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();
     base.Update();
 }
开发者ID:rossmas,项目名称:DualStickShooter,代码行数:11,代码来源:ChasingEnemy.cs

示例6: GetNormal

 public static Vector2 GetNormal(Vector2 src)
 {
     Vector2 result = new Vector2();
     result.Y = src.X / src.Length();
     result.X = -src.Y /src.Length();
     return result;
 }
开发者ID:JackTheHack,项目名称:Vortex.Engine,代码行数:7,代码来源:VectorHelper.cs

示例7: Update

        public override void Update(GameTime gameTime)
        {
            Vector2 direction = new Vector2(0, 0);

            mFixture.Body.ResetDynamics();
            mFixture.Body.LinearVelocity = new Vector2(0, 0);
            mFixture.Body.Rotation = 0;

            UpdateInput();

            //base.Update(gameTime);
            if (mControllable)
            {
                direction = DetermineDesiredDirection();

                if (direction.Length() > .065f)
                {
                    mFixture.Body.LinearVelocity = (direction * mMaxSpeed);
                }

                if (direction.Length() > 0)
                {
                    direction.Normalize();
                    mDirection = direction;
                }
            }

            mPosition = new Vector2(mFixture.Body.Position.X, mFixture.Body.Position.Y); // converts Body.Position (meters) into pixels

            UpdateAnimation(gameTime, direction);
        }
开发者ID:Danathus,项目名称:ggj-resurrection-2012,代码行数:31,代码来源:DeadPlayer.cs

示例8: MaxLimit

 public static Vector2 MaxLimit(Vector2 vector, float limit)
 {
     if (vector.Length() > limit)
     {
         float factor = vector.Length() / limit;
         vector = vector / factor;
     }
     return vector;
 }
开发者ID:arjan9322,项目名称:AiprojectCsharp,代码行数:9,代码来源:VectorHelper.cs

示例9: AngleBetween

        public float AngleBetween(Vector2 a, Vector2 b)
        {
            float dotProd = Vector2.Dot(a, b);
            float lenProd = a.Length() * b.Length();
            float cos = dotProd / lenProd;

            a = new Vector2(a.Y, -a.X);
            dotProd = Vector2.Dot(a, b);
            lenProd = a.Length() * b.Length();
            float sin = dotProd / lenProd;

            return (float)Math.Atan2(sin, cos);
        }
开发者ID:martinvium,项目名称:ngj2012,代码行数:13,代码来源:BloodManager.cs

示例10: update

 public void update()
 {
     wallCollisionCheck();
     Velocity *= friction;
     if (Velocity.Length() < 0.1)  Velocity = Vector2.Zero;
     Position += Velocity;
 }
开发者ID:obcilion,项目名称:xnaKanonSpill,代码行数:7,代码来源:Ball.cs

示例11: 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

示例12: Arrow

        private Arrow(Vector2 position, Vector2 direction, Color color, float radius)
        {
            _vertices = new VertexPositionColor[6];

            var length = 3f * (float)Math.Sqrt(direction.Length());

            direction.Normalize();

            var arrowHeadSize = radius / 3f;
            var perpendicular = new Vector3(-direction.Y, direction.X, 0);
            var arrowBase = new Vector3(position + (direction * radius), 0);
            var arrowHead = new Vector3(position + (direction * (radius + length)), 0);
            var arrowPoint = new Vector3(position + (direction * (radius + length + arrowHeadSize)), 0);

            _vertices[0].Position = arrowBase;
            _vertices[1].Position = arrowHead;
            _vertices[2].Position = arrowHead - (perpendicular * arrowHeadSize);
            _vertices[3].Position = arrowPoint;
            _vertices[4].Position = arrowHead + (perpendicular * arrowHeadSize);
            _vertices[5].Position = arrowHead;

            for (var i = 1; i <=5; i++)
            {
                _vertices[i].Color = color;
            }
            _vertices[0].Color = Color.Transparent;
        }
开发者ID:DaveEmmerson,项目名称:SpaceWar2,代码行数:27,代码来源:Arrow.cs

示例13: Update

        public override void Update(GameTime gameTime)
        {
            
            enginePower = 3;


            delta = Target.Position - Position;
            delta.Normalize();

            delta *= enginePower;

            Rotation = (float)(Math.Atan2(delta.Y,delta.X));

            var currentSpeed = delta.Length();
            if (currentSpeed > MaxSpeed)
            {
                currentSpeed = MaxSpeed;
            }
            if (currentSpeed > 0)
            {
                currentSpeed -= Friction;
            }
            else currentSpeed = 0;
            delta *= currentSpeed;


            Move(delta);

            base.Update(gameTime);
        }
开发者ID:reubencummins,项目名称:CasualGamesAssignment,代码行数:30,代码来源:AutoShip.cs

示例14: Update

        public void Update(float dt)
        {
            if (m_Recrutable.Target == null)
            {
                m_PickedUp = false;
                return;
            }

            if (m_PickedUp == false)
            {
                m_PickedUp = true;
                m_GM.InfluenceRadius += 0.05f;
            }

            Vector3 playerPos = m_Recrutable.Target.GetWorldTranslation();

            Vector3 current = Owner.GetWorldTranslation();

            Vector3 toplayer3D = playerPos - current;
            Vector2 toplayer = new Vector2(toplayer3D.X, toplayer3D.Y);

            m_Joint.TargetAngle = (float)Math.Atan2((double)toplayer.Y, (double)toplayer.X) - (float)Math.Atan2(1, 0);

            if (toplayer.Length() < 0.6f)
            {
                m_Physics.LinearVelocity = new Vector2(0,0);
            }
            else
            {
                toplayer.Normalize();
                m_Physics.ApplyForce(m_Force * toplayer);
            }
        }
开发者ID:huardca,项目名称:jampack_xna,代码行数:33,代码来源:Manifestant.cs

示例15: Update

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            // Calculate the timeDelta I.e. How much time has elapsed since the last time this function was called
            // We use this in the updates
            float timeDelta = (float) gameTime.ElapsedGameTime.TotalSeconds;
            float speed = 100.0f;

            Vector2 acceleration = arrive() / mass;
            velocity = velocity + acceleration * timeDelta;
            if (velocity.Length() > maxSpeed)
            {
                velocity = Vector2.Normalize(velocity) * maxSpeed;
            }
            Position += velocity * timeDelta;
            if (velocity.Length() > 0)
            {
                Look = Vector2.Normalize(velocity);
            }

            Vector2 basis = new Vector2(0, -1);
            Rotation = (float) Math.Acos(Vector2.Dot(basis, Look));
            if (Look.X < 0)
            {
                Rotation *= -1;
            }
        }
开发者ID:skooter500,项目名称:Simple-2D-XNA-Steering-Demo,代码行数:28,代码来源:PlayerTank.cs


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