【发布时间】:2018-06-19 11:58:36
【问题描述】:
我正在做一个简单的 n 体模拟。现在,我正在“暴力破解”它,这意味着我每帧计算每个对象对每个其他对象施加的每一个力。
我现在的问题是,如果我选择大量对象,例如 2000 个,在某些情况下,在开始时,对象“行星”会消失大约 2 帧。当我通过将System.out.println(PlanetHandler.planets.get(0).position.x); 添加到主循环中来检查发生了什么时,我得到了
487.0
486.99454
NaN
NaN
通过注释掉一些东西和反复试验,我发现问题出在:
private static void computeAndSetPullForce(Planet planet)
{
for(Planet otherPlanet : planets)
{
//Also here, if we are deleting the planet, don't interact with it.
if(otherPlanet != planet && !otherPlanet.delete)
{
//First we get the x,y and magnitudal distance between the two bodies.
int xDist = (int) (otherPlanet.position.x - planet.position.x);
int yDist = (int) (otherPlanet.position.y - planet.position.y);
float dist = Vector2Math.distance(planet.position, otherPlanet.position);
//Now we compute first the total and then the component forces
//Depending on choice, use r or r^2
float force = Constants.GRAVITATIONAL_CONSTANT * ((planet.mass*otherPlanet.mass)/(dist*dist));
float forceX = force * xDist/dist;
float forceY = force * yDist/dist;
//Given the component forces, we construct the force vector and apply it to the body.
Vector2 forceVec = new Vector2(forceX, forceY);
planet.force = Vector2Math.add(planet.force, forceVec);
}
}
}
“行星”列表是CopyOnWriteArray<Planets>。
我已经有一段时间了,但还没有弄清楚是什么可能导致值(位置,速度)变南。也许有这方面经验或通常擅长这类事情的人可以帮助我。
【问题讨论】:
-
唯一似乎能够在您发布的内容中编辑planet.position 是Vector2Math.distance
-
您使用距离公式(暗示平方根),但您始终使用结果的平方。您可以通过将长度平方来节省一些速度。 (始终尽量减少物理引擎中的 sqrt/pow 调用量)
-
但是我怎么能计算`浮动力 = Constants.GRAVITATIONAL_CONSTANT * ((planet.massotherPlanet.mass)/(distdist));浮动 forceX = 力 * xDist/dist;浮动 forceY = force * yDist/dist;`?如果我是正确的,我只能通过使用几何来计算组件。
-
float对于任何严重的使用来说都是一种糟糕的浮点类型,并且只有大约 7-8 位的精度。至少更喜欢double。
标签: java simulation physics