How to calculate the angle between two 3D vectors? (2024)

441 views (last 30 days)

Show older comments

Unnamed User on 11 Mar 2024

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/2092961-how-to-calculate-the-angle-between-two-3d-vectors

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/2092961-how-to-calculate-the-angle-between-two-3d-vectors

Edited: James Tursa on 11 Mar 2024

Open in MATLAB Online

I have two vectors that I want to calculate the angle between in 3D space, U and V. Vector U is calculated by subtracting where the first object was at Point 1 from where the object currently is at Point 2. Vector V is calculated by subtracting where the second object is at Point 2 from where the first object is at Point 2. This should essentially make vector V perpendicular-ish to vector U.

I have scoured these forums and come across multiple lines of code that apprently work:

angle = atan2(norm(cross(u,v)), dot(u,v))

CosTheta = max(min(dot(u,v)/(norm(u)*norm(v)),1),-1);

angle = acos(dot(u,v)/norm(u)*norm(v))

angle = acos(dot(u)/norm(u),v/norm(v))

All seem to produce the desired angle when I run it with changes only in 2D, so no movement in the Z domain. However as soon as I try use the above formula for 3D movement is starts producing wrong results. I've double checked what the angle should be by using an online calculator and doing the calculations by hand and the formula seem to always be off.

For example. Object 1 travelling from (128,97,138) to (78,47,88) and object 2 travelling from (0,0,0) to (50,50,0) should produce an angle of 144.735 degrees at the last step; but using any of the above formula gives 134 degrees.

Is there something I'm missing here?

Thanks!

1 Comment

Show -1 older commentsHide -1 older comments

James Tursa on 11 Mar 2024

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/2092961-how-to-calculate-the-angle-between-two-3d-vectors#comment_3095206

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/2092961-how-to-calculate-the-angle-between-two-3d-vectors#comment_3095206

You've got errors in the the last two formulae. See my post below for corrections.

Sign in to comment.

Sign in to answer this question.

Answers (2)

James Tursa on 11 Mar 2024

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2092961-how-to-calculate-the-angle-between-two-3d-vectors#answer_1423761

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2092961-how-to-calculate-the-angle-between-two-3d-vectors#answer_1423761

Edited: James Tursa on 11 Mar 2024

Open in MATLAB Online

See a discussion here (includes a tiny angle example):

https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab?s_tid=srchtitle

A comparison (correcting the formulae you wrote):

% most robust, most accurate, recovers tiny angles very well, slowest

atan2(norm(cross(u,v)), dot(u,v))

% robust, does not recover tiny angles, faster

max(min(dot(u,v)/(norm(u)*norm(v)),1),-1)

% not robust (may get domain error), does not recover tiny angles, fasterer

acos(dot(u,v)/(norm(u)*norm(v)))

% not robust (may get domain error), does not recover tiny angles, fasterer

acos(dot(u/norm(u),v/norm(v)))

% not robust (may get domain error), does not recover tiny angles, fastest

acos(dot(u,v)/sqrt(dot(u,u)*dot(v,v)))

By "domain error", I mean the dot( ) argument magnitude can be slightly larger than 1 in some u==v cases due to floating point numerical effects resulting in an unintended complex result. The "robust" methods are protected against this. Also, if one of the arguments is 0 vector, the atan2( ) method returns 0 while the other methods will return NaN. So the atan2( ) method is the "most robust" in this sense. Caveat: This assumes the language in use has atan2(0,0) coded this way (like MATLAB, Java, etc.), but is not guaranteed across all languages.

That last method is one I threw in that is essentially the same as method 3, except that only one sqrt( ) is needed instead of two norm( ) calls.

Finally, the relative speed rankings above are somewhat notional and can be highly dependent on the language being used and how the algorithm is coded. E.g., the dot( ) function is notoriously slow in MATLAB, so if I were to code the methods above I would use some form of matrix multiply instead. If u and v are column vectors, that last method could be coded as:

acos(u'*v/sqrt((u'*u)*(v'*v)))

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Jonas on 11 Mar 2024

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2092961-how-to-calculate-the-angle-between-two-3d-vectors#answer_1423661

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/2092961-how-to-calculate-the-angle-between-two-3d-vectors#answer_1423661

Open in MATLAB Online

look here:

https://de.mathworks.com/matlabcentral/answers/328240-calculate-the-3d-angle-between-two-vectors

P1=[78,47,88]-[128,97,138];

P2=[50,50,0]-[0,0,0];

a = atan2d(norm(cross(P1,P2)),dot(P1,P2))

a = 144.7356

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABProgramming

Find more on Programming in Help Center and File Exchange

Tags

  • vectors

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


How to calculate the angle between two 3D vectors? (5)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

How to calculate the angle between two 3D vectors? (2024)

References

Top Articles
Barbie Outfit - Niska cena na Allegro.pl
55+ Super Cute Barbie Outfits To Copy For The Barbiecore Aesthetic
Kreme Delite Menu
Live Basketball Scores Flashscore
Lifebridge Healthstream
How To Be A Reseller: Heather Hooks Is Hooked On Pickin’ - Seeking Connection: Life Is Like A Crossword Puzzle
Stolen Touches Neva Altaj Read Online Free
Call of Duty: NEXT Event Intel, How to Watch, and Tune In Rewards
Bbc 5Live Schedule
Aquatic Pets And Reptiles Photos
Mycarolinas Login
All Buttons In Blox Fruits
Samsung Galaxy S24 Ultra Negru dual-sim, 256 GB, 12 GB RAM - Telefon mobil la pret avantajos - Abonament - In rate | Digi Romania S.A.
State HOF Adds 25 More Players
Beebe Portal Athena
Troy Bilt Mower Carburetor Diagram
Georgia Vehicle Registration Fees Calculator
Byui Calendar Fall 2023
Sprinkler Lv2
Why Does Lawrence Jones Have Ptsd
Wgu Academy Phone Number
Best Transmission Service Margate
R&S Auto Lockridge Iowa
How to Make Ghee - How We Flourish
Play Tetris Mind Bender
F45 Training O'fallon Il Photos
48 Oz Equals How Many Quarts
California Online Traffic School
Nottingham Forest News Now
Joann Fabrics Lexington Sc
Weather Underground Durham
Past Weather by Zip Code - Data Table
Verizon TV and Internet Packages
Golden Tickets
The Vélodrome d'Hiver (Vél d'Hiv) Roundup
Studio 22 Nashville Review
Lyca Shop Near Me
The Closest Walmart From My Location
Gvod 6014
PruittHealth hiring Certified Nursing Assistant - Third Shift in Augusta, GA | LinkedIn
Final Fantasy 7 Remake Nexus
Doe Infohub
Smite Builds Season 9
Nami Op.gg
Sea Guini Dress Code
Amy Zais Obituary
Aloha Kitchen Florence Menu
Kate Spade Outlet Altoona
Tommy Gold Lpsg
Lorcin 380 10 Round Clip
What Are Routing Numbers And How Do You Find Them? | MoneyTransfers.com
Latest Posts
Article information

Author: Trent Wehner

Last Updated:

Views: 5417

Rating: 4.6 / 5 (56 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Trent Wehner

Birthday: 1993-03-14

Address: 872 Kevin Squares, New Codyville, AK 01785-0416

Phone: +18698800304764

Job: Senior Farming Developer

Hobby: Paintball, Calligraphy, Hunting, Flying disc, Lapidary, Rafting, Inline skating

Introduction: My name is Trent Wehner, I am a talented, brainy, zealous, light, funny, gleaming, attractive person who loves writing and wants to share my knowledge and understanding with you.