# Calculate the angle between two 3D vectors

Note: I have absolutely no clue about Vector math, especially not in 3D.

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.

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 here for the Documentation.

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:

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;



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 (???).

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.

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!

I've tried using positional values instead, which gives me NaN results because the values seem to be to big.

So, my question is: how could I reliably calculate the angle between two Vectors? What am I missing here?

The correct formula is

cos(angle) = dot(a,b)/(norm(a)*norm(b))



where norm is the euclidean norm or length.

You should have gotten a wrong result, but the lengths of a and b should be constant, so the result should have been consistently wrong…
{"ft_lang_label":"__label__en","ft_lang_prob":0.8990321,"math_prob":0.9204727,"size":2108,"snap":"2020-10-2020-16","text_gpt3_token_len":479,"char_repetition_ratio":0.11929658,"word_repetition_ratio":0.0056818184,"special_character_ratio":0.2243833,"punctuation_ratio":0.14252874,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9883513,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-03-30T16:11:49Z\",\"WARC-Record-ID\":\"<urn:uuid:205c1e1e-ca4f-4a81-b2c1-230c9e369358>\",\"Content-Length\":\"20284\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5720d4f2-4b48-47a5-86ad-2acbcf94457f>\",\"WARC-Concurrent-To\":\"<urn:uuid:47c3fedd-752e-44da-8c9d-454532ffc63d>\",\"WARC-IP-Address\":\"104.31.72.112\",\"WARC-Target-URI\":\"https://www.codesd.com/item/calculate-the-angle-between-two-3d-vectors.html\",\"WARC-Payload-Digest\":\"sha1:KR5MHOBNRKP6IY6OPMW4TBLQOPZR73PI\",\"WARC-Block-Digest\":\"sha1:4IXCTXJX2M5UJTME3QKEJB6R3HH7URQP\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-16/CC-MAIN-2020-16_segments_1585370497171.9_warc_CC-MAIN-20200330150913-20200330180913-00157.warc.gz\"}"}