Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.openkinect.*;
- import org.openkinect.processing.*;
- import codeanticode.syphon.*;
- PGraphics canvas;
- SyphonServer server;
- // Showing how we can farm all the kinect stuff out to a separate class
- KinectTracker tracker;
- // Kinect Library object
- Kinect kinect;
- float deg = 15; // Start at 15 degrees
- int npartTotal = 300;
- int npartPerFrame = 2;
- float speed = 2.0;
- float gravity = 0.05;
- float partSize = 20;
- int totalx = 640;
- int totaly = 480;
- int partLifetime;
- PVector positions[];
- PVector velocities[];
- int lifetimes[];
- int fcount, lastm;
- float frate;
- int fint = 3;
- boolean fill = false;
- void setup() {
- size(totalx, totaly, P3D);
- kinect = new Kinect(this);
- tracker = new KinectTracker();
- kinect.tilt(deg);
- canvas = createGraphics(totalx, totaly, P3D);
- // frameRate(120);
- partLifetime = npartTotal / npartPerFrame;
- initPositions();
- initVelocities();
- initLifetimes();
- server = new SyphonServer(this, "Processing Syphon");
- canvas.background(0);
- }
- void draw () {
- // Run the tracking analysis
- tracker.track();
- // Show the image
- // tracker.display();
- // Let's get the raw location
- PVector v1 = tracker.getPos();
- canvas.beginDraw();
- canvas.background(0);
- infodisplay();
- // canvas.stroke(255);
- // noFill();
- canvas.fill(255);
- canvas.tint(255, 255);
- canvas.normal(0, 0, 1);
- for (int n = 0; n < npartTotal; n++) { //lifetime check
- lifetimes[n]++;
- if (lifetimes[n] == partLifetime) {
- lifetimes[n] = 0;
- }
- if (0 <= lifetimes[n]) { // spawn
- if (lifetimes[n] == 0) {
- // Re-spawn dead particle
- // positions[n].x = mouseX;
- // positions[n].y = mouseY;
- positions[n].x = v1.x;
- positions[n].y = v1.y;
- float angle = random(0, TWO_PI);
- float s = random(0.5 * speed, 0.5 * speed);
- velocities[n].x = s * cos(angle);
- velocities[n].y = s * sin(angle);
- }
- else { // gravity
- positions[n].x += velocities[n].x;
- positions[n].y += velocities[n].y;
- velocities[n].y += gravity;
- }
- drawParticle(positions[n]); //, opacity);
- }
- }
- if (fill) canvas.stroke(0);
- else canvas.stroke(255);
- if (v1.y < height/4) {
- partSize = 10;
- gravity = 0.01;
- speed = 10;
- }
- else {
- partSize = 20;
- gravity = 0.2;
- speed = 2;
- }
- fcount += 1;
- int m = millis();
- if (m - lastm > 1000 * fint) {
- frate = float(fcount) / fint;
- fcount = 0;
- lastm = m;
- println("fps: " + frate);
- }
- canvas.endDraw();
- image(canvas, 0, 0);
- server.sendImage(canvas);
- }
- void drawParticle(PVector center) { //, float opacity) {
- canvas.beginShape(QUAD);
- canvas.vertex(center.x - partSize/2, center.y - partSize/2, 0, 0);
- canvas.vertex(center.x + partSize/2, center.y - partSize/2, 64, 0);
- canvas.vertex(center.x + partSize/2, center.y + partSize/2, 64, 64);
- canvas.vertex(center.x - partSize/2, center.y + partSize/2, 0, 64);
- canvas.endShape();
- }
- void initPositions() {
- positions = new PVector[npartTotal];
- for (int n = 0; n < positions.length; n++) {
- positions[n] = new PVector();
- }
- }
- void initVelocities() {
- velocities = new PVector[npartTotal];
- for (int n = 0; n < velocities.length; n++) {
- velocities[n] = new PVector();
- }
- }
- void initLifetimes() {
- // Initializing particles with negative lifetimes so they are added
- // progressively into the screen during the first frames of the sketch
- lifetimes = new int[npartTotal];
- int t = -1;
- for (int n = 0; n < lifetimes.length; n++) {
- if (n % npartPerFrame == 0) {
- t++;
- }
- lifetimes[n] = -t;
- }
- }
- void keyPressed() {
- int t = tracker.getThreshold();
- if (key == CODED) {
- if (keyCode == LEFT) {
- t+=5;
- tracker.setThreshold(t);
- }
- else if (keyCode == RIGHT) {
- t-=5;
- tracker.setThreshold(t);
- }
- else if (keyCode == UP) {
- deg++;
- }
- else if (keyCode == DOWN) {
- deg--;
- }
- deg = constrain(deg, 0, 30);
- kinect.tilt(deg);
- }
- }
- void keyReleased()
- {
- switch(key) {
- case 'f':
- if (fill) fill = false;
- else if (!fill) fill = true;
- break;
- }
- }
- void infodisplay() {
- // Display some info
- int t = tracker.getThreshold();
- println("threshold: " + t + " " + "framerate: " + (int)frameRate + " " + "Angle:" + deg);
- }
- void stop() {
- tracker.quit();
- background(0);
- super.stop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement