有没有人有实现轨道力学的例子(最好是在 XNA 中)?我当前使用的代码如下,但执行时“感觉不对”。这个物体只是稍微向行星弯曲,无论我如何调整变量,我都无法让它进入轨道,甚至是部分轨道。
shot.Position += shot.Velocity;
foreach (Sprite planet in planets)
{
Vector2 directionToPlanet = (planet.Position - shot.Position);
directionToPlanet.Normalize();
float distance = Vector2.DistanceSquared(shot.Position, planet.Position);
float gPull = (float)(planet.gravityStrength * (planet.Mass * shot.Mass) / distance) + planet.gravityField;
shot.Position += new Vector2(directionToPlanet.X * gPull, directionToPlanet.Y * gPull);
}
编辑 将 Mendelt 的回答标记为正确,指出我需要更新速度,而不是位置。我还需要将 gPull 的计算更改为
float gPull = shot.Mass * planet.Mass / distanceSqr * planet.gStr;
最佳答案
在最后一行中,您正在更新镜头的位置。您应该更新速度。
您可能想看看这篇博文 http://blog.mendeltsiebenga.com/post/Fun-with-planets.aspx 中的代码没有 xna,但工作轨道力学。 (虽然我从来没有摆脱屏幕闪烁)
关于c# - 轨道力学,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/655843/