java 2d净重力计算

标签 java gravity

我正在尝试计算重力产生的净加速度,以便使用 (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/

相关文章:

java - 如何将java中的电话号码格式化为Android

java - 什么是NullPointerException,我该如何解决?

java - Box2d 主体不响应输入

android - 按钮中的字符串中心和底部

Java 实现一个可运行的 FPS 系统

Java += 运算符?

java - 为什么 MapMaker.softKeys() 被弃用?

java - 在 UntypedActor Play 2.5 java 中注入(inject)变量 null

php - 重力形式自定义验证过滤器

c - 受限3人体模拟无法正常工作