我正在尝试计算重力产生的净加速度,以便使用 (G*(m1 * m2)/d * d)/m1 构建一个简单的太空飞行模拟。船往往以阶梯模式朝着半正确的方向行驶。
主类的更新函数
public void update()
{
double[] accels = new double[bodies.size()];//acceleration of the planets
double[][] xyaccels = new double[bodies.size()][2];//storing the x and y
for(Body n: bodies){
int count = 0;
double dist = distance(ship.loc.x,ship.loc.y,n.loc.x,n.loc.y);
double acel = getAccel(n.mass, ship.mass, dist);
accels[count] = acel;
double alpha = getAngle(ship.loc.x,ship.loc.y,n.loc.x,n.loc.y);
//xyaccels[count][0] = Math.cos(alpha) * acel;
//xyaccels[count][1] = Math.sin(alpha) * acel;
//split the acceleration into the x and y
XA += Math.cos(alpha) * acel;
YA += Math.sin(alpha) * acel;
count++;
}
ship.update(XA, YA);
//XA = 0;
//YA = 0;
accels = null;
xyaccels = null;
}
飞船更新功能
public void update(double XA, double YA){
xAccel += XA;
yAccel += YA;
//add the x-acceleration and the y-acceleration to the loc
loc.x += Math.round(xAccel);
loc.y += Math.round(yAccel);
}
最佳答案
您不会通过加速更新位置。我看不到任何有关速度和加速度的方程。你的物理学是错误的。
二维 n 体问题需要每个体有四个耦合常微分方程。加速度、速度和位移都是二维 vector 。
dv/dt = F/m // Newton's F = ma
ds/dt = v // Definition of velocity that you'll update to get position.
你必须将它们全部整合在一起。
我假设您了解微积分和物理学。如果你不这样做,最好找到一个由其他人编写的库:类似 JBox2D .
关于java 2d净重力计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28389873/