本文整理汇总了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();
}
示例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();
}
}
示例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;
}
示例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;
}
示例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();
}
示例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;
}
示例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);
}
示例8: MaxLimit
public static Vector2 MaxLimit(Vector2 vector, float limit)
{
if (vector.Length() > limit)
{
float factor = vector.Length() / limit;
vector = vector / factor;
}
return vector;
}
示例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);
}
示例10: update
public void update()
{
wallCollisionCheck();
Velocity *= friction;
if (Velocity.Length() < 0.1) Velocity = Vector2.Zero;
Position += Velocity;
}
示例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;
}
示例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;
}
示例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);
}
示例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);
}
}
示例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;
}
}