xxxxxxxxxx
122
function Slime(x, y, m, p) {
//set up pos,vel
this.pos = createVector(x, y);
this.vel = p5.Vector.random2D();
this.vel.setMag(2);
this.acc = createVector(0, 0);
this.maxSpeed = 1;
this.maxForce = 0.05;
this.mass = m;
this.perception = p;
this.history = [];
this.update = function() {
this.vel.add(this.acc);
this.vel.limit(this.maxSpeed)
this.pos.add(this.vel);
//this.acc.mult(0);
}
//display (color, ellipse)
this.display = function() {
noStroke();
fill(frameCount %360, satSlider.value(), 100);
ellipse(this.pos.x, this.pos.y, this.mass);
}
//new function to update mass
this.updateMass = function(newMass){
this.mass = newMass;
}
this.updatePerception = function(newPerception){
this.perception = newPerception;
}
this.targetMouse = function (x, y) {
let target = createVector(x, y);
let desired = p5.Vector.sub(target, this.pos);
desired.setMag(this.maxSpeed);
let steering = p5.Vector.sub(desired, this.vel);
steering.limit(this.maxForce);
this.applyForce(steering);
}
this.applyForce = function(force) {
this.acc.add(force);
}
this.seperate = function(slimes){
let perceptionRadius = this.perception;
let steering = createVector();
let total = 0;
for (let other of slimes) {
let d = dist(this.pos.x, this.pos.y, other.pos.x, other.pos.y);
if (other != this && d < perceptionRadius) {
let diff = p5.Vector.sub(this.pos, other.pos);
diff.div(d * d);
steering.add(diff);
total++;
}
}
if (total > 0) {
steering.div(total);
steering.setMag(this.maxSpeed);
steering.sub(this.vel);
steering.limit(this.maxForce);
}
this.applyForce(steering);
}
// this.edges = function() {
// if (this.pos.x > width) {
// this.vel = p5.Vector.random2D(PI, TWO_PI);
// }
// if (this.pos.x < 0) {
// this.vel = p5.Vector.random2D(0, PI);
// }
// if (this.pos.y > height) {
// this.vel = p5.Vector.random2D(HALF_PI, -HALF_PI);
// }
// if (this.pos.y < 0) {
// this.vel = p5.Vector.random2D(-HALF_PI, HALF_PI)
// }
// }
this.edges = function() {
if (this.pos.x > width) {
this.pos.x = 0;
} else if (this.pos.x < 0) {
this.pos.x = width;
}
if (this.pos.y > height) {
this.pos.y = 0;
} else if (this.pos.y < 0) {
this.pos.y = height;
}
}
//Movement of Slimes
// var movement = p5.Vector.random2D();
// movement.mult(0.9); //(connects previous & next cell)
// this.pos.add(movement);
}