繁体   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