xxxxxxxxxx
340
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.team = team;
this.color = color(0,0,0);
//this.inVisionColor = color(255,255,255,50);
}
Particle.prototype.flock = function(p,n) {
teamMates = [];
opp =[];
var run = createVector();
var chase = createVector();
this.assignTeam();
this.border();
for (var i = 0; i < p.length; i++) {
var other = p[i];
if (this.team == other.team){
append(teamMates,other);
}else{
append(opp, other);
}
}
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);
}
sep.mult(2 * n);
ali.mult(1 * n);
coh.mult(1 * n);
run.mult(3 * 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.run = function(others) {
var desiredseparation = this.size * 15 / 2;
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);
var vision = this.inVision(other);
if ((distance > 0) && ((vision) || (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 * 2;
if (this.team == 0){
desiredseparation = this.size * 2;
}
if (this.team == 1){
desiredseparation = this.size * 7;
}
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 target = createVector(0,0);
var sum = createVector(0,0);
var count = 0;
for (var i = 0; i < others.length; i++) {
var other = others[i];
var d = p5.Vector.dist(this.pos, other.pos);
var vision = this.inVision(other);
//if ((d > 0) && ((vision) || (this.size*15/2))) {
if(vision){
sum.add(other.vel);
count++;
target = other;
}
}
if (count > 0) {
line(this.pos.x,this.pos.y,target.pos.x,target.pos.y);
var steer = this.seek(target.pos);
return steer;
} else {
return zeroVector.copy();
}
}
Particle.prototype.align = function(others) {
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);
var vision = this.inVision(other);
if ((d > 0) && ((d < this.size*15/2) || (vision))) {
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.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.sound = function(){
/*
Plays sound when enemy seen
Inputs: inVision = true
Outputs: play sound
*/
}
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(other.pos.x,other.pos.y,this.pos.x,this.pos.y,ARC_RADIUS,ROTATION_ANGLE,ARC_ANGLE);
if (inVis){
//mySound.play();
this.inVisionColor = color(255,0,0,50);
} else{
this.inVisionColor = color(255,255,255,0);
}
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();
stroke(0,0,0,50);
rotate(ROTATION_ANGLE-theta);
//fill(this.inVisionColor);
arc(0,0,2 * ARC_RADIUS,2 * ARC_RADIUS, -ARC_ANGLE / 2, ARC_ANGLE / 2, PIE);
//circle(0,0,this.size*15);
pop();
}