xxxxxxxxxx
290
let boids = [];
let img;
function preload()
{
//img = loadImage('http://www.markpilkington.org.uk/open2020/data/b1.png');
}
function setup() {
createCanvas(windowWidth, windowHeight);
// Add an initial set of boids into the system
for (let i = 0; i < 100; i++) {
boids[i] = new Boid(random(width), random(height));
}
}
function draw() {
background(0);
textSize(25);
textAlign(CENTER);
fill(90);
text('Ryho', 100, 30);
text('Mark Pilkington/Thought Universe', 100, 60);
grid();
stroke(255);
text("v8", 25, 0);
line(83, 0, 83, height);// vertical
text("v7", 103, 250);
line(103, 0, 103, height);//v7
//image(img, 0, 0, 100, 100);
// Run all the boids
for (let i = 0; i < boids.length; i++) {
boids[i].run(boids);
}
noisePanel();
}
function noisePanel ()
{
let xnoise = float(0.0);
let ynoise = float(0.0);
let inc = float(0.06);
let density = int(5);
let znoise = float(0.0);
text("noisePanel", 0, 0);
//camera(width/2, 1035, (height/2.0) / tan(PI*30.0 / 180.0), width/2.0, height/2.0, 0, 0, 1, 0);
for (let y = 0; y < 100; y += density)
{
for (let x = 0; x < 100; x += density)
{
let n = noise(xnoise, ynoise, znoise) * 255;
fill(n);
rect(y,x+200, density/1+1219, density);
print(mouseX);
xnoise += inc;
}
xnoise = 0.0;
ynoise += inc;
}
znoise += inc;
}
// Boid class
// Methods for Separation, Cohesion, Alignment added
class Boid {
constructor(x, y) {
this.acceleration = createVector(0, 0);
this.velocity = p5.Vector.random2D();
this.position = createVector(20, y);
this.r = 6.0;
this.maxspeed = 10; // Maximum speed
this.maxforce = 0.5; // Maximum steering force
}
run(boids) {
this.flock(boids);
this.update();
this.borders();
this.render();
}
// Forces go into acceleration
applyForce(force) {
this.acceleration.add(force);
}
// We accumulate a new acceleration each time based on three rules
flock(boids) {
let sep = this.separate(boids); // Separation
let ali = this.align(boids); // Alignment
let coh = this.cohesion(boids); // Cohesion
// Arbitrarily weight these forces
sep.mult(1.5);
ali.mult(1.0);
coh.mult(1.0);
// Add the force vectors to acceleration
this.applyForce(sep);
this.applyForce(ali);
this.applyForce(coh);
}
// Method to update location
update() {
// Update velocity
this.velocity.add(this.acceleration);
// Limit speed
this.velocity.limit(this.maxspeed);
this.position.add(this.velocity);
// Reset acceleration to 0 each cycle
this.acceleration.mult(0);
}
// A method that calculates and applies a steering force towards a target
// STEER = DESIRED MINUS VELOCITY
seek(target) {
let desired = p5.Vector.sub(target, this.position); // A vector pointing from the location to the target
// Normalize desired and scale to maximum speed
desired.normalize();
desired.mult(this.maxspeed);
// Steering = Desired minus Velocity
let steer = p5.Vector.sub(desired, this.velocity);
steer.limit(this.maxforce); // Limit to maximum steering force
return steer;
}
// Draw boid as a circle
render() {
fill(mouseX, 127);
stroke(200);
fill('#FF00FF');
rect(this.position.x, this.position.y, 10, 10);
}
// Wraparound
borders() {
if (this.position.x < -this.r) this.position.x = width + this.r;
if (this.position.y < -this.r) this.position.y = height + this.r;
if (this.position.x > width + this.r) this.position.x = -this.r;
if (this.position.y > height + this.r) this.position.y = -this.r;
}
// Separation
// Method checks for nearby boids and steers away
separate(boids) {
let desiredseparation = 25.0;
let steer = createVector(0, 0);
let count = 0;
// For every boid in the system, check if it's too close
for (let i = 0; i < boids.length; i++) {
let d = p5.Vector.dist(this.position, boids[i].position);
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
if ((d > 0) && (d < desiredseparation)) {
// Calculate vector pointing away from neighbor
let diff = p5.Vector.sub(this.position, boids[i].position);
diff.normalize();
diff.div(d); // Weight by distance
steer.add(diff);
count++; // Keep track of how many
}
}
// Average -- divide by how many
if (count > 0) {
steer.div(count);
}
// As long as the vector is greater than 0
if (steer.mag() > 0) {
// Implement Reynolds: Steering = Desired - Velocity
steer.normalize();
steer.mult(this.maxspeed);
steer.sub(this.velocity);
steer.limit(this.maxforce);
}
return steer;
}
// Alignment
// For every nearby boid in the system, calculate the average velocity
align(boids) {
let neighbordist = 10;
let sum = createVector(0, 0);
let count = 0;
for (let i = 0; i < boids.length; i++) {
let d = p5.Vector.dist(this.position, boids[i].position);
if ((d > 0) && (d < neighbordist)) {
sum.add(boids[i].velocity);
count++;
}
}
if (count > 0) {
sum.div(count);
sum.normalize();
sum.mult(this.maxspeed);
let steer = p5.Vector.sub(sum, this.velocity);
steer.limit(this.maxforce);
return steer;
} else {
return createVector(20, 0);
}
}
// Cohesion
// For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
cohesion(boids) {
let neighbordist = 15;
let sum = createVector(0, 0); // Start with empty vector to accumulate all locations
let count = 0;
for (let i = 0; i < boids.length; i++) {
let d = p5.Vector.dist(this.position, boids[i].position);
if ((d > 0) && (d < neighbordist)) {
sum.add(boids[i].position); // Add location
count++;
}
}
if (count > 0) {
sum.div(count);
return this.seek(sum); // Steer towards the location
} else {
return createVector(0, 0);
}
}
}
function grid()
{
// grid 1
x = 23.4;//83
y = 3.31;//324
x1 = 18.04;//103
x2 = 6.75;//284
x3 = 7.35;//262
x4 = 1.2802;//1502
x5 = 1.26;//1524
x6 = 2.02;//949
x7 = 1.975;//972
y1 = 3.12;//345
y2 = 2.03;//533
y3 = 4.25;//253
y4 = 2.04;//529
y5 = 1.264;//854
y6 = 2.04;//530
y7 = 1.96;//555
y8 = 1.96;//551
y9 = 1.26;//842
stroke(255);
//grid track 1
textSize(100);
text("v8", width/x, height/y2);
line(width/x, height/y, width/x, height/y2);//v8
text("v7", width/x1, height/y2);
line(width/x1, height/y1, width/x1, height/y2);//v7
text("h4", width/x, height/y);
line(width/x, height/y, width/x2, height/y);
text("h3", width/x1, height/y1);
line(width/x1, height/y1, width/x3, height/y1);
text("v6", width/x3, height/y1);
line(width/x3, height/y1, width/x3, height/y7);
line(width/x3, height/y1, width/x3, height/y7);
text("v5", width/x2, height/y);
line(width/x2, height/y, width/x2, height/y6);
text("v11", width/x4, height/y3);
line(width/x4, height/y3, width/x4, height/y4);
text("v10", width/x5, height/y3);
line(width/x5, height/y3, width/x5, height/y5);
text("h2", width/x2, height/y6);
line(width/x2, height/y6, width/x6, height/y6);//335 point of change white noise
text("h1", width/x3, height/y7);
line(width/x3, height/y7, width/x6, height/y7);//horizontal
text("h5", width/x7, height/y6);
text("h5", width/x7, height/y6);
line(width/x7, height/y6, width/x4, height/y6);//335 point of change white noise
text("h6", width/x7, height/y7);
line(width/x7, height/y7, width/x4, height/y7);//horizontal
text("v9", width/x4, height/y5);
line(width/x4, height/y5, width/x4, height/y8);
text("v2", width/x6, height/y9);
line(width/x6, height/y7, width/x6, height/y5);//vertical
text("v1", width/x7, height/y9);
line(width/x7, height/y7, width/x7, height/y5);//vertical 2
text("v3", width/x7, height/y3);
line(width/x7, height/y3, width/x7, height/y6);//vertical3
text("v4", width/x6, height/y3);
line(width/x6, height/y3, width/x6, height/y6);//vertical4
}