xxxxxxxxxx
95
class particle{
constructor(x,y,cell){
this.pos = createVector(x,y);
this.vec = createVector(random(-1,1),random(-1,1));
this.size = 10;
this.curCell = cell;
this.prevCell = cell;
this.nearby = [];
this.viewRange = 40;
}
//update cell
updateCell(cell){
this.curCell = cell;
}
//find particles in nearby cells
findNearby(){
let possibleNearby = []; //keep track of all particles nearby
let range = 1; //how many cells it should check. 1 => 3 x 3 around cell
//make it wrap around
for(let i = this.curCell.x-range; i <= this.curCell.x + range; i++){
for(let j = this.curCell.y-range; j <= this.curCell.y + range; j++){
//edge case!!!
//if(i < 0){
//}
for(let g of grid[i][j]){
append(possibleNearby,g);
}
}
}
this.nearby = possibleNearby;
}
connectNearby(){
for(let n of this.nearby){
let d = dist(this.pos.x,this.pos.y,n.pos.x,n.pos.y);
if(d < this.viewRange){
stroke(255,0,255,50);
line(this.pos.x,this.pos.y,n.pos.x,n.pos.y);
}
}
}
update(){
//check if particle changed cells
if(this.curCell.x != this.prevCell.x || this.curCell.y != this.prevCell.y){
changedCell(this);
this.prevCell = this.curCell;
}
//edge check
if(outOfBorder(this.pos.x,this.pos.y,this.size)){
this.vec.mult(-1);
}
this.pos.add(this.vec);
}
show(){
noStroke();
//fill(255,0,0);
//visualize 2x2 grid
// if(this.curCell.x == 1 && this.curCell.y == 0){
// fill(255,0,0)
// }else if(this.curCell.x == 0 && this.curCell.y == 0){
// fill(0,255,0)
// }else if(this.curCell.x == 0 && this.curCell.y == 1){
// fill(0,0,255)
// }else if(this.curCell.x == 1 && this.curCell.y == 1){
// fill(0,255,255)
// }else{
// fill(0);
// }
//let clr = color(this.curCell.x*20,this.curCell.y*10,200);
//fill(clr);
circle(this.pos.x,this.pos.y,this.size);
}
}