簡體   English   中英

Java 2D凈重計算

[英]java 2d net gravity calculation

我試圖計算由於重力引起的凈加速度,以便使用(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);
}

您不會通過加速來更新位置。 我沒有看到任何將速度與加速度相關的方程式。 你的物理學是錯誤的。

2D n體問題要求每個體具有四個耦合的常微分方程。 加速度,速度和位移都是2D向量量。

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

您必須將所有這些集成在一起。

我假設您對微積分和物理學有所了解。 如果不這樣做,最好找一個由其他人編寫的庫: JBox2D之類的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM