Like William Miller says, What do you want them to do instead of bounce off? You could simply keep them apart by the radius + the other radius.
https://jsfiddle.net/EthanHermsey/n906fdmh/21/
collide() {
for (let p of particles) {
if (this != p && p5.Vector.dist(this.pos, p.pos) < this.r + p.r) {
//a vector pointing from the other point to this point
let directionVector = p5.Vector.sub( this.pos, p.pos );
//set the magnitude of the vector to the length of both radiusses
directionVector.setMag( this.r + p.r );
//now set this.pos at a fixed distance from p.pos. In the same direction as it was before.
this.pos = p.pos.copy();
this.pos.add( directionVector );
}
}
}
Also I moved the 'this != p' to the front, that's a little bit faster because the distance calculation doesn't have to be done first.
That distance function is pretty slow anyway because of the square root calculation, you could try and use the magSq() function, like this;
collide() {
for (let p of particles) {
if ( p == this ) continue; //go to the next particle
//a vector pointing from the other point to this point
let directionVector = p5.Vector.sub( this.pos, p.pos );
//pow( this.r + p.r, 2 ) is the same as ( this.r + p.r ) * ( this.r + p.r )
if ( this != p && directionVector.magSq() < pow( this.r + p.r, 2 ) ) {
//set the magnitude of the vector to the length of both radiusses
directionVector.setMag( this.r + p.r );
//now set this.pos at a fixed distance from p.pos. In the same direction as it was before.
this.pos = p.pos.copy();
this.pos.add( directionVector );
}
}
}