gpt4 book ai didi

java - n 体模拟幻影力量

转载 作者:行者123 更新时间:2023-12-02 11:05:41 25 4
gpt4 key购买 nike

在我的 n 体模拟中,我有大约 1k 个粒子在四处飞行。我将位置存储为 float 。我遇到的一个问题是,每次运行代码时,当两个粒子彼此非常接近(本质上是相同的位置)时,它们就会极度加速。通常粒子的行为是平滑的。

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 更改为适合您模拟的值。

关于java - n 体模拟幻影力量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50985512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com