lundi 21 juin 2021

How to use random over time in Processing

I am trying to change the positions of some circled objects with random();. My problem is that I either can initialise random positions in setup()… (not being able to overwrite them in draw()… or initialise them in draw()… so that everything happens way too fast… I don't want to slow down frameRate() either... So I thought writing an extra function and only calling it every 30 frames would solve it – but then my circled objects are handled as one and are drawn in the same spot... what am I missing? I think the logic is a bit tricky? Thank you for any kind of help!

Circle[] circles = new Circle[3];    
float rX;
float rY;    
float posX;

void setup () {
  size(540, 960);
  randomizePositions();
}
   
void randomizePositions() {
  rX = random(width);
  rY = random(height);
}
    
void draw() {
  background(#c1c1c1);
  for (int i = 0; i <circles.length; i++) { 
    circles[i] = new Circle(rX, rY);
  }
    
  for (int i = 0; i < circles.length; i++) {
    circles[i].display();
  }

  if (frameCount % 30 == 0) {
    randomizePositions();
  }
}

and this is my Object:

class Circle {

  int size;
  float x;
  float y;
 
  Circle(float tempX, float tempY) {
    size = width/10;
    x = tempX;
    y = tempY;
  }

  void display() {
    float wave = sin(radians(frameCount*10));
    for (int i = 0; i < 10 * wave; i++) {
      noFill();
      stroke(0);
      strokeWeight(20);
      ellipse(x, y, i * size, i * size);
    }
  }
}



Aucun commentaire:

Enregistrer un commentaire