LeapMotion not communicating with class

edited December 2015 in Library Questions

Hey everyone. I'm trying to update this sketch to receive input from the LeapMotion. I've gotten the Leap working but the data from the fingers isn't updating in the class. Does anyone have any ideas on how to get this to work? I've tried initializing the Leap in different way and in different places in the sketch but nothing has worked so far. Any help would be appreciated! I'm really stuck

Here's the main code:

import processing.video.*;

import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentHashMap;
//import com.leapmotion.leap.*;

import com.leapmotion.leap.Controller;
import com.leapmotion.leap.Finger;
import com.leapmotion.leap.Frame;
import com.leapmotion.leap.Hand;
import com.leapmotion.leap.Tool;
import com.leapmotion.leap.Vector;
import com.leapmotion.leap.processing.LeapMotion;




import gifAnimation.*;


//import codeanticode.gsvideo.*;
import processing.core.PApplet;

//gifs
//GifMaker gifExport;

//Importing Leap
LeapMotion leap;
//Controller leap = new Controller();
Vector palmPosition;


float fingerX; 
float fingerY;
float fingerZ;

MyPoint[] pts;
PImage img;
Capture cam;
int nbW = 50, nbH = 25;




void setup(){
 size(800, 480, P2D);
 leap = new LeapMotion(this);
 palmPosition = new Vector();
 cam = new Capture(this, width, height, 30);
 cam.start();
 initialize();
 stroke(100, 80);
}

//Converting the img size to point objects
void initialize()
{
 fingerX = leap.leapToSketchX(palmPosition.getX());
 fingerY = leap.leapToSketchY(palmPosition.getY());
  pts = new MyPoint[nbW*nbH];
  for(int j=0; j<nbH;j++){
     for(int i=0; i<nbW;i++){
        pts[i + j*nbW] = new MyPoint(new PVector(i * width/nbW, j * height/nbH));
     }
  }
}


void onFrame(final Controller controller)
{
  Frame frame = controller.frame();

  palmPosition = frame.hands().get(0).palmPosition();
  for (Finger finger : frame.fingers())
  {
    Finger mostForwardFinger = frame.fingers().frontmost();
    Vector fingX = mostForwardFinger.tipPosition();

    println(fingX);
 }
  println(palmPosition);
println("X and Y: " + fingerX + "," + fingerY);

}


void draw(){

  //starting the camera
  if (cam.available() == true) {
    cam.read();
    pushMatrix();
    translate(width, 0);
    scale(-1,1);
    image(cam, 0, 0);
    popMatrix();
    cam.updatePixels();
    img = get();
    img.resize(nbW, nbH);


     beginShape(TRIANGLES);
   // beginShape(QUAD);
//    texture(img);
    PVector a, b, c, d;
    float x1, x2, y1, y2;
    for(int j=0; j<nbH; j++){
        for(int i=0; i<nbW; i++){
           if(j<nbH-1 && i<nbW-1) 
           {     

             a = pts[i + j*nbW].pos;
             b = pts[i+1 + j*nbW].pos;
             c = pts[i+1 + (j+1)*nbW].pos;
             d = pts[i + (j+1)*nbW].pos;

              // these are the floats do something
             x1 = (a.x + b.x + d.x) / 3;
             x2 = (b.x + c.x + d.x) / 3;
             y1 = (a.y + b.y + d.y) / 3;
             y2 = (b.y + c.y + d.y) / 3;

            //using the floats above to grab data from the image created from the cam
            //uses that data to draw new vertex'es
            fill(img.get(int(x1/width*nbW), int(y1/height*nbH)));
            vertex(a.x, a.y);
            vertex(b.x, b.y);
            vertex(d.x, d.y);

            fill(img.get(int(x2/width*nbW), int(y2/height*nbH)));
            vertex(b.x, b.y);
            vertex(c.x, c.y);
            vertex(d.x, d.y);            


            vertex(a.x, a.y, int(a.x/width*nbW), int(a.y/height*nbH));
            vertex(b.x, b.y, int(b.x/width*nbW), int(b.y/height*nbH));
            vertex(c.x, c.y, int(c.x/width*nbW), int(c.y/height*nbH));
            vertex(d.x, d.y, int(d.x/width*nbW), int(d.y/height*nbH));

           }

            pts[i + j*nbW].process();
        }
    }
    endShape(CLOSE); 
    //img.updatePixels();
  }

  }

And the class that controls the distortion:

class MyPoint
{
  final static float MAX_DIST_MOUSE = 80;//mouse influence zone
  final static float FRICTION_AIR = .92;//'air' FRICTION_AIR
  PVector target;//original position, MyPoint always tries to get back to it
  PVector f = new PVector(0, 0);//force applied to the point
  PVector pos, tmpv;  //
  int countExclude;
  float pfingerX;
  float pfingerY;



  MyPoint(PVector p_p)
  {
    pos = p_p;
    target = pos.get();
    pfingerX = fingX;
    pfingerY = fingY;


  }

  void process()
  {
   f.mult(FRICTION_AIR);   
   float d = dist(pos.x, pos.y,pfingerX, pfingerY);
   if (d < 6) d = 6;//prevent erratic behavior
   if (d < MAX_DIST_MOUSE)//mouse effect
   {
      tmpv = new PVector( [pfingerX, pfingerY);
      tmpv.sub(pos);
      float a = .83 * cos(map(d, 0, MAX_DIST_MOUSE, 0, HALF_PI));
      tmpv.mult((mousePressed ? -1 : 1) * a / d);
      f.add(tmpv);
  }

    //attracted to its original position
    tmpv = target.get();
    tmpv.sub(pos);
    tmpv.mult(.09);
    f.add(tmpv);    
    pos.add(f);   
  }

}
Sign In or Register to comment.