如何正确添加大量载体? (JAVA)

时间:2017-09-03 20:24:39

标签: java vector physics

我正在使用Euler方法创建一个只有2个维度的星系碰撞模拟来计算重力(Runge-Kutta会更准确,但我确实想要将它用于此程序)。我有" Galaxy核心"作为主要的引力质量和"星"作为测试群体,但他们自己不施加任何引力。问题在于,当我用测试质量填充星系的轨道速度时,我已经检查过以确保它是正确的,但不会超过重力。所有方程都应该是正确的,所以我认为我添加向量的方式存在问题。

这是物理图书馆......

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = ['password'];
}

这是Trig类...

package Physics;

import MathLib.Trig;
import MathLib.Vector2D;
import Objects.GalaxyCore;
import Objects.GravObjects;

public class Physics {
    public static final double G = 6.674 * Math.pow(10, -11);
    private static final double LIGHT_YEAR = 9.46 * Math.pow(10, 15);
    private static final double DT = 3.1536E12; // 100,000 years in seconds
    public static final double SCALE = LIGHT_YEAR * 5000;

    private static <GravOb extends GravObjects> void calcAcceleration(GalaxyCore[] coreArray, GravOb[] obArray) {
        for (GravOb ob : obArray) {
            Vector2D bufferVector;
            for (GalaxyCore core : coreArray) {
                double dist = Trig.calcDist(ob.getX(), ob.getY(), core.getX(), core.getY()) * SCALE;
                double angle = Trig.calcAngle(ob.getX(), ob.getY(), core.getX(), core.getY());
                double acceleration = dist > 0 ? (G * core.getMASS()) / Math.pow(dist, 2) : 0;

                bufferVector = new Vector2D(Math.cos(angle) * acceleration, Math.sin(angle) * acceleration);
                ob.setAccelVector(Trig.addVector(ob.getAccelVector(), bufferVector));
            }
        }
    }

    private static <GravOb extends GravObjects> void calcVelocity(GravOb[] obArray) {
        for (GravOb ob : obArray) {
            Vector2D bufferVector = new Vector2D(ob.getAccelVector().getMagX() * DT, ob.getAccelVector().getMagY() * DT);
            ob.setVelVector(Trig.addVector(ob.getVelVector(), bufferVector));
        }
    }

    public static <GravOb extends GravObjects> void calcPos(GalaxyCore[] coreArray, GravOb[] obArray) {
        for (GravOb ob : obArray) {
            ob.setX(ob.getX() + ob.getVelVector().getMagX() * DT / SCALE);
            ob.setY(ob.getY() + ob.getVelVector().getMagY() * DT / SCALE);
            calcAcceleration(coreArray, obArray);
            calcVelocity(obArray);
        }
    }
}

和矢量类......

package MathLib;

import static java.lang.Math.*;

public class Trig {
    public static double calcDist(double x1, double y1, double x2, double y2) {
        return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
    }

    public static double calcAngle(double x1, double y1, double x2, double y2) {
        return atan2(y2 - y1, x2 - x1);
    }

    public static double calcAngle(double x, double y) {
        return atan2(y, x);
    }

    public static double calcMag(double magX, double magY) {
        return sqrt(pow(magX, 2) + pow(magY, 2));
    }

    public static Vector2D addVector(Vector2D v1, Vector2D v2) {
        return new Vector2D(v1.getMagX() + v2.getMagX(), v1.getMagY() + v2.getMagY());
    }
}

编辑:我根据评论者的建议更新了代码。

编辑2:如果有一个测试质量与多个测试质量相比,程序的行为会有所不同。

0 个答案:

没有答案