title: Calculate Angle between two 3D Vectors
body: <p>Note: I have absolutely no clue about Vector math, especially not in 3D.</p>
  
  <p>I am currently working on some Javascript code that determines if a Finger that got captured by a Leap Motion Controller is extended (i.e. completely straight) or not.</p>
  
  <p>Leap Motion provides us with an API that gives us Object for Hands, Fingers and Bones. Bones in particular have several properties, such as position Vectors, direction Vectors and so on, see <a href="https://developer.leapmotion.com/documentation/javascript/api/Leap.Bone.html#id32" rel="nofollow">here for the Documentation</a>.</p>
  
  <p>My idea was to take the Distal Phalang (tip of your finger) and Proximal Phalang (first bone of your finger), calculate the angle between them by getting the dot product of the two direction Vectors of the bones and then decide if it is straight or not. Like this, essentially:</p>
  
  <pre><code>var a = hand.indexFinger.distal.direction();
  var b = hand.indexFinger.proximal.direction();
  var dot = Leap.vec3.dot(a,b);
  var degree = Math.acos(dot)*180/Math.PI;
  </code></pre>
  
  <p>The issue here is that these values are not reliable, especially if other fingers move about. It seems like the direction Vectors of the bones change when other fingers change direction (???).</p>
  
  <p>For example, when all my Fingers are extended, the value of degree is roughly 0 and fluctuates between -5 and 5. When I make a fist, the value shoots up to 10, 15, 20. Logging the values of the direction Vectors reveals that they indeed do get changed, but how does this make sense? The Finger doesn't move, so its direction should stay the same.</p>
  
  <p>Even worse for the thumb, the values don't add up there at all. An extended thumb can get values similar to the IndexFinger, but rotation the thumb upwards or downwards has changes in the range of 60 degrees!</p>
  
  <p>I've tried using positional values instead, which gives me NaN results because the values seem to be to big.</p>
  
  <p>So, my question is: how could I reliably calculate the angle between two Vectors? What am I missing here?</p>