java - n 体模拟幻影力量

标签 java simulation game-physics

在我的 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/

相关文章:

java - 使用 Java 中的 Google Translate V2 API 客户端库发送翻译请求

java - 无法使用 eclipse link 和 oracle db 生成表 - IllegalBlockSizeException

python - Python中的分子动力学模拟

r - 生成具有特定标准差的相关随机变量

algorithm - 计算和应用摩擦力

java - 如何使 JDBC 发出的查询显示在 postgres 日志中?

java - 编译并升级到JDK 1.7

c# - 设计分布式仿真的最佳方式

c# - Unity 二维物理 : angle limits and motors are preventing rigidbodies to sleep

python - 简单游戏的Pygame低帧率