n-Body 模拟幻影力
n-Body Simulation Phantom Forces
在我的 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);
}
我没有找到关于这个主题的任何信息,但这是我做错了什么,还是我必须实现最大加速度或粒子之间的最小距离?
模拟世界与现实世界有些不同,为了模拟的好我们需要添加一些限制。
问题:
When two particles get really close to each other, they just explode
outwards at breakneck speeds.
原因
The reason for this is simple force of gravity is inversely
proportional to distant squared between two bodies. When the two
bodies come too close ,radius (distance between them) becomes very less
and the force acting on them become very very large.
解决方案
Add a virtual limit on how much close two particles can come.By
virtual limit i mean that the limit is only on the values but not on
the simulation.For ex. if the distance between them is less than 5(a
threshold) set the distance to 5.
代码中的更改
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);
// add a limit to xDist and yDist
if(xDist<5)
xDist=5;
if(yDist<5)
yDist=5;
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);
}
您当然想将 5 更改为适合您模拟的值。
在我的 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);
}
我没有找到关于这个主题的任何信息,但这是我做错了什么,还是我必须实现最大加速度或粒子之间的最小距离?
模拟世界与现实世界有些不同,为了模拟的好我们需要添加一些限制。
问题:
When two particles get really close to each other, they just explode outwards at breakneck speeds.
原因
The reason for this is simple force of gravity is inversely proportional to distant squared between two bodies. When the two bodies come too close ,radius (distance between them) becomes very less and the force acting on them become very very large.
解决方案
Add a virtual limit on how much close two particles can come.By virtual limit i mean that the limit is only on the values but not on the simulation.For ex. if the distance between them is less than 5(a threshold) set the distance to 5.
代码中的更改
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);
// add a limit to xDist and yDist
if(xDist<5)
xDist=5;
if(yDist<5)
yDist=5;
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);
}
您当然想将 5 更改为适合您模拟的值。