【问题标题】:XNA: adding a force to a vectorXNA:向矢量添加力
【发布时间】:2010-08-25 09:07:37
【问题描述】:

我确实在 2d 平面上有 2 个点。一个已经有一个向量可以确定它将向哪个方向移动。

现在我想向这个现有的向量添加一个向量。所以他向另一点的方向加速。

说得更清楚一点,大约有 2 颗小行星在太空中飞行(只有 2d),引力应该使它们彼此靠近一点。

到目前为止我所做的是:

c = body.position - body2.position;
dist = c.Length();

acc = (body.masse * body2.masse) / (dist * dist);

xDist = body2.position.X - body.position.X;
yDist = body2.position.Y - body.position.Y;

direction = MathHelper.ToDegrees((float)(Math.Atan2((double)yDist, (double)xDist)));

body.velocity.Y = body.velocity.Y + (float)(Math.Sin(direction) * acc);
body.velocity.X = body.velocity.X + (float)(Math.Cos(direction) * acc);

在计算的方向完全关闭的那一刻。当然我只是犯了一个愚蠢的错误,但我不知道。

【问题讨论】:

    标签: c# vector xna


    【解决方案1】:

    您需要将您的方向角以 弧度 为单位传递给 Math.sin 和 Math.Cos(而不是像在您的 smaple 代码中那样以度数为单位)。

    另见: http://msdn.microsoft.com/en-us/library/system.math.sin.aspx

    角度 a 必须以弧度为单位。乘以 Math.PI/180 将度数转换为弧度。

    【讨论】:

    • 可能移除对ToDegrees的调用,而不是去乘以结果;)
    • 改变方向 = MathHelper.ToDegrees((float)(Math.Atan2((double)yDist, (double)xDist)));到方向 = (float)(Math.Atan2((double)yDist, (double)xDist));成功了。谢谢!
    • @ralph,你的算法不太正确。在计算 acc 的地方,你错过了两件事:引力常数和通过身体质量调节力。力必须除以质量才能获得加速度,试试这个以获得更好的准确性:acc = ((GravitationalConstant * (body.mass * body1.mass)) / (dist squared)) / body.mass;如果你想要 1.0,你可以忽略引力常数,但你不应该忽略它最后除以身体的质量。否则,就像在空中跳跃,地球上来迎接你的速度和你下来迎接它的速度一样快。
    【解决方案2】:

    我的力学和线性代数有点生疏,但我认为你应该能够在不诉诸三角学的情况下做到这一点。这些公式可能需要调整,我不确定我是否把 u 和 -u 弄混了。

    这里是伪代码

    T is whatever time period you're iterating over
    G is the gravitational constant
    
    body1 starts with  a velocity of v1
    body2 starts with  a velocity of v2
    
    c = body.position - body2.position
    
    c1 is a vector
    use the vector c to get a vector of length 1 in the direction of the force
    
    u = c1 / c.Length()
    
    body1 should have an acceleration vector of a1 =  G * body2mass/c.Length()^2 * (-u)
    
    body2 should have an acceleration vector of a2 = G * body1mass/c.Length()^2 * (u)
    
    body1 has a new velocity vector of v1 + a1/T
    
    body2 has a new velocity vector of v1 + a2/T
    

    冲洗并重复

    【讨论】:

    • 我稍后会研究那个。看起来更优雅一些。
    【解决方案3】:

    不完全确定您尝试做什么。为什么不能只使用 Vector2.Add(v1, v2)?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 2017-09-29
      相关资源
      最近更新 更多