xxxxxxxxxx
171
function Particle(x,y){
this.pos = createVector(x,y);
this.vel = createVector();
this.acc = createVector();
this.normalPoint = createVector();
this.target = createVector();
this.prodectLoc = createVector();
this.distance = createVector();
this.size = 20;
this.maxSpeed = 5;
this.maxForce = 1;
}
Particle.prototype.behaviors = function(p){
var arrive = this.arrive();
var flee = this.seperate(p);
arrive.mult(0.5);
flee.mult(1);
this.acc.add(arrive);
this.acc.add(flee);
this.border();
}
Particle.prototype.predict = function(path){
var record = 1000000000000;
this.findPredict();
for(var i = 0; i < path.points.length; i++){
this.a = path.get(i);
this.b = path.get((i + 1) % path.points.length);
var normal = this.findN(path);
let dir = p5.Vector.sub(this.b, this.a);
// If it's not within the line segment, consider the normal to just be the end of the line segment (point b)
//if (da + db > line.mag()+1) {
if (normal.x < min(this.a.x, this.b.x) || normal.x > max(this.a.x, this.b.x) || normal.y < min(this.a.y, this.b.y) || normal.y > max(this.a.y, this.b.y)) {
normal = this.b.copy();
// If we're at the end we really want the next line segment for looking ahead
this.a = path.points[(i + 1) % path.points.length];
this.b = path.points[(i + 2) % path.points.length]; // Path wraps around
dir = p5.Vector.sub(this.b, this.a);
}
distance = dist(this.predictLoc.x,this.predictLoc.y,normal.x,normal.y);
if (distance < record){
record = distance;
this.normalPoint = normal;
this.distance = distance;
dir.normalize();
// This is an oversimplification
// Should be based on distance to path & velocity
dir.mult(25);
this.target = normal.copy();
this.target.add(dir);
}
}
return this.distance
}
Particle.prototype.velGet = function(){
var n = createVector(0,0);
p5.Vector.add(n,this.vel)
return p5.Vector.add(n,this.vel)
}
Particle.prototype.findPredict = function(){
var predict = this.velGet();
predict.normalize();
predict.mult(50);
this.predictLoc = p5.Vector.add(this.pos,predict);
}
Particle.prototype.findN = function(path){
var ap = p5.Vector.sub(this.predictLoc,this.a);
var ab = p5.Vector.sub(this.b,this.a);
ab.normalize();
ab.mult(ap.dot(ab));
return p5.Vector.add(this.a,ab);
}
Particle.prototype.findTarget = function(t){
this.target = t;
}
Particle.prototype.border = function(){
//teleports particle
if(this.pos.x < -10){
this.pos.x = wid-10;
}
if(this.pos.x > wid+10){
this.pos.x = -10;
}
if(this.pos.y > hit+10){
this.pos.y = -10;
}
if(this.pos.y < -10){
this.pos.y = hit-10;
}
}
Particle.prototype.arrive = function(){
var dir = p5.Vector.sub(this.target,this.pos);
var d = dir.mag();
var speed = this.maxSpeed;
dir.setMag(speed);
var steer = p5.Vector.sub(dir,this.vel);
steer.limit(this.maxForce);
return steer;
}
Particle.prototype.seperate = function(t){
var desiredseparation = this.size*2;
var sum = createVector();
var count = 0;
for(var i = 0; i < t.length; i++){
var other = t[i];
var distance = p5.Vector.dist(this.pos,other.pos);
if ((distance > 0) && (distance < desiredseparation)){
var opposite = p5.Vector.sub(this.pos,other.pos);
opposite.normalize();
opposite.div(distance);
sum.add(opposite);
count++;
}
}
if(count > 0){
sum.div(count);
sum.setMag(this.maxSpeed);
var steer = p5.Vector.sub(sum,this.vel);
steer.limit(this.maxForce);
return steer
}else{
return createVector(0,0);
}
}
Particle.prototype.update = function(){
this.pos.add(this.vel);
this.vel.add(this.acc);
this.acc.mult(0);
}
Particle.prototype.show = function(){
fill(175);
stroke(0);
circle(this.pos.x,this.pos.y,this.size);
}