我已經得到了通過旋轉移動,其旋轉是不斷變化的,但我也需要制定出如果目標是到它的左邊或右邊和玩家精靈不在前後旋轉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