particle_system/particle.pde

99 lines
2.4 KiB
Plaintext

class Particle{
PVector pos;
PVector speed;
int col;
int circular_nb;
int circular_id; // the id in the circular buffer
PVector[] circular_pos;
boolean draw_previous;
float slow_distance;
// pour pouvoir faire les trainer il faudrait retenir les position precedente
// avoir un beffer tournant
// retenir l'index actuel
void init_circular(int nb_previous, boolean previous_drawing){
draw_previous = previous_drawing;
circular_nb = nb_previous;
circular_id = 0;
circular_pos = new PVector[circular_nb];
for (int i = 0; i < circular_pos.length; i++){
circular_pos[i] = new PVector(pos.x, pos.y, 0);
}
}
void init_particle(){
pos = new PVector(random(50, 750), random(50, 70));
speed = new PVector(0, 0, 0);
col = getColInt(255, 255, 255);
slow_distance = 0.03;
}
Particle(){
init_particle();
init_circular(1, false);
}
Particle(int nb_previous){
init_particle();
init_circular(nb_previous, true);
}
void update_position(Attractor[] attractors){
for (Attractor att : attractors){
if (!att.is_active)
continue;
PVector d = vecSub(att.pos, pos);
float dist = sqrt(d.x * d.x + d.y * d.y);
float factor = att.weight * 1.0 / (dist + 10);
normalize(d);
speed.x += d.x * factor;
speed.y += d.y * factor;
float cross = cross_product(getNormalized(speed), d);
// limitation de l'eloignement
if (cross < 0){
speed.x *= 1.0 - (slow_distance * abs(cross));
speed.y *= 1.0 - (slow_distance * abs(cross));
}
pos.x += speed.x;
pos.y += speed.y;
circular_id = (circular_id + 1) % circular_pos.length;
circular_pos[circular_id].x = float(int(pos.x));
circular_pos[circular_id].y = float(int(pos.y));
}
}
void draw(){
//println("hello im drowing:", pos);
if (draw_previous){
int id;
Point[] previous_pos = new Point[circular_pos.length];
for (int i = 0; i < circular_pos.length; i++){
id = (circular_id - i + circular_pos.length) % circular_pos.length;
previous_pos[i] = new Point(int(circular_pos[id].x), int(circular_pos[id].y), getColInt(0, 255, 0));
}
//println("plop");
laser_frame.add_line(previous_pos);
}
else
laser_frame.add_line(create_point(pos, getColInt(0, 255, 0)));
}
}
Point[] create_point(PVector pos, int col){
Point[] point = new Point[2];
point[0] = new Point(int(pos.x), int(pos.y), col);
point[1] = new Point(int(pos.x+1), int(pos.y+1), col);
return point;
}