两个物体之间的重力加速度 [英] Gravity's acceleration between two objects

查看:197
本文介绍了两个物体之间的重力加速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在编写一个程序,其中可以有两个对象(圆圈).我希望它们像行星一样围绕彼此绕行,但仅以2D旋转.

So I am making a program, where you can have two objects (circles). I want them to orbit like planets around each other, but only in 2D.

我知道,使用牛顿万有引力定律,我可以得到两个物体之间的力.我也知道A = F/M.我的问题是如何从前面的方程式中取A并将其更改为向量?

I know that using Newtons Universal Law of Gravitation I can get the force between the two objects. I also know A = F / M. My question is how would I take the A from the previous equation and change it into a vector?

推荐答案

您需要使用向量方程式:

You need to use vector equations:

// init values (per object)
double ax=0.0,ay=0.0,az=0.0; // acceleration [m/s^2]
double vx=0.0,vy=0.0,vz=0.0; // velocity [m/s]
double  x=0.0, y=0.0, z=0.0; // position [m]
double m=1.0;                  // mass [kg] 

// iteration inside some timer (dt [seconds] period) ...   
int i; double a,dx,dy,dz; // first compute acceleration
for (ax=0.0,ay=0.0,az=0.0,i=0;i<obj.num;i++) 
 if (obj[i]!=this) // ignore gravity from itself
  {
  dx=obj[i].x-x;
  dy=obj[i].y-y;
  dz=obj[i].z-z;
  a=sqrt((dx*dx)+(dy*dy)+(dz*dz));     // a=distance to obj[i]
  a=6.67384e-11*(obj[i].m*m)/(a*a*a); // a=acceleration/distance to make dx,dy,dz unit vector 
  ax+=a*dx; // ax,ay,az = actual acceleration vector (integration)
  ay+=a*dy;
  az+=a*dz;
  }
vx+=ax*dt; // update speed via integration of acceleration
vy+=ay*dt;
vz+=az*dt;
 x+=vx*dt; // update position via integration of velocity
 y+=vy*dt;
 z+=vz*dt;

代码摘自此处

  • obj[]是所有对象的列表
  • obj.num是它们的数量
  • obj[] is list of all your objects
  • obj.num is the count of them

我建议使用(ax,ay,az,...m)中的所有变量创建对象类,将它们初始化一次,然后在某个计时器中不断更新(迭代).如果要获得更高的精度,则应首先并且仅在更新速度和位置之后才为所有对象计算ax,ay,az(以避免在重力计算过程中更改对象的位置).如果要驱动对象(如使用信任器),则只需将其加速度添加到ax,ay,az向量)

I recommend to create object class with all the variables inside (ax,ay,az,...m), init them once and then continuously update (iteration) in some timer. If you want more accuracy then you should compute ax,ay,az for all objects first and only after update speed and position (to avoid change of position of objects during gravity computation). If you want to drive an object (like with truster) then just add its acceleration to ax,ay,az vector)

现在只需设置一个轨道即可:

Now to setup an orbit just:

  1. 放置行星对象

必须足够大,并将其position / velocity设置为您想要的

must be massive enough and also set its position / velocity to what you want

放置卫星

初始位置应该在行星附近.它不应该太大.初始化也以与轨道轨迹相切的方向来加速向量.如果速度太低,它将崩溃进入行星;如果速度太高,它将脱离行星,否则将绕轨道运行(圆形或椭圆形)

Initial position should be somewhere near planet. It should not be too massive. Init also speed vector with tangent direction to orbiting trajectory. If speed is too low it will collapse into planet and if speed is too high it will escape from planet otherwise will be orbiting (circle or ellipse)

计时器

间隔越短越好,通常10ms的模拟是可以的,但是对于大而远的物体,100ms也可以,并且更好.如果您想要粒子或其他东西,请使用1ms(非常动态的场景).

lower the interval better the simulation usually 10ms is OK but for massive and far objects is also 100ms and more OK. If you want particles or something then use 1ms (very dynamic sceene).

我强烈建议您阅读以下相关的质量检查:

I strongly recommend to read this related QA:

尤其是[edit3]关于集成精度和创建轨道数据的信息.

especially [edit3] about the integration precision and creating orbital data.

这篇关于两个物体之间的重力加速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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