Rotating Object with Leap Motion

Hi All,

I am having trouble using Processing with my Leap Motion device. I want to be able to rotate an image depending on where my pinky is on the screen. Therefore, I am trying to find the angle between my thumb (where the image should be ‘pinned’) and where my pinky is. Although I have not dealt with Processing very much and need help in doing so. I have put in bold my equation to find the angle, as well as the images I want rotated.

Side Note: Is there a way to ‘pin’ a part of an image to the canvas acting as a rotation point? If so, could someone help me with that also.

import de.voidplus.leapmotion.*;

float RightThumbX;
float RightThumbY;
float LeftThumbX;
float LeftThumbY;
float RightPinkyX;
float RightPinkyY;
float LeftPinkyX;
float LeftPinkyY;
boolean initiate = false;
float HeadX;
float HeadY;
boolean BothHands;
float P1WingRY = (RightPinkyY + HeadY/3)-150;
float P1WingLY = (LeftPinkyY + HeadY/3)-150;
float P1WingRX = (RightPinkyX + HeadX/3);
float P1WingLX = (LeftPinkyX + HeadX/3);
float P2WingRY = (RightPinkyY + HeadY/2)-150;
float P2WingLY = (LeftPinkyY + HeadY/2)-150;
float P2WingRX = (RightPinkyX + HeadX/2);
float P2WingLX = (LeftPinkyX + HeadX/2);
PImage wingL;
PImage wingR;
PVector head, rightThumb;
float angleRight;

LeapMotion leap;

void setup() {
  size(980, 520);
  background(255);
  // ...
  wingL = loadImage("WingsLeft.png");
  wingR = loadImage("WingsRight.png");
  leap = new LeapMotion(this);

}


// ======================================================
// 1. Callbacks

void leapOnInit() {
  // println("Leap Motion Init");
}
void leapOnConnect() {
  // println("Leap Motion Connect");
}
void leapOnFrame() {
  // println("Leap Motion Frame");
}
void leapOnDisconnect() {
  // println("Leap Motion Disconnect");
}
void leapOnExit() {
  // println("Leap Motion Exit");
}


void draw() {
  background(255);
   //translate(width/2, height/2);
  // ...

  int fps = leap.getFrameRate();
  for (Hand hand : leap.getHands ()) {


    // ==================================================
    // 2. Hand


    boolean handIsLeft         = hand.isLeft();
    boolean handIsRight        = hand.isRight();

    // --------------------------------------------------
    // Drawing
        
    hand.draw();


    // ==================================================
    // 3. Arm

    if (hand.hasArm()) {
      Arm     arm              = hand.getArm();
    }


    for (Finger finger : hand.getFingers()) {

      switch(finger.getType()) {
      case 0:
        // System.out.println("thumb");
        break;
      case 1:
        // System.out.println("index");
        break;
      case 2:
        // System.out.println("middle");
        break;
      case 3:
         //System.out.println("ring");
        break;
      case 4:
        // System.out.println("pinky");
        break;
      }
      
      // Clarify that both hands are present to start scanning
      if (handIsLeft && handIsRight) {
        BothHands = true;
      } else {
        BothHands = false;
      }

     if (handIsLeft) {
       switch(finger.getType()) {
      case 0:
        LeftThumbX = finger.getPosition().x;
        LeftThumbY = finger.getPosition().y;
        break;
       }
    }
    
     if (handIsRight) {
       switch(finger.getType()) {
      case 0:
        RightThumbX = finger.getPosition().x;
        RightThumbY = finger.getPosition().y;
        break;
       }
    }
    
     if (handIsLeft) {
       switch(finger.getType()) {
      case 4:
        LeftPinkyX = finger.getPosition().x;
        LeftPinkyY = finger.getPosition().y;
        break;
       }
    }
    
     if (handIsRight) {
       switch(finger.getType()) {
      case 4:
        RightPinkyX = finger.getPosition().x;
        RightPinkyY = finger.getPosition().y;
        break;
       }
    }


    if (dist(RightThumbX, RightThumbY, LeftThumbX,LeftThumbY) < 60) {
      initiate = true;
    } else { 
     initiate = false; 
    }

    // Find midpoint of thumbs to determine head position
    HeadX = (RightThumbX + LeftThumbX)/2;
    HeadY = (RightThumbY + LeftThumbY)/2;
    
    head = new PVector(HeadX, HeadY);
    rightThumb = new PVector(RightThumbX, RightThumbY); 
    
    
    
    if (initiate) {
     fill (#00a9ff);
     ellipse(HeadX, HeadY, 70, 70);
     curve(P1WingRX, P1WingRY, RightPinkyX, RightPinkyY, HeadX, HeadY, HeadX, HeadY);
     curve(P1WingLX, P1WingLY, LeftPinkyX, LeftPinkyY, HeadX, HeadY, HeadX, HeadY);
     strokeWeight(4);
     **image(wingR, HeadX, HeadY-140);**
     **image(wingL, HeadX-410, HeadY-140);**
     **angleRight = (atan(RightPinkyX-HeadX)/(HeadY-RightPinkyY));**
     
    }

   print(angleRight + "                  ");   

    // ==================================================
    // 6. Tools

    for (Tool tool : hand.getTools()) {
      int     toolId           = tool.getId();
      PVector toolPosition     = tool.getPosition();
      PVector toolStabilized   = tool.getStabilizedPosition();
      PVector toolVelocity     = tool.getVelocity();
      PVector toolDirection    = tool.getDirection();
      float   toolTime         = tool.getTimeVisible();

      // ------------------------------------------------
      // Drawing:
      // tool.draw();

      // ------------------------------------------------
      // Touch emulation

      int     touchZone        = tool.getTouchZone();
      float   touchDistance    = tool.getTouchDistance();

      switch(touchZone) {
      case -1: // None
        break;
      case 0: // Hovering
        // println("Hovering (#" + toolId + "): " + touchDistance);
        break;
      case 1: // Touching
        // println("Touching (#" + toolId + ")");
        break;
      }
    }
  }


  // ====================================================
  // 7. Devices

  for (Device device : leap.getDevices()) {
    float deviceHorizontalViewAngle = device.getHorizontalViewAngle();
    float deviceVericalViewAngle = device.getVerticalViewAngle();
    float deviceRange = device.getRange();
  }


  }
}
1 Like

The lines aren’t in bold for some reason, they are the last three lines in the if (initiate) statement towards the bottom part of the code.