javascript - 当移动物体靠近鼠标时,如何降低移动物体的加速度

问题描述

https://processing.org/examples/accelerationwithvectors.html

我想我有一个相当简单的问题,但我不知道如何以优雅的方式解决它。在上面的链接中有一个示例,说明如何使用加速属性使对象跟随鼠标。但是,当它靠近鼠标时,加速度的大小并没有减小,而是物体一直在绕圈。我想知道什么是减慢物体速度并最终在物体接近鼠标时阻止物体移动的最佳方法

代码是从处理网站复制的:

// A Mover object
Mover mover;

void setup() {
  size(640,360);
  mover = new Mover(); 
}

void draw() {
  background(0);
  
  // Update the location
  mover.update();
  // display the Mover
  mover.display(); 
}




/**
 * acceleration with Vectors 
 * by Daniel Shiffman.  
 * 
 * Demonstration of the basics of motion with vector.
 * A "Mover" object stores location,veLocity,and acceleration as vectors
 * The motion is controlled by affecting the acceleration (in this case towards the mouse)
 */


class Mover {

  // The Mover tracks location,and acceleration 
  PVector location;
  PVector veLocity;
  PVector acceleration;
  // The Mover's maximum speed
  float topspeed;

  Mover() {
    // Start in the center
    location = new PVector(width/2,height/2);
    veLocity = new PVector(0,0);
    topspeed = 5;
  }

  void update() {
    
    // Compute a vector that points from location to mouse
    PVector mouse = new PVector(mouseX,mouseY);
    PVector acceleration = PVector.sub(mouse,location);
    // Set magnitude of acceleration
    acceleration.setMag(0.2);
    
    // VeLocity changes according to acceleration
    veLocity.add(acceleration);
    // Limit the veLocity by topspeed
    veLocity.limit(topspeed);
    // Location changes by veLocity
    location.add(veLocity);
  }

  void display() {
    stroke(255);
    strokeWeight(2);
    fill(127);
    ellipse(location.x,location.y,48,48);
  }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)