二维轨道物理 [英] 2D orbital physics

查看:26
本文介绍了二维轨道物理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为游戏开发 2D 物理引擎.我有重力和质量,使用简单的迭代方法(我知道我最终必须升级);我可以手动推动群众,看着他们移动,一切都如我所愿.

I'm working on a 2D physics engine for a game. I have gravity and masses working, using a simple iterative approach (that I know I'll have to upgrade eventually); I can push the masses around manually and watch them move and it all works as I'd expect.

现在,我正在尝试使用围绕行星的简单圆形轨道上的卫星提前设置游戏世界.为此,我需要根据行星的质量和所需的距离来计算卫星的初始速度矢量;这应该是微不足道的,但我一生都无法让它正常工作.

Right now I'm trying to set up the game world in advance with a satellite in a simple circular orbit around a planet. To do this I need to calculate the initial velocity vector of the satellite given the mass of the planet and the desired distance out; this should be trivial, but I cannot for the life of me get it working right.

标准物理教科书告诉我,一个物体在围绕质量为 M 的圆形轨道上的轨道速度是:

Standard physics textbooks tell me that the orbital velocity of an object in circular orbit around a mass M is:

v = sqrt( G * M / r )

然而,在应用适当的向量后,卫星的速度不够快,并落在了一个尖锐的椭圆轨道上.随机修修补补表明,在一个案例中,它下降了大约 3 倍.

However, after applying the appropriate vector the satellite isn't going anything like fast enough and falls in in a sharply elliptical orbit. Random tinkering shows that it's off by about a factor of 3 in one case.

我的重力模拟代码使用的是传统的:

My gravity simulation code is using the traditional:

F = G M m / r^2

G 在我的宇宙中设置为 1.

G is set to 1 in my universe.

有人可以向我确认这些方程在二维空间中仍然成立吗?我看不出有什么理由不这样做,但此时我真的很想知道问题出在我的代码还是我的假设中...

Can someone confirm to me that these equations do still hold in 2D space? I can't see any reason why not, but at this point I really want to know whether the problem is in my code or my assumptions...

更新:我的物理引擎的工作原理如下:

Update: My physics engine works as follows:

for each time step of length t:
  reset cumulative forces on each object to 0.
  for each unique pair of objects:
    calculate force between them due to gravity.
    accumulate force to the two objects.
  for each object:
    calculate velocity change dV for this timestep using Ft / m.
    v = v + dV.
    calculate position change dS using v * t.
    s = s + dS.

(当然,在适当的地方使用向量.)

(Using vectors where appropriate, of course.)

现在我每帧都做一个物理滴答,每秒发生大约 500-700 次.我知道这会很快积累错误,但至少应该让我开始.

Right now I'm doing one physics tick every frame, which is happening about 500-700 times per second. I'm aware that this will accumulate errors very quickly, but it should at least get me started.

(顺便说一句,我找不到处理轨道力学的现成物理引擎——大多数 2D 物理引擎,例如 Chipmunk 和 Box2D,而是更专注于刚性结构.谁能建议我可以看看?)

(BTW, I was unable to find an off-the-shelf physics engine that handles orbital mechanics --- most 2D physics engines like Chipmunk and Box2D are more focused on rigid structures instead. Can anyone suggest one I could look at?)

推荐答案

您需要确保您的 delta t 迭代时间值足够小.您肯定必须修改常量以获得您期望的行为.在您的情况和大多数情况下,迭代模拟是一种集成形式,其中错误快速且不可预测地累积.

You need to make sure that your delta t iterative time value is small enough. You will definitely have to tinker with the constants in order to get the behaviour you expect. Iterative simulation in your case and most cases is a form of integration where errors build up fast and unpredictably.

这篇关于二维轨道物理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