Skip to content

Commit

Permalink
Wander
Browse files Browse the repository at this point in the history
Created for the new URX notebooks. Line drawing with entities that draw
faint straight lines, then dark curved lines inside the logo.
  • Loading branch information
James Lawrence Turner committed Jun 11, 2015
1 parent 02aa133 commit 113cfe8
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#### Copyright James Lawrence Turner, 2015, All Rights Reserved

## Wander [Mon June 2, 2015]
![Wander](/wander/output.png?raw=true)

## Angel Camoflage [Mon Feb 2, 2015]
![Angel Camoflage](/angel_camoflage/output.png?raw=true)

Expand Down
Binary file added wander/1.pdf
Binary file not shown.
Binary file added wander/2.pdf
Binary file not shown.
Binary file added wander/3.pdf
Binary file not shown.
Binary file added wander/4.pdf
Binary file not shown.
228 changes: 228 additions & 0 deletions wander/boids.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
// The Boid class

class Boid {

PVector location;
PVector plocation;
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);

// This is a new PVector method not yet implemented in JS
// velocity = PVector.random2D();

// Leaving the code temporarily this way so that this example runs in JS
float angle = random(TWO_PI);
velocity = new PVector(cos(angle), sin(angle));

location = new PVector(x, y);
r = 2.0;
maxspeed = 2;
maxforce = 0.03;
}

void run(ArrayList<Boid> boids) {
try{
if(logoMask.pixels[floor(location.y) * width + floor(location.x)] == color(255)) {
stroke(0,255);
flock(boids);
} else {
stroke(0,20);
}
}
catch (Exception e) {
// 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(1.5);
ali.mult(1.0);
coh.mult(1.0);
// Add the force vectors to acceleration
applyForce(sep);
applyForce(ali);
applyForce(coh);
}

// Method to update location
void update() {
// Update velocity
velocity.add(acceleration);
// Limit speed
velocity.limit(maxspeed);
plocation = location.get();
location.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, location); // A vector pointing from the location to the target
// Scale to maximum speed
desired.normalize();
desired.mult(maxspeed);

// Above two lines of code below could be condensed with new PVector setMag() method
// Not using this method until Processing.js catches up
// desired.setMag(maxspeed);

// Steering = Desired minus Velocity
PVector steer = PVector.sub(desired, velocity);
steer.limit(maxforce); // Limit to maximum steering force
return steer;
}

void render() {
// Draw a triangle rotated in the direction of velocity
float theta = velocity.heading2D() + radians(90);
// heading2D() above is now heading() but leaving old syntax until Processing.js catches up

if(location.dist(plocation) < 20) {
line(plocation.x, plocation.y, location.x, location.y);
}
}

// Wraparound
void borders() {
if (location.x < -r) location.x = width+r;
if (location.y < -r) location.y = height+r;
if (location.x > width+r) location.x = -r;
if (location.y > height+r) location.y = -r;
}

// Separation
// Method checks for nearby boids and steers away
PVector separate (ArrayList<Boid> boids) {
float desiredseparation = 25.0f;
PVector steer = 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(location, other.location);
// 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(location, other.location);
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((float)count);
}

// As long as the vector is greater than 0
if (steer.mag() > 0) {
// First two lines of code below could be condensed with new PVector setMag() method
// Not using this method until Processing.js catches up
// steer.setMag(maxspeed);

// Implement Reynolds: Steering = Desired - Velocity
steer.normalize();
steer.mult(maxspeed);
steer.sub(velocity);
steer.limit(maxforce);
}
return steer;
}

// 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(location, other.location);
if ((d > 0) && (d < neighbordist)) {
sum.add(other.velocity);
count++;
}
}
if (count > 0) {
sum.div((float)count);
// First two lines of code below could be condensed with new PVector setMag() method
// Not using this method until Processing.js catches up
// sum.setMag(maxspeed);

// Implement Reynolds: Steering = Desired - Velocity
sum.normalize();
sum.mult(maxspeed);
PVector steer = PVector.sub(sum, velocity);
steer.limit(maxforce);
return steer;
}
else {
return new PVector(0, 0);
}
}

// Cohesion
// For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
PVector cohesion (ArrayList<Boid> boids) {
float neighbordist = 100;
PVector sum = new PVector(0, 0); // Start with empty vector to accumulate all locations
int count = 0;
for (Boid other : boids) {
float d = PVector.dist(location, other.location);
if ((d > 0) && (d < neighbordist)) {
sum.add(other.location); // Add location
count++;
}
}
if (count > 0) {
sum.div(count);
return seek(sum); // Steer towards the location
}
else {
return new PVector(0, 0);
}
}
}

// The Flock (a list of Boid objects)

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);
}

}


Empty file added wander/output.pdf
Empty file.
Binary file added wander/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions wander/sketch.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mode.id=processing.mode.java.JavaMode
mode=Java
20 changes: 20 additions & 0 deletions wander/urx-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions wander/wander.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
PShape logo;
PGraphics logoMask;
import processing.pdf.*;

Flock flock;

void setup() {
size(1000,500);
beginRecord(PDF, "output.pdf");

logo = loadShape("urx-logo.svg");
logo.disableStyle();
logo.scale((width/2.0) / logo.width);

logoMask = createGraphics(width, height);
logoMask.beginDraw();
logoMask.background(0);
logoMask.translate(((width/2) / 8) + width /2, height/4 + height / 16);
logoMask.scale(0.75);
logoMask.fill(255);
logoMask.shape(logo);
logoMask.endDraw();

smooth();
background(color(255));

flock = new Flock();
for(int i = 0; i < 50; i++) {
flock.addBoid(new Boid(random(width), height));
}
}

void draw() {
flock.run();
}

void mousePressed() {
endRecord();
exit();
}

0 comments on commit 113cfe8

Please sign in to comment.