小编Pau*_*ian的帖子

如何在太阳系模拟器中添加月亮?

我正在尝试用 Java 编写太阳系模拟代码(使用现实世界的值)。首先,我利用牛顿万有引力公式和决定轨道时间的起始速度制作了一颗绕太阳旋转的行星。代码的工作原理如下:我有一个行星列表(假设太阳是一颗行星),在每一帧上我循环遍历该列表,并根据所有其他行星的总力对每个行星应用重力。每个行星在 2D 空间中都有一个速度矢量和一个位置矢量,并且执行最多工作的 2 个函数如下所示:

private Point2D getAttractionForce(Planet planet){
    double distanceX = planet.x-this.x;
    double distanceY = planet.y-this.y;
    double distance = Math.sqrt(Math.pow(distanceX, 2)+Math.pow(distanceY, 2));

    double force = SolarSystem.G*this.mass*planet.mass/Math.pow(distance, 2);
    double angle = Math.atan2(distanceY, distanceX);
    double forceX = force * Math.cos(angle);
    double forceY = force * Math.sin(angle);

    return new Point2D(forceX, forceY);
}

public void updatePosition(List<Planet> planets){
    double totalForceX = 0;
    double totalForceY = 0;

    for (Planet planet : planets){
        if (planet == this) continue;
            Point2D force = getAttractionForce(planet);
            totalForceX += …
Run Code Online (Sandbox Code Playgroud)

java math physics javafx

4
推荐指数
1
解决办法
127
查看次数

标签 统计

java ×1

javafx ×1

math ×1

physics ×1