xxxxxxxxxx
360
function Particle(x, y, team) {
zeroVector = createVector(0, 0);
this.pos = createVector(x, y);
this.vel = createVector(random(-5,5));
this.acc = createVector();
this.normalPoint = createVector();
this.target = createVector();
this.prodectLoc = createVector();
this.distance = createVector();
this.maxForce = 0.2;
this.maxSpeed = 0;
this.lifeStatus = true;
this.team = team;
this.color = color(0,0,0);
this.score = 0;
this.gate = false;
this.hit = false;
}
Particle.prototype.flock = function(p,n) {
teamMates = [];
opp = [];
var run = createVector();
var chase = createVector();
this.assignTeam();
this.findTeam(p);
this.border();
var sep = this.seperate(teamMates);
var ali = this.align(teamMates);
var coh = this.cohesion(teamMates);
if (this.team == 0){
run = this.run(opp);
}
if (this.team == 1){
chase = this.chase(opp,p);
this.checkHit(opp,p);
}
sep.mult(2 * 1);
ali.mult(1 * 1);
coh.mult(1 * 1);
run.mult(1.5 * 1);
chase.mult(1 * 1);
this.acc.add(sep);
this.acc.add(ali);
this.acc.add(coh);
this.acc.add(run);
this.acc.add(chase);
}
Particle.prototype.findTeam = function(p){
for (var i = 0; i < p.length; i++) {
var other = p[i];
if (this.team == other.team){
append(teamMates,other);
}else{
append(opp, other);
}
}
}
Particle.prototype.checkHit = function(others,p) {
for (var i = 0; i < others.length; i++) {
var other = others[i];
var distance = p5.Vector.dist(this.pos, other.pos);
if(distance < other.size){
this.hit = true;
}
if(this.gate == false){
if(this.hit == true){
this.score++;
other.lifeStatus = false;
this.gate = true;
}
}
if(this.hit == false){
this.gate = false;
}
}
}
Particle.prototype.run = function(others) {
var desiredseparation = others[0].size * 20;
var sum = createVector();
var count = 0;
for (var i = 0; i < others.length; i++) {
var other = others[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 zeroVector.copy();
}
}
Particle.prototype.seperate = function(others) {
var desiredseparation = this.size * 3;
var sum = createVector();
var count = 0;
for (var i = 0; i < others.length; i++) {
var other = others[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 zeroVector.copy();
}
}
Particle.prototype.chase = function(others) {
var range = 200;
var target = createVector();
var sum = zeroVector.copy();
var count = 0;
for (var i = 0; i < others.length; i++) {
var other = others[i];
var d = p5.Vector.dist(this.pos, other.pos);
if (d < range) {
sum.add(other.pos);
count++;
target = other;
}
}
if (count > 0) {
sum.div(count);
var steer = this.seek(sum);
return steer;
} else {
return zeroVector.copy();
}
}
Particle.prototype.align = function(others) {
var range = 50;
var sum = zeroVector.copy();
var count = 0;
for (var i = 0; i < others.length; i++) {
var other = others[i];
var d = p5.Vector.dist(this.pos, other.pos);
if ((d > 0) && (d < range)) {
sum.add(other.vel);
count++;
}
}
if (count > 0) {
sum.div(others.length);
sum.setMag(this.maxSpeed);
var steer = p5.Vector.sub(sum, this.vel);
steer.limit(this.maxForce);
return steer;
} else {
return zeroVector.copy();
}
}
Particle.prototype.cohesion = function(others) {
var range = 10;
var sum = zeroVector.copy();
var count = 0;
for (var i = 0; i < others.length; i++) {
var other = others[i];
var d = p5.Vector.dist(this.pos, other.pos);
if ((d > 0) && (d < range)) {
sum.add(other.pos);
count++;
}
}
if (count > 0) {
sum.div(others.length);
sum.setMag(this.maxSpeed);
var steer = p5.Vector.sub(sum, this.vel);
steer.limit(this.maxForce);
return this.seek(sum);
} else {
return zeroVector.copy();
}
}
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.findPredict = function(p){
var predict = p
predict.normalize();
predict.mult(50);
return p5.Vector.add(this.pos,predict);
}
Particle.prototype.flee = function(t){
var dir = p5.Vector.sub(t,this.pos);
var d = dir.mag();
if(d < 50){
var speed = this.maxSpeed;
dir.mult(-1);
var steer = p5.Vector.sub(dir,this.vel);
steer.limit(this.maxForce);
return steer;
}else{
return createVector(0,0);
}
}
*/
Particle.prototype.seek = function(t) {
var dir = p5.Vector.sub(t, 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.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;
var ROTATION_ANGLE = this.getTheta() + PI*3/2;
var ARC_RADIUS = 35 * this.size;
var ARC_ANGLE = PI/3;
inVis = collidePointArc(mouseX,mouseY,this.pos.x,this.pos.y,ARC_RADIUS,ROTATION_ANGLE,ARC_ANGLE);
return inVis;
}
Particle.prototype.assignTeam = function(){
if (this.team == 0){
this.color = color(255,0,0);
this.maxSpeed = 5;
this.size = 8;
}
if (this.team == 1){
this.color = color(0,0,255);
this.maxSpeed = 3;
this.size = 12
}
}
Particle.prototype.show = function(){
theta = this.getTheta();
var ROTATION_ANGLE = this.getTheta() + PI*3/2;
var ARC_RADIUS = 35 * this.size;
var ARC_ANGLE = PI/3;
fill(this.color);
stroke(0);
push();
translate(this.pos.x,this.pos.y);
rotate(theta);
beginShape();
vertex(0, -this.size*2);
vertex(-this.size, this.size*2);
vertex(this.size, this.size*2);
endShape(CLOSE);
noFill();
if(this.team == 0){
text(this.lifeStatus,0,30);
}
if(this.team == 1){
text(this.score,0,20);
}
//stroke(0,0,0,50);
//rotate(ROTATION_ANGLE-theta);
//arc(0,0,2 * ARC_RADIUS,2 * ARC_RADIUS, -ARC_ANGLE / 2, ARC_ANGLE / 2, PIE);
pop();
}