Search code examples
javaprocessinggestureleap-motion

Leap motion - Processing - Swipe Gesture direction?


I have been using 2 libraries up til now for my leap motion project as a bodge because they both allowed different functions, unique (better implemented) to each other. However I would like to just use one library as multiple is causing issues.

Basically I am using Leap Motion by Michael Heuer ( I need to use this library because it is currently the only library I can find that allows me to set optimize hmd and also scale factor).

The gesture implementation is as below, is there a way to get the swipe direction from this?

void onInit(final Controller controller)
{
  controller.enableGesture(Gesture.Type.TYPE_SWIPE);
  // enable top mounted policy
  controller.setPolicyFlags(Controller.PolicyFlag.POLICY_OPTIMIZE_HMD);
}

void onFrame(final Controller controller)
{
  Frame frame = controller.frame();
  for (Gesture gesture : frame.gestures())
  {
      if ("TYPE_SWIPE".equals(gesture.type().toString()) && "STATE_START".equals(gesture.state().toString())) {
    }

    println("gesture " + gesture + " id " + gesture.id() + " type " + gesture.type() + " state " + gesture.state() + " duration " + gesture.duration() + " durationSeconds " + gesture.durationSeconds()); 

 }
}

I tried gesture.direction() in the vain hope it might work but direction is not a recognized function.

Any help is much appreciated! Thanks in advance! Will


Solution

  • import com.leapmotion.leap.Controller;
    import com.leapmotion.leap.Frame;
    import com.leapmotion.leap.Gesture;
    import com.leapmotion.leap.Hand;
    import com.leapmotion.leap.HandList;
    import com.leapmotion.leap.processing.LeapMotion;
    import com.leapmotion.leap.SwipeGesture;
    import com.leapmotion.leap.Vector;
    
    LeapMotion leapMotion;
    
    void setup()
    {
      size(16 * 50, 9 * 50);
      background(20);
    
      leapMotion = new LeapMotion(this);
    }
    
    void draw()
    {
      fill(20);
      rect(0, 0, width, height);
    }
    
    void onInit(final Controller controller)
    {
      controller.enableGesture(Gesture.Type.TYPE_SWIPE);
      // enable top mounted policy
      //controller.setPolicyFlags(Controller.PolicyFlag.POLICY_OPTIMIZE_HMD);
    }
    
    void onFrame(final Controller controller)
    {
      Frame frame = controller.frame();
      for (Gesture gesture : frame.gestures())
      {
    
     if(gesture.type() == Gesture.Type.TYPE_SWIPE) {
        SwipeGesture swipeGesture = new SwipeGesture(gesture);
    
        Vector swipeVector  = swipeGesture.direction();
        println("swipeVector : " + swipeVector);
    
        float swipeDirection = swipeVector.getX();
        println(swipeDirection);
       }
    }
    
      HandList hands = frame.hands();
      //println(hands.count());
    }
    

    Just for future reference, hopefully this can help someone else out!