Search code examples
javascriptleap-motion

How to detect circle gesture direction?


THis is my code gist.

Leap.loop({enableGestures: true}, function(frame) {
var gestures = frame.gestures;

for (var i = 0; i < gestures.length; i++) { 
  // I want to do something when draw circle with one pointable 
   if (gesture.type == "circle" && gesture.state == "stop" && gesture.pointableIds.length == 1) {
    var isClockWise = ? ;//  how to know the direction of circle ?
  }
}
} );

How to know circle is clockwise or counter clock wise with gesture object ?

I was using leap motion only 2 days and really need your help.


Solution

  • Looking on the C++ sample given on the leap website, piece of code is given to detect is the circle is clockwise.

    C++ code :

    if (circle.pointable().direction().angleTo(circle.normal()) <= PI/4)
       {
          clockwiseness = "clockwise";
       }
       else
       {
          clockwiseness = "counterclockwise";
       }
    

    I haven't used the Javascript API, but I think this can be something equivalent This code hasn't been tested, but in Javascript it may be something like :

    // considere your gesture is a circle, and there is at least one pointable object.
    if (gesture.type == "circle" && gesture.state == "stop" && gesture.pointableIds.length >= 1)
    {
      var dir = frame.pointables[gesture.pointableIds[0] ].direction; // get direction of the Pointable used for the circle gesture
      var angle = dir.AngleTo (circle.normal);
      var isClockWise =  angle <= (3.14 / 4);
    }
    

    Got all infos from Leap JS API from GitHub and Leap Motion Developers site

    -> Be careful frame.pointables return pointables objects given in arbitrary order.(Cf JS API doc). This piece of code is just for the explanation of the algorithm