Air drawing with kinect

edited May 2014 in Kinect

So I am following this example from the book 'kinect hacks': https://github.com/pablonosh/DIYAngryBirds/blob/master/DIYAngryBirds/DIYAngryBirds/DIYAngryBirds.pde

It runs fine, but I only see RGB on the screen. Tried waving; no luck.. Can anybody help why? The version on github uses older version of simple openNI library, so I ported to the new ones..

 import fisica.*;
import java.awt.*;
import SimpleOpenNI.*;

SimpleOpenNI context;
//String gestureToRecognize = "RaiseHand";

FWorld world;
FPoly tempShape;

FBody lastSelectedBody;

long handsStillSince;
PVector hand = null;
PVector lastHand = null;
PVector lastFinger;
ArrayList handPositions = new ArrayList();

void setup() {
  size(640, 480);
  smooth();

  setupWorld();
  setupKinect();
}

void setupWorld() {
  Fisica.init(this);
  world = new FWorld();
  world.setGravity(0, 200);
  world.setEdges();
  world.setEdgesRestitution(0);
}

void setupKinect() {
  context = new SimpleOpenNI(this);
  if (context.isInit() == false)
  {
    println("Can't init SimpleOpenNI, maybe the camera is not connected!"); 
    exit();
    return;
  }

  context.enableDepth();
  context.enableRGB();
  context.setMirror(true);
  context.enableHand();
  context.startGesture(SimpleOpenNI.GESTURE_WAVE);
}

void draw() {
  background(0);

  context.update();
  drawRGB();
  world.step();
  world.draw(this);

  if (tempShape != null) {
    tempShape.draw(this);
  }

  processUserInputs();
  lastHand = hand;
}




void processUserInputs() {
  Boolean isDrawingShape = false;
  Boolean hasSelectedShape = lastSelectedBody != null;

  if (lastHand != null && hand != null) {
    if (handPositions.isEmpty()) {

      FBody b = world.getBody(hand.x, hand.y);
      if (b != null && !hasSelectedShape && handsBeenStillFor(lastHand, hand, 500)) {
        println("selected objec " + b);
        //if touching
        lastSelectedBody = b;
        lastFinger = hand;
        b.setFill(120, 120, 120);
      }
      else if (lastSelectedBody != null && lastSelectedBody != b) {
        println("Moving object");

        float dx = (lastFinger.x - hand.x);
        float dy = (lastFinger.y - hand.y);

        if (dx != 0 && dy != 0) {
          lastSelectedBody.setVelocity(-20*dx, -20*dy);
        }

        lastSelectedBody.setFill(120, 30, 90);
        lastSelectedBody = null;
      } 
      else if (!hasSelectedShape && handsBeenStillFor(lastHand, hand, 2000)) {

        println("creating new");
        isDrawingShape = true;
      } 
      else if (hasSelectedShape) {
        b.setFill(120, 120, 120);
      }
    }
    if (!handPositions.isEmpty()) {

      if (!handsBeenStillFor(lastHand, hand, 2000)) {
        println("extedning ");
        isDrawingShape = true;
      } 
      else {
        println("finsished");
        world.add(tempShape);
        tempShape = null;
        handPositions.clear();
      }
    }


    if (isDrawingShape) {
      println("Refreshing ");
      handPositions.add(hand);
      tempShape = new FPoly();
      tempShape.setStrokeWeight(3);
      tempShape.setFill(120, 20, 90);
      tempShape.setDensity(10);
      tempShape.setRestitution(0);

      for (int i = 0; i < handPositions.size(); i++) {
        PVector p = (PVector) handPositions.get(i);
        tempShape.vertex(p.x, p.y);
      }
    }

    if (isDrawingShape || hasSelectedShape) {
      fill(0, 255, 0, 64);
      stroke(0, 233, 1);
    } 
    else {
      fill(255, 33, 2, 66);
      stroke(255, 0, 0);
    }

    ellipse(hand.x, hand.y, 20, 20);
  }
}



void drawRGB() {
  pushMatrix();
 // scale(-1.0, -1.0);
  image(context.rgbImage(), 0, 0);
  popMatrix();
}

boolean handsBeenStillFor(PVector oldHand, PVector newHand, int timeOut) {
  float dis = oldHand.dist(newHand);
  if(dis > 10.0 || handsStillSince == 0) {
    // reset system timer
    handsStillSince = System.currentTimeMillis();
    return false;
  } 
  else if (System.currentTimeMillis() - handsStillSince > timeOut) { 
    handsStillSince=0;
    return true;
  }
  return false;
}
void keyPressed() {
  if (key == 'c') {
    world.clear();
    setupWorld();
  }
}

void onRecognizeGesture(String strGesture, PVector idPosition, PVector endPosition)
{
  println("onRecognizeGesture - strGesture: " + strGesture + ", idPosition: " + idPosition + ", endPosition:" + endPosition);


  context.endGesture(SimpleOpenNI.GESTURE_WAVE); 
  context.startTrackingHand(endPosition);
}

void onUpdateHands(int handId, PVector pos, float time)
{
  //println("onUpdateHandsCb - handId: " + handId + ", pos: " + pos + ", time:" + time);
  PVector tempHand = new PVector();
  context.convertRealWorldToProjective(pos, tempHand);
  hand = new PVector(640-tempHand.x, tempHand.y);
}

void onDestroyHands(int handId, float time)
{
  println("onDestroyHandsCb - handId: " + handId + ", time:" + time);

  hand = null;
  context.startGesture(SimpleOpenNI.GESTURE_WAVE);
}
Sign In or Register to comment.