【发布时间】:2014-09-03 15:35:32
【问题描述】:
我有一种方法可以让一个精灵(舰队)围绕另一个精灵(行星)运行。问题是,随着时间的推移,舰队会移动得更远(非常缓慢,除非你让轨道运行几分钟,否则几乎不会被注意到)。
public void Orbit()
{
// Planet's position - fleet position
Vector2 distanceToDestination = currentLocation.Position - position;
// Calculate fleet's rotation ( - right angle so fleet rotates clockwise)
rotation = MathHelper.WrapAngle((float)Math.Atan2(distanceToDestination.Y, distanceToDestination.X)) - Helpers.RightAngle;
// Get the direction that the fleet is facing
direction = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation));
// Fleet position, thrust is set to 0.4f
position = Vector2.Add(position, direction * thrust);
// Fleet rectangle
rect = new Rectangle((int)position.X, (int)position.Y, fleetTexture.Width, fleetTexture.Height);
}
public static class Helpers
{
public const float RightAngle = (float)Math.PI / 2;
}
如果社区能指出为什么我的舰队没有保持一致的轨道,我将不胜感激,这正是我想要实现的!非常感谢。
【问题讨论】:
-
感谢两位出色的回答。 @dbc - 我选择了您的答案,因为我使用了您发布的方法的变体。再次感谢。