Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HMath.removeTempSeed() is not resetting usingTempSeed #161

Open
mrd0ll4r opened this issue Nov 10, 2018 · 2 comments
Open

HMath.removeTempSeed() is not resetting usingTempSeed #161

mrd0ll4r opened this issue Nov 10, 2018 · 2 comments

Comments

@mrd0ll4r
Copy link

... which causes any subsequent calls to removeTempSeed to reset the seed to the one produced at the very start. See https://github.com/hype/HYPE_Processing/blob/lib_staging/src/main/java/hype/HMath.java#L442

@mrd0ll4r mrd0ll4r changed the title HMath.removeTempSeed() is not resetting usingTempSeed HMath.removeTempSeed() is not resetting usingTempSeed Nov 10, 2018
@DonoG
Copy link
Contributor

DonoG commented Nov 28, 2018

Can you provide an example of what you are trying to achieve with HMath.removeTempSeed(), and why its not working?

@mrd0ll4r
Copy link
Author

Assume you're using a pair of HMath.tempSeed() and HMath.removeTempSeed() in your code. tempSeed will save a seed to return to later, but only on the first ever call made to it, because removeTempSeed doesn't reset usingTempSeed.

Concrete problem: I have a bunch of things I want to color with an HColorPool, randomly, but not-changing per frame. I take the index of the thing I'm coloring, multiply it by a large-ish constant to get some random mapping, then call HColorPool.getColor(int). Additionally, I'm using Perlin noise to do whatever, and want to re-seed the noise on a keypress. For that, I generate a random value with PApplet.random(Long.MAX_VALUE). But, because the random seed is not correctly reset in HMath, which is used by HColorPool.getColor(int), subsequent calls to seed the noise field will always use the same seed.

In the example, observe how pressing 'g' multiple times behaves differently based on whether or not the color pool is being used.

Code:

import hype.*;
import hype.extended.behavior.*;
import hype.extended.colorist.*;
import hype.extended.layout.*;

void setup() {
  size(640, 640);
  H.init(this).background(#C0C0C0);

  pool = new HDrawablePool(100).autoAddToStage().add(new HRect(10).anchorAt(H.CENTER).noStroke().fill(0xFF8736A3)).layout(new HGridLayout(10, 10).startLoc(100, 100).spacing(50, 50)).requestAll();

  c = new HColorPool(#713131,#317131,#313171);
}

private HColorPool c;
private boolean withColors;
private HDrawablePool pool;

void draw() {
  int i = 0;
  for (HDrawable o : pool) {
    // This is the issue: c.getColor(int) calls HMath.tempSeed with the given value, then resets the temp seed with HMath.removeTempSeed.
    // However, because removeTempSeed doesn't reset HMath.usingTempSeed, the next call to HMath.tempSeed will not overwrite the saved resetSeedValue.
    // This then makes any call to PApplet.random() afterwards deterministic.
    // In this case: Try to generate new noise seeds with coloring enabled.
    // Because the call to random() produces the same value for each frame drawn, well... you see.
    if (withColors)
      o.fill(c.getColor(i*541233));
    else
      o.fill(0xFF8736A3);

    o.size(50f*noise(o.x()*0.01f, o.y()*0.01f, frameCount*0.01f));

    i++;
  }

  H.drawStage();
}

void keyPressed() {
  switch (key) {
  case 'c':
    withColors = !withColors;
    break;
  case 'g':
    noiseSeed((long) random(Long.MAX_VALUE));
    break;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants