Using Leap Motion instead of mouseX and mouseY

edited April 2014 in Library Questions

Hello, i'm playing around with the leap motion and processing and basically i'm trying to mix up a couple of sketches i found but somehow i really can't find a solution for something i thought it was simple...

so this is a simple sketch i found and modified that tracks one single finger from the leap motion and writes a small dot:


    import com.leapmotion.leap.*;

    int width = 800;
    int height = 600;
    color canvasColor = 0xffffff;
    float alphaVal = 10;

    Controller leap = new Controller();

    void setup()
    {
       frameRate(120);
       size(width, height);
       background(canvasColor);
       stroke(0x00ffffff);
    }


    void draw(){
      Frame frame = leap.frame();
      Pointable pointer = frame.pointables().frontmost();
      if( pointer.isValid() )
      {

        color frontColor = color( 255, 0, 0, alphaVal );

        InteractionBox iBox = frame.interactionBox();
        Vector tip = iBox.normalizePoint(pointer.tipPosition());
        fingerPaint(tip, frontColor);
      }
    }

    void fingerPaint(Vector tip, color paintColor)
    {
       fill(paintColor);
        float x = tip.getX() * width;
        float y = height - tip.getY() * height;
        ellipse( x, y, 10, 10);   
    }

    void keyPressed()
    {
       background(canvasColor);
    }

And this is a nice sketch i found on openprocessing.org by user Raven Kwok (original here: http://www.openprocessing.org/sketch/143842) that i'm trying to modify. I'm trying to have the leap motion controller control the generation point instead of the mouse position. Does anyone have an idea how to approach this?


import com.leapmotion.leap.*;

ArrayList<Particle> pts;
boolean onPressed, cleanUp;
PFont f;

Controller leap = new Controller();

void setup() {
  size(720, 720, P2D);
  smooth();
  frameRate(30);
  colorMode(HSB);
  rectMode(CENTER);

  pts = new ArrayList<Particle>();

  cleanUp = true;

  background(255);
}

void draw() {
  if (cleanUp) {
    background(255);
    fill(128);
  }

  if (onPressed) {
    for (int i=0;i<10;i++) {
      Particle newP = new Particle(mouseX, mouseY, i+pts.size(), i+pts.size());
      pts.add(newP);
    }
  }

  for (int i=0; i<pts.size(); i++) {
    Particle p = pts.get(i);
    p.update();
    p.display();
  }

  for (int i=pts.size()-1; i>-1; i--) {
    Particle p = pts.get(i);
    if (p.dead) {
      pts.remove(i);
    }
  }
}

void mousePressed() {
  onPressed = true;

  }


void mouseReleased() {
  onPressed = false;
}


class Particle{
  PVector loc, vel, acc;
  int lifeSpan, passedLife;
  boolean dead;
  float alpha, weight, weightRange, decay, xOffset, yOffset;
  color c;

  Particle(float x, float y, float xOffset, float yOffset){
    loc = new PVector(x,y);

    float randDegrees = random(360);
    vel = new PVector(cos(radians(randDegrees)), sin(radians(randDegrees)));
    vel.mult(random(5));

    acc = new PVector(0,0);
    lifeSpan = int(random(30, 90));
    decay = random(0.75, 0.9);
    c = color(random(255),random(255),255);
    weightRange = random(3,50);

    this.xOffset = xOffset;
    this.yOffset = yOffset;
  }

  void update(){
    if(passedLife>=lifeSpan){
      dead = true;
    }else{
      passedLife++;
    }

    alpha = float(lifeSpan-passedLife)/lifeSpan * 70+50;
    weight = float(lifeSpan-passedLife)/lifeSpan * weightRange;

    acc.set(0,0);

    float rn = (noise((loc.x+frameCount+xOffset)*0.01, (loc.y+frameCount+yOffset)*0.01)-0.5)*4*PI;
    float mag = noise((loc.y+frameCount)*0.01, (loc.x+frameCount)*0.01);
    PVector dir = new PVector(cos(rn),sin(rn));
    acc.add(dir);
    acc.mult(mag);

    float randDegrees = random(360);
    PVector randV = new PVector(cos(radians(randDegrees)), sin(radians(randDegrees)));
    randV.mult(0.5);
    acc.add(randV);

    vel.add(acc);
    vel.mult(decay);
    vel.limit(3);
    loc.add(vel);
  }

  void display(){
    strokeWeight(weight+1.5);
    stroke(0, alpha);
    point(loc.x, loc.y);

    strokeWeight(weight);
    stroke(c);
    point(loc.x, loc.y);
  }
}

Thank you in advance guys, emilio

Tagged:

Answers

  • Answer ✓

    Hey, here's a program I used to make my priject with the leap motion :

    import de.voidplus.leapmotion.*; 
    LeapMotion leap; 
    ArrayList<PVector> points; 
    PVector fp; 
    
    void setup() {
      size(800, 500, P3D);
      background(255);
      leap = new LeapMotion(this);
      points = new ArrayList<PVector>(); 
      smooth(8);
      noStroke();
    }
    
    void draw() {
      background(255);
      int fps = leap.getFrameRate();
      frameRate(fps);
      for (Hand hand : leap.getHands()) {
        for (Finger finger : hand.getFingers()) {
          fp   = finger.getPosition(); 
    
          if (fp.z <= 30) {
            fill(0);
            ellipse(fp.x, fp.y, constrain(fp.z, 10, 20), constrain(fp.z, 10, 20));
          }
          else if (fp.z > 30) {
            points.add(new PVector(fp.x, fp.y));
          }
        }
      }
      for (int i = points.size()-1; i >= 0; i--) {
        PVector p = points.get(i);
        fill(23, 77, 255);
        ellipse(p.x, p.y, 20, 20);
      }
    }
    
    void keyPressed() {
      if (key == 32) {
        points = new ArrayList<PVector>();
      }
    }
    

    This allows you to draw with your fingers, so from this you can easily see how it actually works, it's pretty easy. Good luck :)

  • Answer ✓

    Btw : I didn't create this program. It is coming from here: http://tutoprocessing.com/avance/leap-motion-processing/

  • Thank you very much Shiyo! Both the sketch and the link are really useful, i'll post later my result.

  • You're welcome :)

  • I made it, here's the sketch. I'm pretty sure it's quite "dirty" and needs to be cleaned (any advice is welcome of course!). Now i'm trying to drive the same control parameters from the Leap Motion to a MIDI output (routed to Ableton Live) using the library MIDI Bus. Kudos again to Raven Kvok the creator of the original sketch on openprocessing.org and to Shiyo!

    import de.voidplus.leapmotion.*; 
    LeapMotion leap;
    ArrayList<PVector> points; 
    PVector fp;
    
    
    ArrayList<Particle> pts;
    boolean onPressed, cleanUp;
    PFont f;
    
    
    
    void setup() {
      size(720, 720, P2D);
      smooth();
      frameRate(30);
      colorMode(HSB);
      rectMode(CENTER);
      leap = new LeapMotion(this);
      points = new ArrayList<PVector>();  // Create an empty ArrayList
      smooth(8);
      noStroke();
      pts = new ArrayList<Particle>();
    
      //cleanUp = true;
    
      background(255);
    }
    
    void draw() {
      if (cleanUp) {
        background(255);
        fill(128);
      }
      int fps = leap.getFrameRate();
      frameRate(fps);
      // Mani
      for (Hand hand : leap.getHands()) {
        // Dita
        for (Finger finger : hand.getFingers()) {
          fp   = finger.getPosition(); 
          if (fp.z <= 30) {
           points = new ArrayList<PVector>();
          }
    
          else if (fp.z > 30) {
            points.add(new PVector(fp.x, fp.y));
          }
        }
      }
    
    {
       for (int i = points.size()-1; i >= 0; i--) {
        PVector p = points.get(i);
          Particle newP = new Particle(p.x, p.y, i+pts.size(), i+pts.size());
          pts.add(newP);
        }
      }
    
      for (int i=0; i<pts.size(); i++) {
        Particle p = pts.get(i);
        p.update();
        p.display();
      }
    
      for (int i=pts.size()-1; i>-1; i--) {
        Particle p = pts.get(i);
        if (p.dead) {
          pts.remove(i);
        }
      }
    }
     void keyPressed() {
      if (key == 'c') {
        for (int i=pts.size()-1; i>-1; i--) {
          Particle p = pts.get(i);
          pts.remove(i);
        }
        background(255);
      }
    }
    
    
    class Particle{
      PVector loc, vel, acc;
      int lifeSpan, passedLife;
      boolean dead;
      float alpha, weight, weightRange, decay, xOffset, yOffset;
      color c;
    
      Particle(float x, float y, float xOffset, float yOffset){
        loc = new PVector(x,y);
    
        float randDegrees = random(360);
        vel = new PVector(cos(radians(randDegrees)), sin(radians(randDegrees)));
        vel.mult(random(5));
    
        acc = new PVector(0,0);
        lifeSpan = int(random(30, 90));
        decay = random(0.75, 0.9);
        c = color(random(255),random(255),255);
        weightRange = random(3,50);
    
        this.xOffset = xOffset;
        this.yOffset = yOffset;
      }
    
      void update(){
        if(passedLife>=lifeSpan){
          dead = true;
        }else{
          passedLife++;
        }
    
        alpha = float(lifeSpan-passedLife)/lifeSpan * 70+50;
        weight = float(lifeSpan-passedLife)/lifeSpan * weightRange;
    
        acc.set(0,0);
    
        float rn = (noise((loc.x+frameCount+xOffset)*0.01, (loc.y+frameCount+yOffset)*0.01)-0.5)*4*PI;
        float mag = noise((loc.y+frameCount)*0.01, (loc.x+frameCount)*0.01);
        PVector dir = new PVector(cos(rn),sin(rn));
        acc.add(dir);
        acc.mult(mag);
    
        float randDegrees = random(360);
        PVector randV = new PVector(cos(radians(randDegrees)), sin(radians(randDegrees)));
        randV.mult(0.5);
        acc.add(randV);
    
        vel.add(acc);
        vel.mult(decay);
        vel.limit(3);
        loc.add(vel);
      }
    
      void display(){
        strokeWeight(weight+1.5);
        stroke(0, alpha);
        point(loc.x, loc.y);
    
        strokeWeight(weight);
        stroke(c);
        point(loc.x, loc.y);
      }
    }
    
Sign In or Register to comment.