NOC Assignment 3: particle thingies

Date February 18, 2009

This is kind of a mixture between Assignment 3 and the midterm proposal, using forces in a basic particle system.
Code
Executable (OSX/Leopard)

NOC Assignment 2: Fleas!++

Date February 4, 2009

These fleas are a lot brighter… since I was already using vectors & basic physics, I spent the time getting edge detection to work (for places to land) and JMyron blob detection to work (for flea targets).

Code and applications (no clue if anything but OSX version work) here

I am a Strange Loop

Date January 31, 2009

Just finished reading the book… found myself not as into it as I remember being about GEB… though still bits of it really resonate, and I think something that it pulls at in me is amazement that somehow our universe is defined in such a way that it supports (encourages, in fact) constructs that climb out of any cage you try to trap them in.

Favorite quote: “… thanks to a mapping, full-fledged meaning can suddenly appear in a spot where it was entirely unsuspected …” — if you think about it, a) it happens not uncommonly (whenever we learn something new that opens up a new way of looking at the world) b) it creates entirely new worlds for us to explore out of places we’ve already been and c) there’s something beyond magic about all of that… I’m mostly posting this entry to help me remember that quote.

By a strange and interesting twist, the book also tied very well in to the beginning chapters of “The Computational Beauty of Nature” which I’ve been reading for Nature of Code… and provided a much deeper discussion of a number of the concepts in the “Computability and Incomputability” sections.

NOC Assignment 1: Fleas!

Date January 28, 2009

I decided to model this assignment after the movement of fleas
fleas (see at about 1:00 for fleas jumping)

The fleas respond to the camera; they will somewhat try to jump towards the brighter area of the screen, and will be able to land on any reasonably bright object.

flea_main.pde:

// Fleas (main program)
// Tymm Twillman 
// The Nature of Code, Spring 2009
import processing.video.*;

static final int NUM_FLEAS = 50;

Flea f[] = new Flea[NUM_FLEAS];
Capture capture;

void captureEvent(Capture capture)
{
  capture.read();
}

void setup() {

  size(400,400);
  frameRate(30);

  // Create flea objects
  for (int i = 0; i < NUM_FLEAS; i++) {
    f[i] = new Flea();
  }

  capture = new Capture(this,width,height,30);
}

void draw() {
  capture.loadPixels();

  float xpos = capture.width / 2;
  float ypos = capture.height / 2;

  float b_tot = 0f;

  int xcent = 0;
  int ycent = 0;

  // find the averaged center of brightness on the screen; this is used to tell
  //  the fleas where to aim for.  Is there an easier way to find (essentially)
  //  the moment of inertia of an irregular mass?
  //  Here, calculate the sum of all brightnesses... then count the sum of
  //  all brightness along X until reach 1/2 total sum... then do same for y.
  for (int y = 0; y < capture.height; y++) {
    for (int x = 0; x < capture.width; x++) {
      float b = brightness(capture.pixels[x + y * capture.width]);
      b_tot += b;
    }
  }

  for (int y = 0; y < capture.height; y++) {
    for (int x = 0; x < capture.width; x++) {
      if (ypos >= b_tot / 2) {
        break;
      }
      float b = brightness(capture.pixels[x + y * capture.width]);
      ypos += b;
      ycent = y;
     }
  }

  for (int x = 0; x < capture.width; x++) {
    for (int y = 0; y < capture.height; y++) {
      if (xpos >= b_tot / 2) {

        break;
      }
      float b = brightness(capture.pixels[x + y * capture.width]);
      xpos += b;
      xcent = x;
    }
  }

  background(255);
  image(capture, 0, 0);

  // Move the fleas
  for (int i = 0; i < NUM_FLEAS; i++) {
    if (xcent <= width && ycent <= height && xcent >= 0 && ycent >= 0) {
      f[i].move(xcent, ycent, capture);
    }
  }

  // And show them
  for (int i = 0; i < NUM_FLEAS; i++) {
    f[i].render();
  }

  // (debugging) show the point the fleas are aiming for
  // rect(xcent, ycent, 20, 20);

}

flea.pde:

// Fleas (with vectors)
// Tymm Twillman 
// The Nature of Code, Spring 2009

class Flea {
  // Current x, y location
  PVector loc;

  // Velocity (currently just impulse @ jump)
  PVector vel;

  // Acceleration (right now just gravity)
  PVector acc;

  // current rotation & angular velocity
  float rot;
  float angVel;

  Flea() {
    loc = new PVector(random(0, width),height - 1);
    vel = new PVector(0, 0);
    // constant gravity
    acc = new PVector(0, .5);
    rot = 0.0;
    angVel = 0.0;
  }

