hi,
i'm trying to figure out how to put this code in to a class, and then call it several times, based on random coordinates, where each class representation will act as it should.
Code:
float easing = 0.05; // Numbers 0.0 to 1.0
float division = 0.4; // line length in percentage
float x, y;
void setup() {
size(400, 400);
stroke(255);
}
void draw() {
background(0);
float targetX = (mouseX-(width/2))*division;
float targetY = (mouseY-(height/2))*division;
x += (targetX-x) * easing;
y += (targetY-y) * easing;
pushMatrix();
translate(width/2, height/2);
line(0, 0, x, y);
ellipse(x, y, 2, 2);
popMatrix();
}
now, i've been trying to do it like this, but it still doesn't seem to work. all the lines get attracted by the same coordinate - i want them to be attracted individually, based on their individual "0,0,0" ... don't know if it makes sense?
Code:
// GLOBAL -------------------------------------------------------
int boidsNumber = 50; // number of boids
float easeVar = 0.05; // Numbers 0.0 to 1.0
float divisionVar = 0.2; // line length in percentage
float horizontal = 80; // horizontal offset from screen (l/r)
float vertical = 100; // vertical offset from screen (t/b)
float x, y;
float[] ptsX = new float[boidsNumber]; // array for start-pt X
float[] ptsY = new float[boidsNumber]; // array for start-pt Y
Boid boid1;
// SETUP --------------------------------------------------------
void setup() {
size(500, 800);
smooth();
stroke(255);
ellipseMode(CENTER);
for (int i = 0; i < boidsNumber; i++) {
ptsX[i] = random(horizontal, float(width)-horizontal);
ptsY[i] = random(vertical, float(height)-vertical);
x = ptsX[i];
y = ptsY[i];
}
boid1 = new Boid(easeVar, divisionVar, x, y);
}
// DRAW ---------------------------------------------------------
void draw() {
background(0);
for (int i = 0; i < boidsNumber; i++) {
pushMatrix();
translate(ptsX[i], ptsY[i]);
boid1.display();
popMatrix();
}
}
// CLASS --------------------------------------------------------
class Boid {
float x_boid, y_boid, targetX, targetY, ease_boid, division_boid;
Boid(float ease_constr, float division_constr, float x_constr, float y_constr) {
ease_boid = ease_constr;
division_boid = division_constr;
x_boid = x_constr;
y_boid = y_constr;
}
void display() {
targetX = (mouseX-(x_boid/2))*division_boid;
targetY = (mouseY-(y_boid/2))*division_boid;
x += (targetX-x) * ease_boid;
y += (targetY-y) * ease_boid;
line(0, 0, x, y);
ellipse(x, y, 2, 2);
}
}
any ideas? thanks in advance.
/claus