【问题标题】:java 2d net gravity calculationjava 2d净重力计算
【发布时间】:2015-02-08 02:19:17
【问题描述】:

我正在尝试计算重力引起的净加速度,以便使用 (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);
}

【问题讨论】:

    标签: java gravity


    【解决方案1】:

    您不会通过加速来更新位置。我没有看到任何将速度与加速度相关的方程式。你的物理学错了。

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

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

    您必须将所有这些集成在一起。

    我假设您对微积分和物理有所了解。如果你不这样做,最好找一个由其他人编写的库:类似JBox2D

    【讨论】:

    • 感谢您的帮助,我并不完全理解这些方程式,但我想我可以弄明白。
    • 你的问题不在于编程;这是微积分和物理。你也学过吗?如果没有,你最好找一个图书馆来帮助你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多