patternMinor
Particles moving around a Perlin Noise field
Viewed 0 times
aroundfieldperlinmovingnoiseparticles
Problem
I made this sketch in Processing (basically Java) that consists of a bunch of particles moving around a Perlin Noise field. Each particle is aware of the position of one other particle and this creates a rope like effect. The sketch is intended to be aesthetic, like a screensaver, and I in that way it might be difficult to judge the efficiency of the code.
Nevertheless, I'm hoping that there is something that I'm doing wrong stylistically that could be pointed out.
Nevertheless, I'm hoping that there is something that I'm doing wrong stylistically that could be pointed out.
int emergenceInterval = int(random(75, 100));
float a, freq = 0.05;
float alphDelta, alphAccel, alphSpringing = 0.0009, alphDamping = 0.98, alphX, maxAlph;
int pAmt = int(random(2000, 4000));
Particle p[] = new Particle[pAmt];
void setup() {
background(0);
size(1000, 500);
for (int i = 0; i width) alphAccel *= -1;
else if(alphX width) pos.x = 0;
if (pos.y > height) pos.y = 0;
if (pos.y < 0) pos.y = height;
vel.add(accel);
float speed = noise(pos.x, pos.y) * maxSpeed;
vel.limit(speed);
pos.add(vel);
}
PVector noiseDir(float chaos) {
theta = abs(noise(pos.x, pos.y)) * 180;
float x = pos.x + stride * cos(theta);
float y = pos.y + stride * sin(theta);
PVector dir = new PVector(x - pos.x, y - pos.y);
dir.normalize();
dir.mult(chaos * maxChaos);
return dir;
}
PVector followDir(PVector target) {
PVector dir = new PVector(target.x - pos.x, target.y - pos.y);
dir.normalize();
dir.mult(5);
return dir;
}
}Solution
(1). From the point of style I'd recommend you not to mix the level of details in a function. For example.
Instead of writing something like this:
You should write something like this. The same is applicable for other functions.
(2). As for naming conventions I'd recommend you to use a verb as a first part for the function's name. Code like this looks unusual.
(3). You should be consistent with names for your variables. For example in your update() function you can see a mix of full and short names like stride, target, chaos vs accel, vel, pos. It really makes me think a lot more about names than I should.
(4).If you have to call this "normalize()" method a lot after construction of the object I'd recommend to think about adding this method to consturctor for example with the flag called "shouldNormalize".
vs
(5). I would strongly recommend to make Particle class immutable. It would save you from a lot of hacks like this and make your code cleaner:
Instead of writing something like this:
void setup() {
background(0);
size(1000, 500);
for (int i = 0; i < pAmt; i++) { // this is bad
p[i] = new Particle(random(width), random(height), i);
}
}You should write something like this. The same is applicable for other functions.
void setup() {
background(0);
size(1000, 500);
fillArray(); // there goes the loop and it's load easier to read.
}(2). As for naming conventions I'd recommend you to use a verb as a first part for the function's name. Code like this looks unusual.
background(0);
size(1000, 500);(3). You should be consistent with names for your variables. For example in your update() function you can see a mix of full and short names like stride, target, chaos vs accel, vel, pos. It really makes me think a lot more about names than I should.
(4).If you have to call this "normalize()" method a lot after construction of the object I'd recommend to think about adding this method to consturctor for example with the flag called "shouldNormalize".
PVector dir = new PVector(x - pos.x, y - pos.y);
dir.normalize();vs
PVector dir = new PVector(x - pos.x, y - pos.y, true);
//or make it to normalize the vector by default and pass false when you don't need it
//but I guess that you should normalize it quite often after creation(5). I would strongly recommend to make Particle class immutable. It would save you from a lot of hacks like this and make your code cleaner:
void update(float stride, PVector target, float chaos) {
this.stride = stride;
... }Code Snippets
void setup() {
background(0);
size(1000, 500);
for (int i = 0; i < pAmt; i++) { // this is bad
p[i] = new Particle(random(width), random(height), i);
}
}void setup() {
background(0);
size(1000, 500);
fillArray(); // there goes the loop and it's load easier to read.
}background(0);
size(1000, 500);PVector dir = new PVector(x - pos.x, y - pos.y);
dir.normalize();PVector dir = new PVector(x - pos.x, y - pos.y, true);
//or make it to normalize the vector by default and pass false when you don't need it
//but I guess that you should normalize it quite often after creationContext
StackExchange Code Review Q#4271, answer score: 3
Revisions (0)
No revisions yet.