xxxxxxxxxx
104
var particles = []; // various particles are stored in this array
function setup(){
var width = windowWidth;
var height = windowHeight;
createCanvas(width, height);
p = new particle();
n = new netForce();
//particles.push(p);
}
function mouseClicked(){
p = new particle(); // when user clicks a new particle is formed
particles.push(p); // particle is added to the array
}
function draw(){
background(200);
for(i=0;i<particles.length;i++){
particles[i].main();
n.dir(particles[i]);
}
}
function particle(){
this.maxvelocity =5; ///
this.maxforce = 0.5; //
this.velocity = createVector(0,0);
this.location = createVector(random(width),random(height)); // random initial location for each object
this.targetMouse = 1; //1 = yes 0 = no(no results in mutual attraction which is borked)
this.target = createVector();
this.dir = createVector();
this.origin = createVector(0,0);
this.acceleration = createVector(0,0);
this.display = function(){ // To display the object
circle(this.location.x,this.location.y,100);
}
this.acquireDir = function(direction){ // acquires the direction to the target
this.dir = direction.sub(this.location);
this.dir.normalize();
this.addForce(this.dir); // Pass the direction vector to be added to the acceleration
}
this.motion = function(){ // this function is responsible for providing the force
this.velocity.add(this.acceleration);
this.velocity.limit(this.maxvelocity);
this.acceleration.mult(0);
}
this.addForce = function(force){
this.acceleration.add(force);
}
this.update = function(){ // updates the location of the object
this.location.add(this.velocity);
}
this.main = function(){
this.display();
this.motion();
this.update();
}
}
function netForce(){
this.dir = function(){
if(particles[i].targetMouse == true){ // This if statement works as expectec
//particles[i].target = createVector(mouseX,mouseY);
this.mouse = createVector(mouseX,mouseY); // The target is the mouse only if targetMouse =1
//particles[i].acquireDir(this.mouse);
particles[i].target = this.mouse;
particles[i].acquireDir(particles[i].target);
}
if(particles[i].targetMouse == false){ // Mutual repulsion // this doesn't work
for(j=0;j<particles.length;j++){
if(i!=j){
particles[i].acquireDir(particles[j].location);
}
}
}
}
}