Noise
in
Programming Questions
•
2 years ago
Hi,
I've been trying to do a sketch where I use PVector and noise() to move n agents around on the screen ... but I can't seem to get it to work properly.
Could anyone help me to see what's wrong with the code? It seems like the x and y increase/decrease are the same, which creates a 45 degree line where the ellipse's are drawn instead of using the entire canvas ... what am I doing wrong here?
Thanks in advance!
/Claus
- // script written by Claus Rytter Bruun de Neergaard, October 2011
- // GLOBAL VARIABLES ///////////////////////////////
- int n = 100; // number of agents
- float increase = 0.01; // maximum agent increase each frame
- Agent[] agents = new Agent[n];
- // SETUP //////////////////////////////////////////
- void setup() {
- size(670, 500);
- ellipseMode(CENTER);
- fill(255);
- stroke(255, 200);
- strokeWeight(4);
- // create new object for each slot in array
- for (int i = 0; i < agents.length; i++) {
- agents[i] = new Agent();
- }
- }
- // DRAW ///////////////////////////////////////////
- void draw() {
- background(0);
- // call functions for all objects in array
- for (int i = 0; i < agents.length; i++) {
- agents[i].update();
- agents[i].display();
- }
- }
- // CLASS //////////////////////////////////////////
- class Agent {
- PVector loc, vel;
- float inc;
- Agent() {
- loc = new PVector(random(0, width), random(0, height));
- vel = new PVector(0, 0);
- inc = increase;
- }
- void update() {
- /*loc.x = noise(vel.x) * width;
- loc.y = noise(vel.y) * height;
- vel.x = vel.x + inc;
- vel.y = vel.y + inc;*/
- vel.x = vel.x + inc;
- vel.y = vel.y + inc;
- loc.x = noise(vel.x) * width;
- loc.y = noise(vel.y) * height;
- }
- void display() {
- point(loc.x, loc.y);
- }
- }
1