c# - 轨道力学

标签 c# xna physics

有没有人有实现轨道力学的例子(最好是在 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/

相关文章:

java - Box2D 通过 libGDX : How to create one MouseJoint per body for separate Android touches?

c# - 在 XNA 中对网格进行排序

c# - Windows 8 Metro App 仅在读取文件时在调试器中启动

android - 使用加速度计(陀螺仪?)在 2D 空间中绘图

c# - 通过匹配文本的字符串部分来删除重复项

xna - XNA GS 是否足以制作 2D 在线游戏?

c# - 分子 MC 模拟不平衡

c# - C#4 中的动态关键字是否支持扩展方法?

c# - 反编译导致错误CS1112,汇编困惑

c# - C# Thread.Sleep() 和 threadreference.Join() 有什么区别?