我怎样才能阻止微粒的晃动?

时间:2020-04-27 21:39:43

标签: javascript math canvas particles

我正在看这个例子: https://codepen.io/chingy/pen/mddyKWZ

我无法弄清楚如何防止受鼠标位置影响的粒子停止摇动?它们处于正确的位置,但是它们不断来回弹跳。我希望他们仅在需要移动时才移动。

我想问题出在这个部分附近,该距离是根据粒子位置计算的,而粒子位置一直在变化,因为它正试图回到其起始位置?

repulseTo(x, y) {
      let distance = this.getDistanceTo(x, y),
          repulseAngle = Math.atan2(distance.y, distance.x),
          repulseForce = (-1 * Math.pow(mousePower, 2)) / distance.dist;

      this.dX += Math.cos(repulseAngle) * repulseForce;
      this.dY += Math.sin(repulseAngle) * repulseForce;        
  }

  springTo() {
      this.dX += (this.spring.x - this.x) * particleStiffness;
      this.dY += (this.spring.y - this.y) * particleStiffness;        
  }

  update() {
      this.springTo();

      this.dX *= particleFriction;
      this.dY *= particleFriction;

      this.x += this.dX;
      this.y += this.dY;
  }

0 个答案:

没有答案