2015-02-08 37 views
0

我想通过重力计算净加速度,以便用(G *(m1 * m2)/ d * d)/ m1建立一个简单的空间飞行模拟。船舶往往以阶梯式走向半正确的方向。java 2d净重计算

主类

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); 
} 

回答

1

你不从加速更新单元的更新功能。你没有任何关于速度与加速度的方程式,我可以看到。你的物理学是错误的。

二维n体问题需要四个耦合的每个物体的常微分方程。加速度,速度和位移都是2D向量。

dv/dt = F/m // Newton's F = ma 
ds/dt = v // Definition of velocity that you'll update to get position. 

您必须将它们全部集成在一起。

我假设你知道关于微积分和物理的一些东西。如果你不这样做,最好找一个由其他人写的库:比如JBox2D

+0

感谢您的帮助,我不会真正理解这些方程式,但我想我可以弄明白。 – KoalaStorm 2015-02-08 03:29:22

+0

你的问题不是编程;它是微积分和物理。你有没有学过?如果没有,你最好还是去图书馆帮忙。 – duffymo 2015-02-08 12:09:34