我已经得到了通过旋转移动,其旋转是不断变化的,但我也需要制定出如果目标是到它的左边或右边和玩家精灵不在前后旋转45度内。制订一个目标的方位旋转精灵
我写这个代码,我认为应该工作,但拿起一边,稍另一方面,它似乎只是偶尔。
public void GrappleCheck(AsteroidSprite target)
{
float targetTragectory = (float)Math.Atan2(Position.Y - target.Position.Y, Position.X - target.Position.X);
if (targetTragectory < 0)
targetTragectory += (float)(Math.PI * 2);
if (Rotation < 0)
Rotation += (float)(Math.PI * 2);
if ((targetTragectory > Rotation + (float)(MathHelper.PiOver4/2)) && (targetTragectory < Rotation + (float)(Math.PI - (MathHelper.PiOver4/2))))
{
target.Distance = Vector2.Distance(Position, target.Position);
if (RightTarget != null)
{
if (RightTarget.Distance > target.Distance)
{
RightTarget.isTarget = false;
RightTarget = target;
RightTarget.ColorTint = Color.Blue;
RightTarget.isTarget = true;
}
}
else
{
RightTarget = target;
RightTarget.ColorTint = Color.Blue;
RightTarget.isTarget = true;
}
}
else if ((targetTragectory < Rotation - (float)(MathHelper.PiOver4/2)) && (targetTragectory > Rotation - (float)(Math.PI - (MathHelper.PiOver4/2))))
{
target.Distance = Vector2.Distance(Position, target.Position);
if (LeftTarget != null)
{
if (LeftTarget.Distance > target.Distance)
{
LeftTarget.isTarget = false;
LeftTarget = target;
LeftTarget.ColorTint = Color.Red;
LeftTarget.isTarget = true;
}
}
else
{
LeftTarget = target;
LeftTarget.ColorTint = Color.Red;
LeftTarget.isTarget = true;
}
}
else
{
target.isTarget = false;
}
if (controlInput.IsHeld(Keys.X))
{
Speed = Speed;
}
我工作出了问题,我的球员是旋转0 - 2 * PI,其中的“targetTragectory”旋转(顺时针)0 - PI然后-PI - 0,但没有解决如何解决这个问题 – DeviousSquire 2013-02-10 19:49:04