java - 为什么我的 PVector 应该保持不变,但在此草图中被修改

标签 java processing

我正在构建一个草图,目前有两个类;步行者和发射器。步行器进行随机的柏林噪声步行,并随着时间的推移逐渐消失。发射器是发射步行者的点,并具有描述发射器位置的 PVector“位置”和描述每帧动画发射的步行者密度的 float “密度”。

我有两个问题。第一个也是最严重的问题是,由于某种原因,我的发射器类中的位置 PVector 随着时间的推移而变化(看起来好像我以某种方式使其也随机行走)。这是怎么发生的?请注意,在 emit() 方法中,我有一条注释行,它每次都会强制使用相同的 PVector,并且这完全按照预期工作。

第二个问题是,步行者似乎既倾向于向东北方向漂移,又似乎松散地束缚在正方形中。我不知道是什么原因导致这种行为,所以任何见解将不胜感激。

干杯!

代码:

ArrayList<Walker> walkers;
ArrayList<Emitter> emitters;
int tmax = 1200;
int stepSize = 2;

int nWalkers = 50;

void setup(){
  size(1024,1024);
  frameRate(60);
  walkers = new ArrayList<Walker>();
  emitters = new ArrayList<Emitter>();
  emitters.add(new Emitter(new PVector(width/2, height/2), 0.5));



}


void draw() {

  for (Emitter e: emitters){
  e.emit(); 
  }

  fill(255, 50); // alpha will control fade-out (agaaaaaaaaaaaaaaaaaaaaaaaaaaaaaain)
  noStroke();
  rect(0, 0, width, height); // Creates fading tail for walkers


  for(int i = walkers.size() - 1; i>=0; i--){
    Walker w = (Walker) walkers.get(i);
    if(w.time > tmax) {
    walkers.remove(i);

    }
    w.walk();
    w.displayline();


  }

}

class Emitter {
  PVector position;
  float density;

  Emitter(PVector positionIni,  float densityIni) {
  position = positionIni;
  density = densityIni;

  }

  void emit() {

    if(random(1000) > map(density, 0, 1, 0, 1000)) {
      walkers.add(new Walker(position, new PVector(random(-10,10), random(-10,10)))); // DOESN'T WORK
      //walkers.add(new Walker(new PVector(width/2, height/2), new PVector(random(-10,10), random(-10,10))));
    }
  }


}



class Walker {
  PVector location, plocation;
  PVector noff, step;
  int time;

  Walker(PVector locationIni, PVector noffIni) {
    location = locationIni;
    plocation = new PVector(location.x, location.y);
    noff = noffIni;
    step = new PVector(map(noise(noff.x), 0, 1, -stepSize, stepSize), map(noise(noff.y), 0, 1, -stepSize, stepSize));
    time = 0;
  }

  void displayline() {
    strokeWeight(1);
    fill(map(time, 0, tmax, 0, 255));
    stroke(map(time, 0, tmax, 0, 255));
    //ellipse(location.x, location.y,1,1);
    line(plocation.x, plocation.y,location.x, location.y);
    time++;

  }
  void walk() {


    plocation.x = location.x;
    plocation.y = location.y;

    step.x = map(noise(noff.x), 0, 1, -stepSize, stepSize);
    step.y = map(noise(noff.y), 0, 1, -stepSize, stepSize);

    location.add(step);

    noff.x += 0.05;
    noff.y += 0.05;
  }

}

最佳答案

I have two problems. The first and most serious problem is that for some reason the position PVector in my emitter class is varying over time (looks as if I'm somehow making it also randomly walk). How is this happening? Notice in the emit() method I have a commented line which forces the same PVector each time and this works precisely as intended.

您已经准确描述了问题。您将 position 变量传递给 Walker 类,然后 Walker 类会更改该 PVector在这一行中:

location.add(step);

由于您要更改传入的变量,因此您也将更改原始的 position 变量。这就是为什么当您传入不同的 PVector 实例时它可以正常工作的原因。

要解决此问题,您可能需要查看 PVector 类的 copy() 函数。更多信息可参见the reference .

The second problem is that the walkers seem to have a tendency to both drift on a north-easterly bearing and also seem to be loosely bound in a square.

正方形正在发生,因为你的最大边界是一个正方形。考虑一下您的头寸可能获得的最大值(value)。这形成了一个正方形,因此如果您在该正方形中有一堆位置,您将开始看到正方形。要解决这个问题,您必须使用一些基本的三角学来使最大值变为圆。基本上:存储航向和速度,然后使用 cos() 和 sin() 计算位置。 Google 是您的 friend 。

如果您注意到它们朝某个方向移动,则意味着您的随机数生成已关闭。尝试将此行分成多个步骤,以准确追踪偏差的来源:

step = new PVector(map(noise(noff.x), 0, 1, -stepSize, stepSize), map(noise(noff.y), 0, 1, -stepSize, stepSize));

关于java - 为什么我的 PVector 应该保持不变,但在此草图中被修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42248402/

相关文章:

java - Tomcat 垃圾收集

java - 通过在数字之间加“5”来返回最大可能的整数?

Java/有关对象声明和健全性检查的处理

java - twitter4j 和处理出现问题

java 和 c++ 客户端/服务器

java - 将 Liferay portlet 导入 Eclipse IDE

java - 在使用Javafx播放视频时,如何让 “rect”(处理库)出现在它的前面?

java - 如何覆盖系统内部处理的快捷方式

javascript - 在p5js中画一条平滑的线

java - 更高效地读取字符串