我正在try 用Java编写一个太阳系模拟代码(使用真实世界的值).首先,我利用牛顿的引力公式和确定轨道时间的起始速度,制作了一颗绕太阳旋转的行星. 代码的工作原理是这样的:我有一个行星列表(假设太阳是一颗行星),在每一帧上,我循环通过该列表,并根据所有其他行星的总力为每一颗行星应用引力. 每个行星在2D空间中都有一个速度向量和一个位置向量,做功最多的两个函数如下所示:
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 += force.getX();
totalForceY += force.getY();
}
}
this.xVelocity += totalForceX/this.mass*TIMESTEP;
this.yVelocity += totalForceY/this.mass*TIMESTEP;
this.x += this.xVelocity*TIMESTEP;
this.y += this.yVelocity*TIMESTEP;
}
The problem
我的问题是,当我在地球附近加上一个月亮时:
Planet moon = new Planet(Color.GRAY, 4, 7.35e22, -(1.50e11+4e8), 0, 0, 1022);
月球受到太阳的吸引,并不像它应该做的那样绕地球自转.出于调试目的,我计算了以下值:
Distance moon-sun: 1.504E11
Sun force for moon: 4.313523549126089E20
Distance moon-earth: 4.0E8
Earth force for moon: 1.831019119125E20
我得到的月球-太阳引力是月球-地球引力的4倍. 我试着改变月球的初始速度,但我得到的只是月球绕太阳旋转. 我做错了什么?
下面是完整的代码(需要JavaFX):https://pastebin.com/fiU4m7Te
感谢您的任何帮助,谢谢