The different performance of noise() in processing and p5.js

I am learning a tutorial from Vamoss and trying to implement it with Processing.

But I encountered a problem in the noise() part of Chapter 2, which resulted in a different result from Vamoss.

This is my code:

int total = 200;
PVector [] pos;
float [] dir;

void setup() {
  size(800, 800);
  background(-1);
  pos = new PVector[total];
  dir = new float[total];

  for (int i = 0; i < total; i ++) {
    pos[i] = new PVector(width/2, height/2);
    dir[i] = random(TWO_PI);
  }
}

void draw() {
  int time = millis() / 1000;

  for (int i = 0; i < total; i ++) {
    PVector p = pos[i];
    float d = dir[i];

    d += noise(p.x, p.y, time) - 0.5; 

    p.x += cos(d);
    p.y += sin(d);

    circle(p.x, p.y, 10);
  }
}

I have checked it many times, and the code logic is ok, may I ask what went wrong? thank you very much for your help.

2 Likes

I found that it is not the noise() problem, but PVector, which is the latest code, which achieves the same effect as in the Vamoss tutorial (Chapter 2).

int total = 200;
PVector [] pos;
PVector [] dir;

void setup() {
  size(800, 800);
  pixelDensity(2);
  smooth();
  background(-1);
  pos = new PVector[total];
  dir = new PVector[total];

  for (int i = 0; i < total; i ++) {
    pos[i] = new PVector(width/2, height/2);
    dir[i] = new PVector(random(TWO_PI), random(TWO_PI));
  }
}

void draw() {
  int time = millis() / 1000;

  for (int i = 0; i < total; i ++) {
    PVector p = pos[i];
    PVector d = dir[i];

    d.add(noise(p.x, p.y, time) - 0.45, noise(p.x, p.y, time) - 0.45);

    p.x += cos(d.x);
    p.y += sin(d.x);

    circle(p.x, p.y, 10);
  }
}

It just changed float dir into PVector dir . Although it solved the problem, I don’t know the principle.

2 Likes

In Java we create objects outta existing classes via keyword new. We can’t use JS {} syntax for it.

2 Likes