【发布时间】:2018-06-22 10:01:37
【问题描述】:
在我的 n 体模拟中,我有大约 1k 个粒子飞来飞去。我将位置存储为浮点数。我遇到的一个问题是,每次我运行代码时,当两个粒子彼此非常接近(基本上是相同的位置)时,它们会被极大地加速。通常粒子的行为是平滑的。
if((planet.position.x != otherPlanet.position.x && planet.position.y != otherPlanet.position.y) && !otherPlanet.delete)
{
//First we get the x,y and magnitudal distance between the two bodies.
float xDist = (otherPlanet.position.x - planet.position.x);
float yDist = (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);
otherPlanet.force = Vector2Math.subtract(otherPlanet.force, forceVec);
}
我没有找到关于这个主题的任何内容,但这是我做错了什么还是我必须实现最大加速度或粒子之间的最小距离?
【问题讨论】:
-
“当两个粒子非常接近时,它们会以极快的速度向外爆炸。”这在这种情况下没有任何意义,我建议您重新表述您的问题
-
@DPM 我认为它清楚地描述了问题
-
“非常接近”、爆炸、极速和向外并不意味着 10 行 java 代码抽象级别的任何东西。
-
是的,我很久以前就编辑过了。
标签: java simulation game-physics