For this week’s assignment, I created a little meaningless piece BUT with our favorite picture of Aaron. If the mouse is released, all of little Aarons wonder around, but when the mouse is pressed, all of them (after a little hustle) form the word APPLES.
At first, I wanted to make the objects have a steering behavior (Shiffman has a great video with this). In theory, I understood the concepts and code, but when it came to implementing it on all of the objects that form the letters, not just one, I could not make it work. I was a little stubborn to admit that I bit off a little bit more than I could chew and only after a looong period of trying I had to go with something more familiar. Therefore the code is widely based on Aaron’s code we saw in class, with a little bit more playfulness and variations in the behavior. Spending some time, however, on understanding the steering was a great teaching moment, and yesterday’s full-on frustration turned to today’s motivation to actually make it work for another project.
And this is a little bonus not connected to the assignment, just a simpler version of what I was trying to do according to Shiffman’s code, but kind of failed to apply on a bigger scale.
The code and class for Formation of Aarons:
import geomerative.*; RFont font; RPoint[] pnts; Photo[] formation; PImage aaron, greenB; void setup() { size(640, 360); //load the bckg image greenB = loadImage("green.jpg"); //initialize and load the data (font) RG.init(this); font = new RFont("CaviarDreams.ttf", 150, RFont.CENTER); //get the points APPPLES out of the data pnts = getPoints("APPLES"); int amountPerPoint = 1; //use the class formation = new Photo[pnts.length*amountPerPoint]; int index = 0; for (int i=0; i<pnts.length*amountPerPoint; i+=amountPerPoint) { for (int j=0; j<amountPerPoint; j++) { int k = i+j; formation[i] = new Photo(width/2+pnts[index].x+random(-3, 3), height/1.5+pnts[index].y+random(-3, 3)); } index++; } } void mousePressed() { for (int i =0; i<formation.length; i++) { formation[i].seek=true; } } void mouseReleased() { for (int i =0; i<formation.length; i++) { formation[i].xSpeed = random(-5, 5); formation[i].ySpeed = random(-5, 5); formation[i].seek=false; } } void draw() { background(0); image(greenB, 0, 0); for (int i=0; i<formation.length; i= i+2) { formation[i].seekHome(); formation[i].update(); formation[i].display(); formation[i].checkEdges(); } } RPoint[] getPoints(String str) { RCommand.setSegmentLength (10); RCommand.setSegmentator(RCommand.UNIFORMLENGTH); RGroup grp; grp = font.toGroup(str); grp = grp.toPolygonGroup(); return grp.getPoints(); }
class Photo { float x, y, homeX, homeY; float xSpeed, ySpeed; boolean seek; char letter; PImage aaron; Photo(float _x, float _y ) { homeX = x = _x; homeY = y = _y; xSpeed = ySpeed = 0; // diam = _diam; seek = true; } void update() { x += xSpeed; y += ySpeed; xSpeed *= .95; ySpeed *= .95; aaron = loadImage("aaron.png"); } void display() { aaron.resize(25, 50); image(aaron, x, y); } void seekHome() { if (seek) { float dirX = homeX-x; float dirY = homeY-y; dirX*=.05; dirY*=.05; xSpeed+=dirX; ySpeed+=dirY; } } void checkEdges() { if (y>height) { y=0; } if (y<0) { y=height; } if (x>width) { x=0; } if (x<0) { x=width; } } }
Flying Aaron Code:
Vehicle vehicleA; PImage blackhole; void setup() { size(640, 360); vehicleA = new Vehicle(width/2, height/2); blackhole = loadImage("black hole.jpg"); } void draw(){ background(0); image(blackhole, 0, 0); vehicleA.update(); vehicleA.seek(new PVector(mouseX, mouseY)); vehicleA.display(); }
class Vehicle { float maxSpeed; float maxForce; float r; PImage a; PVector location, target, desired, velocity, acceleration; boolean mouseClose; Vehicle (float x, float y) { maxSpeed = 4; maxForce = 0.1; mouseClose = false; acceleration = new PVector(0, 0); velocity = new PVector(0, -2); location = new PVector(x, y); } void update () { //updated velocity velocity.add(acceleration); //to limit the speed velocity.limit(maxSpeed); location.add(velocity); //(come back to zero) acceleration.mult(0); a = loadImage("aaron.png"); } void applyForce(PVector force) { //Q acceleration.add(force); } void seek (PVector target) { PVector desired = PVector.sub(target, location); desired.normalize(); desired.mult(maxSpeed); //steering = desired minus velocity PVector steer = PVector.sub(desired, velocity); steer.limit(maxForce); applyForce(steer); } void display() { float theta = velocity.heading(); pushMatrix(); translate(location.x, location.y); rotate(theta + PI/2); image(a, 0, 0, 50, 100); popMatrix(); } }