Kinect Arsonist

Our first assignment for Computational Cameras was to alter the code of a hand tracking program for the Kinect. The Kinect was initially designed as a gaming device, so I found myself reminiscing about my gaming days and the many hours lost to Diablo II. I usually played a sorceress, and every sorceress’ first skill is the fireball spell. (Nerd alert.) I thought it might be fun to set things on fire inside my little virtual world. Maybe in the next iteration I will add some beasts to vanquish.

I made a png file of a flame image in Photoshop, which I then used to replace the red circle in the original hand tracking program. When you click the F key, the flame image is drawn wherever you move your hand–set the screen on fire! Clicking the C key clears the fiery path from the screen.

[code lang="java"]import SimpleOpenNI.*;

SimpleOpenNI context; 
boolean handsTrackFlag = false; 
PVector handVec = new PVector();
PImage flame;

boolean trailsMode = false;  //flame trails on/off

ArrayList positions;

void setup() {
  size(640, 480);  // strange, get drawing error in the cameraFrustum if i use P3D, in opengl there is no problem
  //size(1024,768,OPENGL);
  context = new SimpleOpenNI(this);
  context.setMirror(false);
  context.enableDepth();
  //unfortunately have to track guestures to get hands
  context.enableGesture();
  context.enableHands();
  context.addGesture("RaiseHand");
  fill(255, 0, 0);
  flame = loadImage("flame.png");
  
  println("w: " +flame.width + " h: " + flame.height);
  
  positions = new ArrayList();
}
void draw() {
  // update the cam
  context.update();
  //paint the image
  image(context.depthImage(), 0, 0, width, height);
  if (handsTrackFlag) 
  {
    PVector myPositionScreenCoords  = new PVector(); //storage device
    //convert the weird kinect coordinates to screen coordinates.
    context.convertRealWorldToProjective(handVec, myPositionScreenCoords);
    tint(255, 230);
    //image(flame, myPositionScreenCoords.x, myPositionScreenCoords.y);
    
    PVector currentPosition = new PVector(myPositionScreenCoords.x, myPositionScreenCoords.y);
    
    if(trailsMode){
          positions.add(currentPosition);

      for(int i = 0; i < positions.size(); i++)
      {
        PVector flamePos = positions.get(i);
        image(flame, flamePos.x - (flame.width/2), flamePos.y - (flame.height/2)); 
      }
    } 
    else 
    {
      image(flame, currentPosition.x - (flame.width/2), currentPosition.y- (flame.height/2));
    }
  }
}

// ----------------------------------------------------------------- // hand events
void onCreateHands(int handId, PVector pos, float time) {
  println("onCreateHands - handId: " + handId + ", pos: " + pos + ", time:" + time);
  handsTrackFlag = true;
  handVec = pos;
}
void onUpdateHands(int handId, PVector pos, float time) {
  //println("onUpdateHandsCb - handId: " + handId + ", pos: " + pos + ", time:" + time);
  //store the location of the hand in a vector object
  handVec = pos;
}
void onDestroyHands(int handId, float time) {
  println("onDestroyHandsCb - handId: " + handId + ", time:" + time);
  handsTrackFlag = false;
  //go back to looking for the guesture that gave you hand.
  context.addGesture("RaiseHand");
}
// ----------------------------------------------------------------- // gesture events
void onRecognizeGesture(String strGesture, PVector idPosition, PVector endPosition) {
  println("onRecognizeGesture - strGesture: " + strGesture + ", idPosition: " + idPosition + ", endPosition:" + endPosition);
  //stop looking for the gesture
  context.removeGesture(strGesture);
  //use the location of this guesture tell you where to start tracking the hand
  context.startTrackingHands(endPosition);
}
void onProgressGesture(String strGesture, PVector position, float progress) {
  //println("onProgressGesture - strGesture: " + strGesture + ", position: " + position + ", progress:" + progress);
}
// ----------------------------------------------------------------- // Keyboard event
void keyPressed() {
  switch(key)
  {
  case ' ':
    context.setMirror(!context.mirror());
    break;
  case 'f':  //leave flame trails
    trailsMode = !trailsMode;
    break;
  case 'c':  //clear flame trails
    positions.clear();
    break;
  }
}[/code]