Skip to main content
 首页 » 编程设计

c#之XNA:向矢量添加力

2024年02月24日28findumars

我在二维平面上确实有 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); 

瞬间计算出的方向完全不对。我当然犯了一个愚蠢的错误,但我不知道。

请您参考如下方法:

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

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

The angle, a, must be in radians. Multiply by Math.PI/180 to convert degrees to radians.