I am fairly new to P5.js and am trying to make an entity of lines move around the screen and rotate (like a twirling motion). I have the array of lines created but I can’t figure out how to make a statement that will apply to all of them as one unit. Also, as a side note, I have set the x1, x2, y1, y2 to loop and increase or decrease to create movement but as the program runs, the lines grow apart (as they keep adding which expands the location parameters). Is there a way to set a boundary? I attempted to set a boundary (commented out in the move section) but it didn’t work. I’m afraid that with my limited knowledge so far, I don’t know of any solutions, but I would love to learn.
My code so far:
let bubbles = [];
function setup() {
createCanvas(600, 600, WEBGL);
for (let i = 0; i < 30; i++) {
bubbles[i] = new Bubble();
}
}
function draw() {
background(255, 255, 255, bc);
for (i = 0; i < bubbles.length; i++) {
bubbles[i].display();
bubbles[i].move();
}
}
class Bubble {
constructor() {
this.x1 = random(-150, -160);
this.y1 = random(50, 100);
this.x2 = random(-135, -175);
this.y2 = 150;
this.speed = 1;
}
display() {
let r = random(100, 255);
let g = random(0, 50);
let b = random(0, 50);
stroke(0);
line(this.x1, this.y1, this.x2, this.y2);
}
move() {
// if (this.x1<-150 && this.x1>-160){
this.x1 += random(-this.speed, this.speed);
// }
//if (this.y1<100 && this.y1>50){
this.y1 += random(-this.speed, this.speed);
// }
//if (this.x2<-135 && this.x2>-175){
this.x2 += random(-this.speed, this.speed);
// }
// if (this.y2>150 && this.y2<150){
this.y2 += random(-this.speed, this.speed);
// }
}
}