Hi @jeremydouglass.
Sorry about the private thing. I have made it public.
I am using Sound Library.
There are four sound files. Two are two seconds long and two are three seconds long. There are 30 boids. Everytime they go out of the map, through the borders method at the Boid class, a sound file is played. Therefore, 30 files may be played at the same time. Is this too many? How do I make the thing more efficient?
Here is the code. It won’t work without the sound files though:
import processing.sound.*;
Flock flock;
SoundFile chaA;
SoundFile chaB;
SoundFile chaC;
SoundFile chaD;
SinOsc sineA;
SinOsc sineB;
TriOsc tri;
float freqA = 400;
float freqB = 400;
float theta;
float xoff = 0.0;
float nois;
void setup() {
size(1080, 910);
//the flock stuff
flock = new Flock();
// Add an initial set of boids into the system
for (int i = 0; i < 30; i++) {
Boid b = new Boid(width/2, height/2);
flock.addBoid(b);
}
//the sound stuff
chaA = new SoundFile(this, "chaA.wav");
chaA.amp(0.02);
chaB = new SoundFile(this, "chaB.wav");
chaB.amp(0.02);
chaC = new SoundFile(this, "chaC.wav");
chaC.amp(0.02);
chaD = new SoundFile(this, "chaD.wav");
chaD.amp(0.02);
sineA = new SinOsc(this);
sineA.amp(0.03);
sineA.play();
sineB = new SinOsc(this);
sineB.amp(0.03);
sineB.play();
tri = new TriOsc(this);
tri.amp(0.02);
tri.play();
}
void draw() {
background(0);
flock.run();
// Instructions
fill(0);
text("Drag the mouse to generate new boids.", 10, height-16);
float hertzA = map(mouseY, 0, 888, 666, 20); //dont touch this beautiful mapping!
float hertzB = map(mouseX, 0, 620, 20, 666);
float hertzNois = map(nois, 0, 1, 20, 333);
sineA.freq(hertzA);
sineB.freq(hertzB);
tri.freq(hertzNois);
fill(#2B6403);
//TREE 1
stroke(255);
// Let's pick an angle 0 to 90 degrees based on the mouse position
float a = (mouseX / (float) width) * 90f;
// Convert it to radians
theta = radians(a);
// Start the tree from the bottom of the screen
translate(width/2, height);
// Draw a line 120 pixels
line(0, 0, 0, -120);
// Move to the end of that line
translate(0, -120);
// Start the recursive branching!
branch(360);
xoff = xoff + 0.01;
nois = noise(xoff);
}
// Add a new boid into the System
void mouseDragged() {
flock.addBoid(new Boid(mouseX, mouseY));
}
void branch(float h) {
// Each branch will be 2/3rds the size of the previous one
h *= 0.66;
// All recursive functions must have an exit condition!!!!
// Here, ours is when the length of the branch is 2 pixels or less
if (h > 2) {
pushMatrix(); // Save the current state of transformation (i.e. where are we now)
rotate(theta); // Rotate by theta
line(0, 0, 0, -h*nois); // Draw the branch
translate(0, -h*nois); // Move to the end of the branch
branch(h*nois); // Ok, now call myself to draw two new branches!!
popMatrix(); // Whenever we get back here, we "pop" in order to restore the previous matrix state
// Repeat the same thing, only branch off to the "left" this time!
pushMatrix();
rotate(-theta);
line(0, 0, 0, -h);
translate(0, -h);
branch2(h*nois);
popMatrix();
}
}
void branch2(float h) {
// Each branch will be 2/3rds the size of the previous one
h *= 0.66;
// All recursive functions must have an exit condition!!!!
// Here, ours is when the length of the branch is 2 pixels or less
if (h > 2) {
pushMatrix(); // Save the current state of transformation (i.e. where are we now)
rotate(theta); // Rotate by theta
line(0, 0, 0, h); // Draw the branch
translate(0, h); // Move to the end of the branch
branch(-h); // Ok, now call myself to draw two new branches!!
popMatrix(); // Whenever we get back here, we "pop" in order to restore the previous matrix state
// Repeat the same thing, only branch off to the "left" this time!
pushMatrix();
rotate(-theta);
line(0, 0, 0, h);
translate(0, h);
branch(-h);
popMatrix();
}
}
class Boid {
PVector position;
PVector velocity;
PVector acceleration;
float r;
float maxforce; // Maximum steering force
float maxspeed; // Maximum speed
Boid(float x, float y) {
acceleration = new PVector(0, 0);
velocity = new PVector(random(-1, 1), random(-1, 1));
position = new PVector(x, y);
r = 3.0;
maxspeed = 3;
maxforce = 0.05;
}
void run(ArrayList<Boid> boids) {
flock(boids);
update();
borders();
render();
}
void applyForce(PVector force) {
// We could add mass here if we want A = F / M
acceleration.add(force);
}
// We accumulate a new acceleration each time based on three rules
void flock(ArrayList<Boid> boids) {
PVector sep = separate(boids); // Separation
PVector ali = align(boids); // Alignment
PVector coh = cohesion(boids); // Cohesion
// Arbitrarily weight these forces
sep.mult(map(mouseX, 0, 640, 0, 4));
ali.mult(1.0);
coh.mult(map(mouseY, 0, 888, 0, 4));
// Add the force vectors to acceleration
applyForce(sep);
applyForce(ali);
applyForce(coh);
}
// Method to update position
void update() {
// Update velocity
velocity.add(acceleration);
// Limit speed
velocity.limit(maxspeed);
position.add(velocity);
// Reset accelertion to 0 each cycle
acceleration.mult(0);
}
// A method that calculates and applies a steering force towards a target
// STEER = DESIRED MINUS VELOCITY
PVector seek(PVector target) {
PVector desired = PVector.sub(target, position); // A vector pointing from the position to the target
// Normalize desired and scale to maximum speed
desired.normalize();
desired.mult(maxspeed);
// Steering = Desired minus Velocity
desired.sub(velocity); //elimine steer. en lugar queda desired. uso un vector menos!
desired.limit(maxforce); // Limit to maximum steering force
return desired;
}
void render() {
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading() + radians(90);
stroke(0);
pushMatrix();
translate(position.x, position.y);
rotate(theta);
fill(random(255), 0, random(255));
rect(-3*r, 2*r, r, r); //he aca un gridsito pegado al triangulo que apunta a donde el monstruo quiere
fill(random(255), 0, random(255));
rect(-2*r, 2*r, r, r); // el cual sera optado como indicador del fenotipo visual del animalin.
fill(random(255), 0, random(255));
rect(-1*r, 2*r, r, r);
fill(random(255), 0, random(255));
rect(0*r, 2*r, r, r);
fill(random(255), 0, random(255));
rect(1*r, 2*r, r, r);
fill(random(255), 0, random(255));
rect(2*r, 2*r, r, r);
fill(random(255), 0, random(255));
rect(-3*r, 3*r, r, r);
fill(random(255), 0, random(255));
rect(-2*r, 3*r, r, r);
fill(random(255), 0, random(255));
rect(-1*r, 3*r, r, r);
fill(random(255), 0, random(255));
rect(0*r, 3*r, r, r);
fill(random(255), 0, random(255));
rect(1*r, 3*r, r, r);
fill(random(255), 0, random(255));
rect(2*r, 3*r, r, r);
fill(random(255), 0, random(255));
rect(-3*r, 4*r, r, r);
fill(random(255), 0, random(255));
rect(-2*r, 4*r, r, r);
fill(random(255), 0, random(255));
rect(-1*r, 4*r, r, r);
fill(random(255), 0, random(255));
rect(0*r, 4*r, r, r);
fill(random(255), 0, random(255));
rect(1*r, 4*r, r, r);
fill(random(255), 0, random(255));
rect(2*r, 4*r, r, r);
endShape();
popMatrix();
}
// Wraparound and Charango activation
void borders() {
float rate;
if (position.x < -r) {
rate = map(position.y, 0, 888, 0.25, 4);
position.x = width+r;
chaA.rate(rate);
chaA.play();
}
if (position.y < -r) {
rate = map(position.x, 0, 640, 0.5, 2);
position.y = height+r;
chaB.rate(rate);
chaB.play();
}
if (position.x > width+r) {
rate = map(position.y, 0, 888, 0.25, 4);
position.x = -r;
chaC.rate(rate);
chaC.play();
}
if (position.y > height+r) {
rate = map(position.y, 0, 640, 0.5, 2);
position.y = -r;
chaD.rate(rate);
chaD.play();
}
}
// Separation
// Method checks for nearby boids and steers away
PVector separate (ArrayList<Boid> boids) {
float desiredseparation = 25.0f;
PVector desired = new PVector(0, 0, 0);
int count = 0;
// For every boid in the system, check if it's too close
for (Boid other : boids) {
float d = PVector.dist(position, other.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
PVector diff = PVector.sub(position, other.position);
diff.normalize();
diff.div(d); // Weight by distance
desired.add(diff);
count++; // Keep track of how many
}
}
// Average -- divide by how many
if (count > 0) {
desired.div((float)count);
}
// As long as the vector is greater than 0
if (desired.mag() > 0) {
// Implement Reynolds: Steering = Desired - Velocity
desired.normalize();
desired.mult(maxspeed);
desired.sub(velocity);
desired.limit(maxforce);
}
return desired;
}
// Alignment
// For every nearby boid in the system, calculate the average velocity
PVector align (ArrayList<Boid> boids) {
float neighbordist = 50;
PVector sum = new PVector(0, 0);
int count = 0;
for (Boid other : boids) {
float d = PVector.dist(position, other.position);
if ((d > 0) && (d < neighbordist)) {
sum.add(other.velocity);
count++;
}
}
if (count > 0) {
sum.div((float)count);
sum.normalize();
sum.mult(maxspeed);
PVector desired = PVector.sub(sum, velocity);
desired.limit(maxforce);
return desired;
} else {
return new PVector(0, 0);
}
}
// Cohesion
// For the average position (i.e. center) of all nearby boids, calculate steering vector towards that position
PVector cohesion (ArrayList<Boid> boids) {
float neighbordist = 50;
PVector sum = new PVector(0, 0); // Start with empty vector to accumulate all positions
int count = 0;
for (Boid other : boids) {
float d = PVector.dist(position, other.position);
if ((d > 0) && (d < neighbordist)) {
sum.add(other.position); // Add position
count++;
}
}
if (count > 0) {
sum.div(count);
return seek(sum); // Steer towards the position
} else {
return new PVector(0, 0);
}
}
}
class Flock {
ArrayList<Boid> boids; // An ArrayList for all the boids
Flock() {
boids = new ArrayList<Boid>(); // Initialize the ArrayList
}
void run() {
for (Boid b : boids) {
b.run(boids); // Passing the entire list of boids to each boid individually
}
}
void addBoid(Boid b) {
boids.add(b);
}
}