Processing 处理1.5-如何实现可变加速度?

Processing 处理1.5-如何实现可变加速度?,processing,Processing,我刚刚开始学习处理,我被中的一个问题(练习1.8)困住了。我试图实现一个可变大小的加速度,当球离鼠标越近或越远时,球的加速度应该越大 我不知道怎么做,希望有人能指导我做这个练习。谢谢。练习中的示例代码当前设置加速度如下: PVector dir = PVector.sub(mouse,location); dir.normalize(); dir.mult(0.5); acceleration = dir; PVector direction = PVector.sub(mouse,locat

我刚刚开始学习处理,我被中的一个问题(练习1.8)困住了。我试图实现一个可变大小的加速度,当球离鼠标越近或越远时,球的加速度应该越大


我不知道怎么做,希望有人能指导我做这个练习。谢谢。

练习中的示例代码当前设置加速度如下:

PVector dir = PVector.sub(mouse,location);
dir.normalize();
dir.mult(0.5);
acceleration = dir;
PVector direction = PVector.sub(mouse,location); //Subtracting the mouse location from the object location to find the vector dx,dy
m = direction.mag(); // calculating the magnitude
println(m); // outputting the result of the magnitude
direction.normalize(); //normalize
direction.mult(m*0.1); //scaling with a fixed magnitude
第一行获取从移动器到鼠标的向量。对其进行规格化使其成为“单位向量”(即长度为1.0)——这将为您提供一个与幅值无关的方向。然后,
mult(0.5)
行缩放该方向,使其固定大小为0.5


本练习要求您根据到鼠标的距离为其指定一个可变大小。您所需要做的就是计算移动器与鼠标的距离,并基于该距离缩放方向向量(而不是硬编码的0.5值)。你会发现使用原始距离可能会太多,所以你需要将它乘以一点。

我认为应该是这样的:

PVector dir = PVector.sub(mouse,location);
dir.normalize();
dir.mult(0.5);
acceleration = dir;
PVector direction = PVector.sub(mouse,location); //Subtracting the mouse location from the object location to find the vector dx,dy
m = direction.mag(); // calculating the magnitude
println(m); // outputting the result of the magnitude
direction.normalize(); //normalize
direction.mult(m*0.1); //scaling with a fixed magnitude

是的,对不起我的身份:/