【问题标题】:n-Body Simulation Phantom Forcesn体模拟幻象力
【发布时间】: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


【解决方案1】:

模拟世界与现实世界有点不同,为了做出好的模拟,我们需要添加一些限制。

问题:

当两个粒子非常接近时,它们就会爆炸 以惊人的速度向外。

原因

原因很简单,重力是相反的 与两个物体之间的距离平方成正比。当两人 物体靠得太近,半径(它们之间的距离)变得非常小 并且作用在它们身上的力变得非常非常大。

解决方案

添加两个粒子可以接近多少的虚拟限制。通过 虚拟限制我的意思是限制只在值上而不是在 模拟。例如。如果它们之间的距离小于 5(a 阈值)将距离设置为 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 更改为适合您模拟的值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 2019-02-05
    • 2014-07-03
    • 2020-04-10
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    相关资源
    最近更新 更多