User Tools

Site Tools


cs276:calm_hands_fingers
import de.voidplus.leapmotion.*;
 
/*
 * 
 * +== OBJECTIVES ==+
 * 1. Add a red rectangle with a dark red border to the center of the screen
 * 2. Make a red circle with a black border appear over your index finger
 * 3. Make the rectangle appear only when your hand is on the screen
 * 4. Change the background color when you are in the "hovering" zone
 *
 * ==Shape presets for use in this exercise==
 * 1. Rectangle
 *      rectMode(CENTER);
 *      strokeWeight(pixels);
 *      stroke(red, green, blue);
 *      fill(#color);
 *      rect(x position, y position, width, height);
 * 2. Circle
 *      strokeWeight(pixels);
 *      stroke(red, green, blue);
 *      fill(#color);
 *      ellipse(x position, y position, width, height);
 * 
 * ==Location hints==
 * 1. Center screen
 *      height/2, width/2
 * 2. Finger position
 *      finger.getPosition().x, finger.getPosition().y
 */
 
int INITC = 205;
color changeC = color(INITC, INITC, INITC);
boolean hover = false;
LeapMotion leap;
 
// Initialize the stage (Runs once in the beginning of your sketch)
void setup() 
{
  size(1024, 768, OPENGL); //inital size of screen
  background(255); //sets a white background
  leap = new LeapMotion(this); //sets up an object to use to access the leap
}
 
// Initialize contents the draw method (Runs once for each frame displayed in your sketch)
void draw() 
{
  // Reset the background to white at the beginning of each frame
  background(changeC);
 
  // ========= HANDS =========
 
  for (Hand hand : leap.getHands ()) {
 
    // ----- BASICS ----- (Initialize hand attributes for each hand seen by the leap object)
    int     hand_id          = hand.getId();
    PVector hand_position    = hand.getPosition();
    PVector hand_stabilized  = hand.getStabilizedPosition();
    PVector hand_direction   = hand.getDirection();
    PVector hand_dynamics    = hand.getDynamics();
    float   hand_roll        = hand.getRoll();
    float   hand_pitch       = hand.getPitch();
    float   hand_yaw         = hand.getYaw();
    boolean hand_is_left     = hand.isLeft();
    boolean hand_is_right    = hand.isRight();
    float   hand_grab        = hand.getGrabStrength();
    float   hand_pinch       = hand.getPinchStrength();
    float   hand_time        = hand.getTimeVisible();
    PVector sphere_position  = hand.getSpherePosition();
    float   sphere_radius    = hand.getSphereRadius();
 
    // ----- SPECIFIC FINGER ----- (Initialize fingers for each hand seen by the leap object)
    Finger  finger_thumb     = hand.getThumb();
    Finger  finger_index     = hand.getIndexFinger();
    Finger  finger_ring      = hand.getRingFinger(); 
    Finger  finger_pink      = hand.getPinkyFinger();
 
    // ----- DRAWING ----- (Actions to be taken when a hand is being seen)
 
    stroke(180, 15, 15);
    strokeWeight(4);
    fill(225, 0, 0);
    rect(width/2 - 100, height/2 - 100, 200, 200);
 
 
    // ========= FINGERS =========
 
    for (Finger finger : hand.getFingers ()) 
    {
 
      // ----- BASICS ----- (Initialize finger attributes for each finger on a hand)
      int     finger_id         = finger.getId();
      PVector finger_position   = finger.getPosition();
      PVector finger_stabilized = finger.getStabilizedPosition();
      PVector finger_velocity   = finger.getVelocity();
      PVector finger_direction  = finger.getDirection();
      float   finger_time       = finger.getTimeVisible();
 
      // ----- SPECIFIC FINGER ----- (Actions to be taken with a specific finger)
      switch(finger.getType()) 
      {
      case 0: // Thumb
        break;
      case 1: // Index
        stroke(0);
        strokeWeight(4);
        ellipse(finger_position.x, finger_position.y, 30, 30);
        break;
      case 2: // Middle
        break;
      case 3: // Ring
        break;
      case 4: // Pinky
        break;
      }
 
      // ----- TOUCH EMULATION ----- (Actions to be taken when fingers are located in a specific distance zone)
      int     touch_zone        = finger.getTouchZone();
      float   touch_distance    = finger.getTouchDistance();
 
      switch(touch_zone) 
      {
      case -1: // None
        hover = false;
        break;
      case 0: // Hovering
        if (!hover) { 
          changeC = color(random(255), random(255), random(255));
          hover = true;
        }
        println("Hovering (#" + finger_id + "): " + touch_distance);
        break;
      case 1: // Touching
        println("Touching (#" + finger_id + "): " + touch_distance);
        hover = false;
        break;
      }
    }
  }
}
cs276/calm_hands_fingers.txt · Last modified: 2016/11/05 11:09 by scarl