Hello!
After watching the coding train for a while I decided to download processing and start learning how to create my own programs. I started last week.
Currently I am working on a infection simulation. I am trying to get my “person” class to recognize when it’s been infected then have a waiting period where it then becomes recovered. To do this I have two boolean variables that are both false at the start. I then check the position of a “person” against every other person and if they touch and the other is infected then they become infected=true. Now I would like to have a “timer” start for them to recover. The not infecting other people is working I do believe however, they are not changing their color. I will post the full code below. The main parts that pertain to this are probably the infected and recover functions in the person class.
Any help is greatly appreciated!
Thanks
int population = 100;
person[] People = new person[population];
void setup() {
size(1200, 600);
for (int i = 0; i < population; i++) {
People[i] = new person(i, People);
}
}
void draw() {
background(0);
for (person p : People) {
p.move();
p.display();
p.initialize();
p.infected(People);
p.recover();
}
}
class person {
PVector velocity = new PVector(random(-1, 1), random(-1, 1));
int size = 10;
int bsize = 100;
float x1 = random(0+bsize/2, (width/2)-bsize/2);
float y1 = random(0+bsize/2, height-bsize/2);
float x2 = random(x1-bsize/3, x1+bsize/3);
float y2 = random(y1-bsize/3, y1+bsize/3);
boolean infected;
boolean recovered = false;
person[] others;
int num;
float time;
person(int nin, person[] oin) {
others = oin;
num = nin;
}
void initialize() {
if (num < 2) {
infected = true;
}
}
void barrier() {
noFill();
stroke(255, 0, 0);
circle(x1, y1, bsize);
}
void display() {
if (infected) {
stroke(255, 0, 0);
fill(255, 0, 0);
ellipse(x2, y2, 10, 10);
} else if (recovered) {
stroke(220, 220, 220);
fill(220, 220, 220);
ellipse(x2, y2, 10, 10);
} else {
stroke(255);
fill(255);
ellipse(x2, y2, 10, 10);
}
}
void move() {
float distance = dist(x1, y1, x2, y2);
if (distance > bsize/2) {
velocity = velocity.mult(-1);
}
x2 = x2 + velocity.x;
y2 = y2 + velocity.y;
}
void infected(person[] others) {
for (person other : others) {
float d = dist(x2, y2, other.x2, other.y2);
//println(d);
if ( d < size && other.infected & !recovered) {
infected = true;
time = millis();
}
}
}
void recover() {
int wait = 10000;
if (millis() - time >= wait) {
infected = false;
recovered = true;
}
}
}