C# 二维轨道随时间增加

C# 二维轨道随时间增加,c#,math,.net-4.0,xna,C#,Math,.net 4.0,Xna,我有一个方法让一个精灵(舰队)绕着另一个精灵(行星)运行。问题是,随着时间的推移,舰队移动得越来越远(非常缓慢,除非你让轨道运行几分钟,否则几乎看不到) 如果社区能够指出为什么我的舰队没有保持一致的轨道,我将不胜感激,这正是我想要实现的!非常感谢。如果我理解你在做什么,你正在尝试使用一种类似于汽车绕着一个圆圈行驶的模型来模拟轨道物理(也就是说,你没有建模垂直于当前速度和速度的重力加速度)。相反,您正在这样做: 稍微垂直于半径圆的中心移动r 重新定向,使“汽车”仍然垂直于圆心 重复一遍 现在,在每

我有一个方法让一个精灵(舰队)绕着另一个精灵(行星)运行。问题是,随着时间的推移,舰队移动得越来越远(非常缓慢,除非你让轨道运行几分钟,否则几乎看不到)


如果社区能够指出为什么我的舰队没有保持一致的轨道,我将不胜感激,这正是我想要实现的!非常感谢。

如果我理解你在做什么,你正在尝试使用一种类似于汽车绕着一个圆圈行驶的模型来模拟轨道物理(也就是说,你没有建模垂直于当前速度和速度的重力加速度)。相反,您正在这样做:

  • 稍微垂直于半径圆的中心移动
    r
  • 重新定向,使“汽车”仍然垂直于圆心
  • 重复一遍
  • 现在,在每一步之后,你的物体离圆心有多远?根据毕达哥拉斯定理,它是
    sqrt(r*r+d*d)
    ,它将略大于
    r
    。对
    d
    使用非常大的值作为强调:

    那应该能解释你的想法

    顺便说一句,您不需要使用trig函数来构造与distanceToDestination垂直的方向。简单地将其与±Z向量交叉就容易多了,结果将是(在2d中)
    ±(-distanceToDestination.Y,distanceToDestination.X)

    更新:既然你不是真的在模拟轨道物理,为什么不通过分析来解决这个问题呢

        public static Vector2 Orbit(Vector2 center, Vector2 startPos, float speed, float time)
        {
            // positive speed means CCW around the center, negative means CW.
            var radiusVec = (startPos - center);
            var radius = radiusVec.Length();
            var angularVelocity = speed / radius; // Add check for divide by zero
            Vector2 perpendicularVec = new Vector2(-radiusVec.Y, radiusVec.X);
            return center + (float)Math.Cos(time * angularVelocity) * radiusVec + (float)Math.Sin(time * angularVelocity) * perpendicularVec;
        }
    

    如果我理解你在做什么,你正在尝试用一种类似于汽车绕着一个圆圈行驶的模型来模拟轨道物理(也就是说,你不是在模拟垂直于当前速度和速度的重力加速度)。相反,您正在这样做:

  • 稍微垂直于半径圆的中心移动
    r
  • 重新定向,使“汽车”仍然垂直于圆心
  • 重复一遍
  • 现在,在每一步之后,你的物体离圆心有多远?根据毕达哥拉斯定理,它是
    sqrt(r*r+d*d)
    ,它将略大于
    r
    。对
    d
    使用非常大的值作为强调:

    那应该能解释你的想法

    顺便说一句,您不需要使用trig函数来构造与distanceToDestination垂直的方向。简单地将其与±Z向量交叉就容易多了,结果将是(在2d中)
    ±(-distanceToDestination.Y,distanceToDestination.X)

    更新:既然你不是真的在模拟轨道物理,为什么不通过分析来解决这个问题呢

        public static Vector2 Orbit(Vector2 center, Vector2 startPos, float speed, float time)
        {
            // positive speed means CCW around the center, negative means CW.
            var radiusVec = (startPos - center);
            var radius = radiusVec.Length();
            var angularVelocity = speed / radius; // Add check for divide by zero
            Vector2 perpendicularVec = new Vector2(-radiusVec.Y, radiusVec.X);
            return center + (float)Math.Cos(time * angularVelocity) * radiusVec + (float)Math.Sin(time * angularVelocity) * perpendicularVec;
        }
    

    如果我理解你在做什么,你正在尝试用一种类似于汽车绕着一个圆圈行驶的模型来模拟轨道物理(也就是说,你不是在模拟垂直于当前速度和速度的重力加速度)。相反,您正在这样做:

  • 稍微垂直于半径圆的中心移动
    r
  • 重新定向,使“汽车”仍然垂直于圆心
  • 重复一遍
  • 现在,在每一步之后,你的物体离圆心有多远?根据毕达哥拉斯定理,它是
    sqrt(r*r+d*d)
    ,它将略大于
    r
    。对
    d
    使用非常大的值作为强调:

    那应该能解释你的想法

    顺便说一句,您不需要使用trig函数来构造与distanceToDestination垂直的方向。简单地将其与±Z向量交叉就容易多了,结果将是(在2d中)
    ±(-distanceToDestination.Y,distanceToDestination.X)

    更新:既然你不是真的在模拟轨道物理,为什么不通过分析来解决这个问题呢

        public static Vector2 Orbit(Vector2 center, Vector2 startPos, float speed, float time)
        {
            // positive speed means CCW around the center, negative means CW.
            var radiusVec = (startPos - center);
            var radius = radiusVec.Length();
            var angularVelocity = speed / radius; // Add check for divide by zero
            Vector2 perpendicularVec = new Vector2(-radiusVec.Y, radiusVec.X);
            return center + (float)Math.Cos(time * angularVelocity) * radiusVec + (float)Math.Sin(time * angularVelocity) * perpendicularVec;
        }
    

    如果我理解你在做什么,你正在尝试用一种类似于汽车绕着一个圆圈行驶的模型来模拟轨道物理(也就是说,你不是在模拟垂直于当前速度和速度的重力加速度)。相反,您正在这样做:

  • 稍微垂直于半径圆的中心移动
    r
  • 重新定向,使“汽车”仍然垂直于圆心
  • 重复一遍
  • 现在,在每一步之后,你的物体离圆心有多远?根据毕达哥拉斯定理,它是
    sqrt(r*r+d*d)
    ,它将略大于
    r
    。对
    d
    使用非常大的值作为强调:

    那应该能解释你的想法

    顺便说一句,您不需要使用trig函数来构造与distanceToDestination垂直的方向。简单地将其与±Z向量交叉就容易多了,结果将是(在2d中)
    ±(-distanceToDestination.Y,distanceToDestination.X)

    更新:既然你不是真的在模拟轨道物理,为什么不通过分析来解决这个问题呢

        public static Vector2 Orbit(Vector2 center, Vector2 startPos, float speed, float time)
        {
            // positive speed means CCW around the center, negative means CW.
            var radiusVec = (startPos - center);
            var radius = radiusVec.Length();
            var angularVelocity = speed / radius; // Add check for divide by zero
            Vector2 perpendicularVec = new Vector2(-radiusVec.Y, radiusVec.X);
            return center + (float)Math.Cos(time * angularVelocity) * radiusVec + (float)Math.Sin(time * angularVelocity) * perpendicularVec;
        }
    

    这是您正在计算的:

    为了得到轨道上的下一个点,你必须保持向量的长度

    因此,您的计算应该是:

    // 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));
    rotation -= Helpers.RightAngle;
    
    // Get the direction that the fleet is facing
    direction = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation));
    direction *= distanceToDestination.Length;
    
    position = currentLocation.Position - direction;
    

    这是您正在计算的:

    为了得到轨道上的下一个点,你必须保持向量的长度

    因此,您的计算应该是:

    // 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));
    rotation -= Helpers.RightAngle;
    
    // Get the direction that the fleet is facing
    direction = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation));
    direction *= distanceToDestination.Length;
    
    position = currentLocation.Position - direction;
    

    这是您正在计算的:

    为了得到轨道上的下一个点,你必须保持向量的长度

    因此,您的计算应该是:

    // 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));
    rotation -= Helpers.RightAngle;
    
    // Get the direction that the fleet is facing
    direction = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation));
    direction *= distanceToDestination.Length;
    
    position = currentLocation.Position - direction;
    

    这是您正在计算的:

    为了得到轨道上的下一个点,你必须保持向量的长度

    因此,您的计算应该是:

    // 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));
    rotation -= Helpers.RightAngle;
    
    // Get the direction that the fleet is facing
    direction = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation));
    direction *= distanceToDestination.Length;
    
    position = currentLocation.Position - direction;
    

    谢谢你们两位出色的回答@dbc-我选择了您的答案,因为我使用了您使用的方法的变体