   void render() {
    stroke(0);
    fill(175);
    rectMode(CENTER);

    // because using rotation, use translate and put
    //  objects at 0,0
    pushMatrix();
    translate(loc.x, loc.y);
    rotate(rot);
    rect(0,0,5,5);
    popMatrix();
  }

  // Move based on a mixture of:
  //  - position-based noise
  //  - a little randomness so different fleas in same place do slightly different thing
  //  - where the "center" of brightness on the screen is

  //  also update rotation by angular velocity.

  void move(int xpull, int ypull, Capture c) {
    vel.add(acc);
    loc.add(vel);

    // Stay on the screen
    loc.x = constrain(loc.x,0,width-1);
    loc.y = constrain(loc.y,0,height-1);

    rot += angVel;

    // this does depend on capture width/height being same as screen
    if ((loc.y >= height - 2) || (brightness(capture.pixels[(int)(loc.y + 1) * capture.width + (int)loc.x]) > 100)) {
      if (vel.y > -.0001) {
        rot = 0.0;

        PVector cent = new PVector(xpull, ypull);
        PVector dst = PVector.sub(cent, loc);
        PVector n = new PVector(dst.x, dst.y);

        angVel = (noise(loc.x, loc.y) - .5) * 3 + random(-.25, .25);

        n.normalize();

        vel = new PVector(noise(loc.x,loc.y) * sqrt(abs(dst.x)) * n.x + random(-1, 1), noise(loc.y,loc.x) * 2 * sqrt(abs(dst.y)) * n.y + random(0, 10));
      }
    }
  }
}

it gets a bit more disturbing when you change the shape/color, even in a very simple way (this just changes the fill/stroke colors & uses ellipses instead of squares):

// Fleas (with vectors)
// Tymm Twillman 
// The Nature of Code, Spring 2009

class Flea {
  // Current x, y location
  PVector loc;

  // Velocity (currently just impulse @ jump)
  PVector vel;

  // Acceleration (right now just gravity)
  PVector acc;

  // current rotation & angular velocity
  float rot;
  float angVel;

  Flea() {
    loc = new PVector(random(0, width),height - 1);
    vel = new PVector(0, 0);
    // constant gravity
    acc = new PVector(0, .5);
    rot = 0.0;
    angVel = 0.0;
  }

   void render() {
    stroke(60);
    fill(0);
    ellipseMode(CENTER);

    // because using rotation, use translate and put
    //  objects at 0,0
    pushMatrix();
    translate(loc.x, loc.y);
    rotate(rot);
    ellipse(0,0,4,8);
    popMatrix();
  }

  // Move based on a mixture of:
  //  - position-based noise
  //  - a little randomness so different fleas in same place do slightly different thing
  //  - where the "center" of brightness on the screen is

  //  also update rotation by angular velocity.

  void move(int xpull, int ypull, Capture c) {
    vel.add(acc);
    loc.add(vel);

    // Stay on the screen
    loc.x = constrain(loc.x,0,width-1);
    loc.y = constrain(loc.y,0,height-1);

    rot += angVel;

    // this does depend on capture width/height being same as screen
    if ((loc.y >= height - 2) || (brightness(capture.pixels[(int)(loc.y + 1) * capture.width + (int)loc.x]) > 100)) {
      if (vel.y > -.0001) {
        rot = 0.0;

        PVector cent = new PVector(xpull, ypull);
        PVector dst = PVector.sub(cent, loc);
        PVector n = new PVector(dst.x, dst.y);

        angVel = (noise(loc.x, loc.y) - .5) * 3 + random(-.25, .25);

        n.normalize();

        vel = new PVector(noise(loc.x,loc.y) * sqrt(abs(dst.x)) * n.x + random(-1, 1), noise(loc.y,loc.x) * 2 * sqrt(abs(dst.y)) * n.y + random(0, 10));
      }
    }
  }
}

I tried to export as an applet, but it didn't seem to run through a browser (maybe due to capture dependency? or I may have just loused something up. rimshot.)

Slackin4: day 2

Date January 14, 2009

build a PIC based USB keyboard emulator

I got a PIC18f14k50 sample from Microchip a bit ago and wanted to make an easy control device prototype with it (similar to the i-PAC boards from Ultimarc, but with a much lesser feature set).

Today ended up being just tons of soldering… I’d hoped to get the software up and going too, but may have to make that a seperate project, as it’s getting late and I’m still kinda wiped out from having been sick. Should be pretty easy using the Microchip USB framework.

Not much to the board: the IC, an oscillator, couple capacitors, reset button, a header for programming and a bunch of screw terminals. If I’d etched it, it probably would have taken half the time to put together…