xxxxxxxxxx
95
function Particle(x,y){
this.pos = createVector(x,y);
this.vel = createVector();
this.acc = createVector();
this.size = 8;
this.maxSpeed = 4;
this.maxForce = 0.3;
this.poly = [];
}
Particle.prototype.findTarget = function(x,y){
this.target = createVector(x,y);
}
Particle.prototype.behaviors = function(){
var arrive = this.arrive(this.target);
arrive.mult(0.3);
this.acc.add(arrive);
}
Particle.prototype.collision = function(){
var col = false;
col = collidePolyPoly([(0,-this.size*2,-this.size,this.size*2,this.size,this.size*2)],[(30,200),(85,200),(85,750),(30,750)]);
if(col){
console.log('in')
}
}
Particle.prototype.arrive = function(t){
var dir = p5.Vector.sub(t,this.pos);
var d = dir.mag();
var speed = this.maxSpeed;
if (d < 100){
speed = map(d, 0, 100, 0, this.maxSpeed);
}
dir.setMag(speed);
var steer = p5.Vector.sub(dir,this.vel);
steer.limit(this.maxForce);
return steer;
}
Particle.prototype.update = function(){
this.pos.add(this.vel);
this.vel.add(this.acc);
this.acc.mult(0);
}
Particle.prototype.getTheta = function(){
var theta = this.vel.heading() + PI/2;
return theta;
}
Particle.prototype.inVision = function(other){
var inVis = false;
ROTATION_ANGLE = this.getTheta() + PI*3/2;
ARC_RADIUS = 35 * this.size;
ARC_ANGLE = PI/3;
inVis = collidePointArc(mouseX,mouseY,this.pos.x,this.pos.y,ARC_RADIUS,ROTATION_ANGLE,ARC_ANGLE);
}
Particle.prototype.collision = function(other){
//console.log(this.poly,other);
var hit = collidePolyPoly(this.poly,other,true);
return hit;
}
Particle.prototype.create = function(){
append(this.poly,createVector(0, -this.size*2));
append(this.poly,createVector(-this.size, this.size*2));
append(this.poly,createVector(this.size, this.size*2));
}
Particle.prototype.show = function(){
theta = this.getTheta();
fill(175);
stroke(0);
push();
translate(this.pos.x,this.pos.y);
rotate(theta);
beginShape();
for(var i = 0; i < this.poly.length; i++){
vertex(this.poly[i].x,this.poly[i].y);
}
endShape(CLOSE);
noFill();
rotate(ROTATION_ANGLE-theta);
arc(0,0,2 * ARC_RADIUS,2 * ARC_RADIUS, -ARC_ANGLE / 2, ARC_ANGLE / 2, PIE);
circle(0,0,this.size*3);
pop();
}