为什么我的 PVector 应该保持不变在此草图中被修改
Why is my PVector which should remain constant being modified in this sketch
我正在构建一个目前有两个 classes 的草图;步行者和发射器。步行者随机进行 perlin-noise 步行,并随着时间的推移逐渐消失。发射器是发射步行者的点,并且有一个描述发射器位置的 PVector 'position' 和一个描述每帧动画发射的步行者密度的浮点数 'density'。
我有两个问题。第一个也是最严重的问题是,由于某种原因,我的发射器 class 中的 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
class,然后 Walker
class 在这一行中更改 PVector
:
location.add(step);
由于您正在更改传入的变量,因此您正在更改原始 position
变量。这就是为什么如果你传入一个不同的 PVector
实例它工作正常。
要解决此问题,您可能需要查看 PVector
class 的 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.
正方形出现是因为您的最大边界是正方形。考虑您的头寸可能采用的最大可能值。这形成了一个正方形,所以如果您在该正方形中有一堆位置,您将开始看到正方形。要解决此问题,您必须使用一些基本的三角函数来代替圆的最大值。基本上:存储航向和速度,然后使用 cos()
和 sin()
来计算位置。 Google 是你的朋友。
如果您注意到它们朝一个方向移动,则表示您的随机数生成已关闭。尝试将这条线分成多个步骤,以准确追踪偏差的来源:
step = new PVector(map(noise(noff.x), 0, 1, -stepSize, stepSize), map(noise(noff.y), 0, 1, -stepSize, stepSize));
我正在构建一个目前有两个 classes 的草图;步行者和发射器。步行者随机进行 perlin-noise 步行,并随着时间的推移逐渐消失。发射器是发射步行者的点,并且有一个描述发射器位置的 PVector 'position' 和一个描述每帧动画发射的步行者密度的浮点数 'density'。
我有两个问题。第一个也是最严重的问题是,由于某种原因,我的发射器 class 中的 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
class,然后 Walker
class 在这一行中更改 PVector
:
location.add(step);
由于您正在更改传入的变量,因此您正在更改原始 position
变量。这就是为什么如果你传入一个不同的 PVector
实例它工作正常。
要解决此问题,您可能需要查看 PVector
class 的 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.
正方形出现是因为您的最大边界是正方形。考虑您的头寸可能采用的最大可能值。这形成了一个正方形,所以如果您在该正方形中有一堆位置,您将开始看到正方形。要解决此问题,您必须使用一些基本的三角函数来代替圆的最大值。基本上:存储航向和速度,然后使用 cos()
和 sin()
来计算位置。 Google 是你的朋友。
如果您注意到它们朝一个方向移动,则表示您的随机数生成已关闭。尝试将这条线分成多个步骤,以准确追踪偏差的来源:
step = new PVector(map(noise(noff.x), 0, 1, -stepSize, stepSize), map(noise(noff.y), 0, 1, -stepSize, stepSize));