2D轨道物理学 [英] 2D orbital physics

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

问题描述

我正在为游戏开发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.

有人可以向我确认这些方程式仍然在2D空间中成立吗?我看不出为什么不这样做,但是在这一点上,我真的想知道问题出在我的代码中还是我的假设中……

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.

(顺便说一句,我找不到能处理轨道力学的现成的物理引擎-像Chipmunk和Box2D这样的大多数2D物理引擎更专注于刚性结构.有人能建议我看看吗?)

(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?)

推荐答案

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

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.

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

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