反重力/反重力?我需要更改引力算法的哪些元素才能反转它? 更新更新 2

问题描述

我想反转我的重力算法以在多个物体相互作用的“过去”中产生位置。通过在一组物体上多次运行算法来生成未来的位置是微不足道的,但是反过来写出物体之前位置的位置让我感到难堪。我不想存储过去的位置,因为这是确定性的,应该可以以某种方式向后运行算法,但我不确定如何。

在循环中从 element 测试的每个主体的片段 universe 中,tick 是增量时间。

function forces(other) {
  if (element === other) {
    return;
  }
  
  var distancePoint = element.point.sub(other.point);
  var normal = Math.sqrt(100.0 + distancePoint.lengthSq());
  var mag = GravatationalConstant /
              Math.pow(normal,3);
  
  var distPointMulOtherMass = distancePoint
    .mul(mag * other.mass);
  
  element.acceleration = element.acceleration.sub(distPointMulOtherMass);
  other.acceleration = other
    .acceleration
    .add(distancePoint
         .mul(mag * element.mass) 
        );
}

element.acceleration = new Point(0,0);
    
universe.forEach(forces);
element.velocity = element.velocity.add(element.acceleration.mul(ticks));
element.point = element.point.add(element.velocity.mul(0.5 * ticks));  

我尝试发送负滴答声和负引力常数,但它为“过去”产生的位置似乎并不遵循元素在真实过去的表现。

我对物理学知之甚少,但我想知道是否可以进行一些小的更改来反转此算法。

更新

感谢 Graeme Niedermayer,我已将重力算法更新为平方反比定律,并且使用负时间似乎可以产生过去的位置!

function forces(other) {
  if (element === other) {
    return;
  }
  var distancePoint = element.point.sub(other.point);  
  const forceElementMass = GravatationalConstant * element.mass * other.mass / 
    Math.pow(element.mass,2)
  const forceOtherMass = GravatationalConstant * element.mass * other.mass / 
    Math.pow(other.mass,2)
  element.acceleration = element.acceleration
    .sub(distancePoint.mul(forceOtherMass))
  other.acceleration = other.acceleration
    .add(distancePoint.mul(forceElementMass))
}

const ticks = forwards ? dt : -dt;
element.acceleration = new Point(0,0);

universe.forEach(forces);
element.velocity = element.velocity.add(element.acceleration.mul(ticks));
element.point = element.point.add(element.velocity.mul(0.5 * ticks));  

轮廓圆圈位于当前位置,“过去”位置是其他逐渐淡出至零不透明度的位置。

enter image description here

更新 2

意识到我在更新 1 中使用了错误的方程(两个力常数使用相同的质量对象)。我查看了更多示例并更新了代码,但现在我不确定我应该在哪里添加增量时间 ticks,它目前仅设置为 1 表示向前,-1 表示向后。下图是如果我将加速度乘以 ticks,然后再将其与每帧 body.velocity = body.velocity.add(body.acceleration.mul(ticks)) 的速度相加,或者如果我使其中一个质量为负 const force = G * body.mass * (forward ? other.mass : -other.mass) / d ** 2,看起来会是什么样子。>

如您所见,绿色主体的“过去”位置(红色轮廓)移至左侧和上方。我希望它们看起来“跟随”当前位置,但我不确定如何反转或反转方程以显示“过去”位置,基本上是如果身体朝相反的方向移动。有没有办法做到这一点?

enter image description here

在下一张图像中,在将速度添加到位置 ticks 之前,我将速度乘以时间增量 body.point = body.point.add(body.velocity.mul(ticks)) 这会导致与记录的身体经过的路径相似的路径(通过将每个位置写入到一个数组并在这些位置之间画一条线)但它有点偏离。此解决方案与我在更新 1 中看到的类似。是否有理由认为这“几乎”正确?

enter image description here

下面的代码没有任何添加来反转位置。

function forces(other,ticks) {
  if (body === other) {
    return;
  }
  
  // Calculate direction of force
  var distanceVector = other.point.sub(body.point)

  // Distance between objects
  var d = distanceVector.mag()
  
  // Normalize vector (distance doesn't matter here,we just want this vector for direction)
  const forceNormalized = distanceVector.normalized()
  
  // Calculate gravitational force magnitude
  const G = 6.674
  const force = G * body.mass * other.mass / d ** 2
  
  // Get force vector --> magnitude * direction
  const magDirection = forceNormalized.mul(force)

  const f = magDirection.div(body.mass)  
  body.acceleration = body.acceleration.add(f)
}

body.acceleration = body.acceleration.mul(0)
universe.forEach(body => forces(body,ticks))
body.velocity = body.velocity.add(body.acceleration)
body.point = body.point.add(body.velocity)

更新 3

我最终去除了负质量和速度乘以滴答声,并颠倒了加速度施加到位置的方式:

if (forward) {
  universe.forEach(body => forces(body,ticks));
  body.velocity = body.velocity.add(body.acceleration)
  body.point = body.point.add(body.velocity)
} else {
  body.point = body.point.sub(body.velocity)
  universe.forEach(body => forces(body,ticks));
  body.velocity = body.velocity.sub(body.acceleration)
}

导致能够从当前位置及时向前和向后生成位置。在图像中,它显示为“过去”位置遵循当前位置的记录轨迹。

enter image description here

要在“过去”中生成一个步骤,它会从当前位置减去当前速度,将其置于上一次所在的位置。接下来,它通过检查来自其他物体的力来获得加速度,然后减去新的加速度(使用负质量会做同样的事情)从速度开始,所以“过去”中的下一个位置将是正确的。

解决方法

您应该能够使其中一个群众成为负面。

负时间不起作用的原因是因为您隐式使用了欧拉方法。欧拉方法在使用负步时不稳定。

此外,您使用的物理也有点奇怪。重力通常是一个平方定律。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...