Negative Noise problem

I found that when you put negative numbers into the noise function like xoff or yoff the noise function converts them to positive numbers. Why?? And how do I solve this to be able to put negative numbers into the noise function. Any help is appreciated.

Code is below ↓↓↓

float NoiseJump = 0.01;
int XPosition = -10;
int YPosition = -10;
int LengthToRun = 32;
int PositionInSave = 1;
String[] Save = new String[LengthToRun * LengthToRun + 1];
int NoiseScaleUpValue = 255;

float xoff = NoiseJump * (XPosition * LengthToRun);

  Save[0] = str(round(random(MAX_INT)));
  saveStrings("Numbers.txt", Save);
  for (int Row = 0; Row < LengthToRun; Row++) {
    xoff += NoiseJump;
    float yoff = NoiseJump * (YPosition * LengthToRun);
    for (int Colmn = 0; Colmn < LengthToRun; Colmn++) {
      yoff += NoiseJump;
      noiseDetail(12, 0.5);
      noiseSeed(33);
      float OceanNoiseGen = noise(xoff, yoff) * NoiseScaleUpValue;

      Save[PositionInSave] = "Row: " + Row + ", Colmn: " + Colmn + ", Value: " + OceanNoiseGen;
      PositionInSave++;

    }
  }
  
  saveStrings("Numbers.txt", Save);
  background(0, 255, 0);  

If you look at the documentation you’ll notice that the output of noise() should always be between 0 and 1.

https://processing.org/reference/noise_.html

Yeah I am pretty sure that’s not the problem because i just scale it up by 255. I think the problem is the fact that if xoff = -0.1 and then goes into the noise function it will change xoff to 0.1 that’s what i can not understand. Why it does that and how to make it stay negative.

It’s not clear from your message whether it’s the fact that the result is not negative that’s the problem, or the fact that it mirrors around zero (so the result of 0.1 is the same as -0.1)? If the latter, then you could add an offset to your input values so they’re always positive?

I’m not sure whether the mirroring around zero is expected behaviour, or just an optimization because the values are used to read from an array.

Thanks for your reply it helped a lot on realizing that noise cannot go into negatives because it only goes 0 to 1. Do you think to solve this problem I would need a to make it were noise goes from -1 to 1. Do you think this would work or would I have to make my own type of noise. Any thought or idea on this?

Can’t you just scale the output of noise?

eg.

float OceanNoiseGen = map(noise(xoff, yoff), 0, 1, -NoiseScaleUpValue, NoiseScaleUpValue);

I tried this but it still gives positive numbers.

Yes, because the area of noise you’re looking at only has values in that region! If you change values like NoiseJump or LengthToRun you’ll see negative values. Let’s take a step back. What are you actually trying to do?

1 Like

I am trying to make a noise map but the problem I am having is were when you move into the negative values it copies the same chunk but in positive numbers. Aka it changes any negative xoff or yoff to positive numbers this is the problem I am trying to solve.

When you change LengthToRun or NoiseJump in your sketch do they go negative. Because in mine I put a minus in front of them and it didn’t change anything?

Why would you make them negative? If you make them bigger you get negative outputs with the map code I posted earlier.

If your problem is mirroring when you pass in negative inputs then add a large enough offset to both numbers so they’re always positive, as I said earlier.

Other than those suggestions I really can’t help further because if that doesn’t cover it I don’t understand what you think is a problem.

Can you try to make a smaller example to demonstrate what your problem is please?

Something really simple and visual to explain what is the behavior that you try to avoid.

For what I understand the answer @neilcsmith gave you should work:
noise(xoff + xOffset, yoff + yOffset)
Where xOffset and yOffset are big enough constants.

Yeah I think that will work. Here’s a code demonstration of the error. Like I said before the error is that the negative numbers change into positive.

key q = blue mountains move left
key a = blue mountains move right

key e = green mountains move left
key d = green mountains move right

float noiseScale = 0.05;
float MinusNoiseX1 = -160;
float MinusNoiseX2 = -160;
float TileSize = 5;
float NoiseJumpMap = 255;
boolean[] Keys = new boolean[4];
float[] NoiseValues;

void setup() {
  fullScreen(P2D);
}

void draw() {
  background(52, 250, 215);
  
  KeysCommand();
  
  noiseDetail(48, 0.50);
  for (int x = 0; x < width / TileSize; x++) {
    
    float noiseVal = noise((x + MinusNoiseX1) * noiseScale, noiseScale);
    
    color Fill = lerpColor(color(noiseVal * NoiseJumpMap), color(0, 0, 100), 0.50);
    fill(Fill);
    stroke(Fill);
    rect(x * TileSize, noiseVal * NoiseJumpMap + 200, TileSize, height - noiseVal * NoiseJumpMap + 200);
  }
  //noiseDetail(24, 0.50);
  for (int x1 = 0; x1 < width / TileSize; x1++) {
    
    float noiseVal = noise((x1 + MinusNoiseX2) * noiseScale, noiseScale);
    
    color Fill = lerpColor(color(noiseVal * NoiseJumpMap), color(0, 255, 0), 0.50);
    fill(Fill);
    stroke(Fill);
    rect(x1 * TileSize, noiseVal * NoiseJumpMap + 350, TileSize, height - noiseVal * NoiseJumpMap + 350);
  }
  fill(255, 0, 0);
  text(MinusNoiseX1, 50, 50);
  fill(50, 70, 200);
  text(MinusNoiseX2, 50, 100);
  fill(0);
  strokeWeight(5);
  stroke(255);
  line(width/2, 0, width/2, height);
  fill(0);
  textSize(30);
  text("0", width/2, 50);
  text("Positive Numbers", width/1.5, 50);
  text("Negative Numbers", width/4, 50);
}

void keyPressed() {
  switch (key) {   
      case 'a' : 
        Keys[0] = true;
        break;
      case 'd' : 
        Keys[1] = true;
        break;
      case 'q' : 
        Keys[2] = true;
        break;
      case 'e' : 
        Keys[3] = true;
        break;
  }
}

void keyReleased() {
  switch (key) {   
      case 'a' : 
        Keys[0] = false;
        break;
      case 'd' : 
        Keys[1] = false;
        break;
      case 'q' : 
        Keys[2] = false;
        break;
      case 'e' : 
        Keys[3] = false;
        break;
  }
}

void KeysCommand() {
  float MovementSpeed = 100;
  
  if (Keys[0] == true) {
    MinusNoiseX1 -= noiseScale * MovementSpeed;
  }
  if (Keys[1] == true) {
    MinusNoiseX1 += noiseScale * MovementSpeed;
  }
  if (Keys[2] == true) {
    MinusNoiseX2 -= noiseScale * MovementSpeed;
  }
  if (Keys[3] == true) {
    MinusNoiseX2 += noiseScale * MovementSpeed;
  }
}

Ok so here what I got:

What’s the problem with that?

1 Like

What error?

It sounds like you’re not understanding how this function works. Can you offer a few input values, and where the output value isn’t what you expect, highlighting where you think there’s a problem?

1 Like

That’s weird did you change something in the code before you took the picture because here’s what I got.

As you can see on the left is a copy of the right but backwards.

Yes, it mirrors around zero. I assume @jb4x has altered the code as has been suggested multiple times - add a constant offset so the inputs to noise() are always positive.

Yeah I think so thanks for your help guys this will work.

That’s weird… I copy paste it and run it. That’s all.
I just changed the displayed value the same way you did when you edited your post.

Huh you must have plused a number into “MinusNoiseX1” to have stop the error.

I just tried it again and I don’t have your problem…
Which version of processing are you using?