Emergent pattern A.K.A. how to spy on your neighbor
in
Programming Questions
•
2 years ago
Hi all!
I've been working on this project for a while now after I read a book called Emergence: The Connected Lives of Ants, Brains, Cities, and Software... Super cool text...
Without going too much in details I want to create a series of identical objects that behave following few simple rules, while interacting with each other...A sort of interconnected frame of objs that observe and react on each other...To make the idea more clear I made a small viz of the logic behind the code...check the png i posted here...
If you look at my coding you'll see that I:
1) created an array of objects called lampList
2) told to two of them to Display(): this means thy will simply see the dist() btw center & mouseX/Y and use that to determine the transparency.
3)assigned the third obj the doOpposite(): it will see what is the condition of the first obj and do the opposite!
The thing I can't seem to do is apply the same three behavior to ALL obj! All object should be able to:
1)read the distance to the mouseX/Y and behave consequently
2)see what is the closest obj doing, and do the opposite
I added some more comment to the coding to explain what I'm doing...
Hopefully if all works, strange cool things should happen...
I've been working on this project for a while now after I read a book called Emergence: The Connected Lives of Ants, Brains, Cities, and Software... Super cool text...
Without going too much in details I want to create a series of identical objects that behave following few simple rules, while interacting with each other...A sort of interconnected frame of objs that observe and react on each other...To make the idea more clear I made a small viz of the logic behind the code...check the png i posted here...
If you look at my coding you'll see that I:
1) created an array of objects called lampList
2) told to two of them to Display(): this means thy will simply see the dist() btw center & mouseX/Y and use that to determine the transparency.
3)assigned the third obj the doOpposite(): it will see what is the condition of the first obj and do the opposite!
The thing I can't seem to do is apply the same three behavior to ALL obj! All object should be able to:
1)read the distance to the mouseX/Y and behave consequently
2)see what is the closest obj doing, and do the opposite
I added some more comment to the coding to explain what I'm doing...
- int lastMove;
float stealTransp;
float scaleRange = 255;
float diff;
ArrayList <Lamp> lampList;
void setup() {
size(800, 600);
smooth();
//create the arrayList and create 3 Lamp OBJ...
lampList = new ArrayList();
lampList.add(new Lamp(200,150));
lampList.add(new Lamp(500,300));
lampList.add(new Lamp(300,350));
}
void draw() {
background(0);
//apply behaviours to each array obj
for(int i=0;i<lampList.size();i++) {
lampList.get(0).display();
lampList.get(2).display();
lampList.get(1).doOpposite();
}
}
void mouseMoved() {
lastMove = millis();
}
class Lamp {
float x;
float y;
int ellipseWidth;
float transp;
color c;
Lamp(float tempx, float tempy) {
x = tempx;
y = tempy;
ellipseWidth = 50;
c = color(random(255),random(255),random(255));
}
void display() {
ellipseMode(CENTER);
/* here is the trick! calculate the dist between center of the obj
and mouse X & Y. costrain the values between 0 and 250 and send it to
the fill() */
transp = dist(x, y, mouseX, mouseY);
transp = constrain(transp,0,255);
fill (c,transp);
ellipse(x, y, ellipseWidth, ellipseWidth);
}
void doOpposite() {
/* here is the first probem which is that I managed to only follow a specific
obj, and not the closest...So here I tell the second to check the first */
stealTransp = lampList.get(0).transp;
diff = scaleRange - stealTransp;
ellipseMode(CENTER);
fill (c, diff);
ellipse(x, y, ellipseWidth, ellipseWidth);
}
}
Hopefully if all works, strange cool things should happen...
1