Noise grid stops moving after a while

Hello!

I have this code that is doing what i want. But i would like for it to keep “animating” in eternity.
I think i know why i stops, but I don’t know how to explain it properly or do anything about it. :slight_smile:

Is there any way to keep it going?

Thanks in advance!

  • Mads
float gridcelleStr =8; 
float gridBredde;
float gridHoejde;
float faktor = 2;
float fart;
float xPos; 
float yPos; 
float tal; 
float strFaktor = 30;

void setup() {
  size(1200, 1200, P3D);
  gridBredde = floor(120);
  gridHoejde = floor(120);
  fart = 0;
  rectMode(CENTER);
  imageMode(CENTER);
}
void draw() {
  background(255);
  for (int y = 0; y < gridHoejde; y++) {
    for (int x = 0; x <gridBredde; x++) {
      pushMatrix();
      translate(100, 80);
      fart += 0.0000002;
      tal = floor(noise(x/gridHoejde, y / gridBredde, fart) * faktor);
      xPos = x * gridcelleStr;
      yPos = y * gridcelleStr;
      noStroke();
      fill(random(0), random(0), random(150, 255));
      rect(xPos, yPos, tal * strFaktor, tal * strFaktor);
      popMatrix();
    }
  }
}

It does keep going, but it doesn’t keep displaying. Your problem is that you have global variables – in particular, speed (fart) – that you keep incrementing, until the number is so large your sketch doesn’t work anymore.

Try bouncing that value around between -1 and 1, for example:

float gridcelleStr =8; 
float gridBredde;
float gridHoejde;
float faktor = 2;
float fart;
float fart_diff = 0.0000002;
float xPos; 
float yPos; 
float tal; 
float strFaktor = 30;

void setup() {
  size(1200, 1200, P3D);
  gridBredde = floor(120);
  gridHoejde = floor(120);
  fart = 0;
  rectMode(CENTER);
  imageMode(CENTER);
}
void draw() {
  background(255);
  if (fart>1) {
    fart_diff = -0.0000002;
  }
  if (fart<-1) {
    fart_diff = 0.0000002;
  }
  for (int y = 0; y < gridHoejde; y++) {
    for (int x = 0; x <gridBredde; x++) {
      pushMatrix();
      translate(100, 80);
      fart += fart_diff;
      tal = floor(noise(x/gridHoejde, y / gridBredde, fart) * faktor);
      xPos = x * gridcelleStr;
      yPos = y * gridcelleStr;
      noStroke();
      fill(random(0), random(0), random(150, 255));
      rect(xPos, yPos, tal * strFaktor, tal * strFaktor);
      popMatrix();
    }
  }
}

Eh close but not exactly, the variable fart only gets to 4.0 before it freezes, which wouldn’t even overflow a byte. The problem is computers are bad at adding really small decimals so adding 4.0+0.0000002 = 4.0 in Java land. So fart will just be 4.0 until the end of time and we will never advance through the noise any farther which causes it to appear to freeze.

Yes, both these things are true. I never said it was an overflow–in fact, I gave a sketch with a demo range of +/-1.0 (!)

There are several ways around tiny float increments that freeze–but if you are trying for the smallest increment possible and don’t want reflection looping, one approach is:

 speed = Math.nextUp(speed);