Quaternion from two vectors: the final version

I have published a first article about how to build a quaternion from two arbitrary direction vectors that will transform one of the directions into the other. That article was deliberately omitting the special case when the two vectors were facing away from each other, which required special treatment. So I wrote a second article explaining how to quickly pick an orthogonal vector.

Combining the results from the two articles should be straightforward. Here is the resulting, final function:

/* Build a unit quaternion representing the rotation
 * from u to v. The input vectors need not be normalised. */
quat quat::fromtwovectors(vec3 u, vec3 v)
    float norm_u_norm_v = sqrt(dot(u, u) * dot(v, v));
    float real_part = norm_u_norm_v + dot(u, v);
    vec3 w;

    if (real_part < 1.e-6f * norm_u_norm_v)
        /* If u and v are exactly opposite, rotate 180 degrees
         * around an arbitrary orthogonal axis. Axis normalisation
         * can happen later, when we normalise the quaternion. */
        real_part = 0.0f;
        w = abs(u.x) > abs(u.z) ? vec3(-u.y, u.x, 0.f)
                                : vec3(0.f, -u.z, u.y);
        /* Otherwise, build quaternion the standard way. */
        w = cross(u, v);

    return normalize(quat(real_part, w.x, w.y, w.z));

If you know you are exclusively dealing with unit vectors, you can replace all occurrences of norm_u_norm_v with the value 1.0f in order to avoid a useless square root.


1. minorlogic@yahoo.com -- 2014-05-09 13:01

The complete version from nonunit vectors looks like

quat quat::fromtwovectors(vec3 u, vec3 v) {

vec3 w = cross(u, v); quat q = quat(dot(u, v), w.x, w.y, w.z); q.w += q.magnitude(); return normalize(q);


  1. Faster. 4 muls for quaternion magnitude instead of 6 muls for vectors magnitude.
  2. Numerical more stable (produce less roundoff error) than use of vectors length.

Derivation of original method can be found http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/minorlogic.htm

2. -tHE SWINe- -- 2014-06-16 16:50

For the simple case and normalized vectors, this can be carried out with just a single dot product, a single cross product and a single square root: just taking a half vector, normalizing it, taking a dot product for cosine of the half-angle and taking a cross product to produce the complex part of the quaternion. That seems to be slightly faster than the proposed version.

I have compared to the proposed implementation (which is in fact used in the glm library), glm seems to be less numerically stable for nearly opposite vectors rotate (1, 2, 3) to (-1, -2, -3.01).

3. anonymous -- 2014-09-01 17:12

Since you blog about these topics, you might be interested to read about Grassman algebra, aka Geometric Algebra.

The math is really nice IMHO, and a lot of 'magic' of complex numbers and quaternions is explained by it.

For example, any rotation in any dimension is actually a double reflection about two vectors where the angle between the vectors is half the rotation angle. The math for a double reflection (hence rotation) in that algebra is N*M*A*M*N, See e.g. http://en.wikipedia.org/wiki/Rotor_(mathematics)

Apparently the guys from the C4 Engine gave a talk about this at GDC! http://gdcvault.com/play/1015322/Fundamentals-of-Grassmann

4. PaulSteven -- 2017-10-13 13:48

There are entirely numerous revolutions that will pivot the primary vector to be toward the second vector, yet this strategy finds the turn that is in some sense the most limited, and likely the one which you are considering. To get the more extended pivots you can increase this quaternion by one that uses the first vector as the heading of the hub, Essay Writing Company with a subjective point of turn.

5. anonymous -- 2017-11-08 07:38

Hello, I am trying to learn what a quaternion is, not just for computer science, I like actually want to know exactly what it is and how to do http://www.click2assignment.co.uk/ calculations with them. I heard they're pretty easy, but I don't understand them. Like adding matrices, adding scalars and vectors and , to get quaternions or however it works.Thanks!!

6. anonymous -- 2017-11-08 10:35

Hello friends, If you love to play stickman games online then visit our website http://stickmangames.xyz and play stcikman games with it's full resources and no registration or download required.

7. Alina Frandus -- 2017-11-09 10:54

Actually, you can define division for 4-dim vectors, the so-called "quaternions". Think of a vector in R4 as an element of the form a+bi + cj + dk, where 1,i, j, k are linearly independent. Define i*i = j*j = k*k = -1, i*j = -j*i, and i*j = k. EZ Assignment Service UK tell about These rules allow us to multiply all vectors in R4, and every non-zero vector has a multiplicative inverse.

8. karry -- 2017-11-22 07:48

Hey dear it will be the golden chance for you to get the roblox cheats and you get all the resources of roblox here.

9. Owen Potts -- 2017-12-21 06:45

This is stunning, already when there was no permit necessary for the work many individuals were over-burden in the place, it's a decent advance. In the event that anybody needs additional data about this news <a href="https://americasuits.com/bomber-womens-fur-black-leather-hooded-jacket"> Bomber Womens Fur Hooded Black Leather Jacket</a>.

10. Chris Greenwalt -- 2017-12-30 15:55

All of the two vectors are necessary whenever students are required dissertation help from most qualified writers. However learning these, people may have good grip on math subject.

11. Alina Martin -- 2018-01-04 07:08

as a sum of 4 squares, i.e. in the form c12 + c22 + c32 + c42. The only issue with understanding is exactly what the numbers are. Are we talking about quaternion numbers (i.e. sums of squares of quaternions)? Promo Codes pretty sure this is not the case, because a) I don't know if it's true, and b) the method they're pointing to works for real numbers. Are we talking about real numbers? If so, then the answer is pretty easy. The numbers that can be written as the sum of 4 squares are exactly all number greater than or equal to 0, and if you multiply them, you get a number greater than or equal to 0.

12. anonymous -- 2018-01-15 09:45

Such tutorials are very rare to be found. You have work hard in collecting data to write this post which i like it a lot. Keep up your good work.

13. Micheal -- 2018-01-15 10:15

Such tutorials are very rare to be found. You have work hard in collecting data to write this post which i like it a lot. Keep up your good work. Buy playground tiles from http://playgroundtilesuk.uk/playground-tiles.html

14. anonymous -- 2018-01-16 06:24

This is really a great stuff for sharing. Thanks for sharing. https://www.finance-assignments.com/

15. anonymous -- 2018-01-16 08:42

I have read your first article and your second article as well. There are very good informative knowledge shared in this post. Thanks for it. To get london brick Click here http://londonbricksuk.co.uk/

16. statistics homework help online -- 2018-01-16 12:49

Well thanks for posting such an outstanding idea. I like this blog & I like the topic and thinking of making it right. https://statskey.com/

17. Kylie Austin -- 2018-01-25 11:17

Deal Hack provides you with the collection of coupons, promo codes, and clearance sales from the Internet arena. You can now save up to 80% on different products and brands, http://dealhack.net is the right choice to make.

18. anonymous -- 2018-01-26 14:13

Thank you very much for sharing resulting, final function of the very good points. I would like to visit again this post. Carry on sharing it. To get Get information about rubber matting product Click here http://ukrubberflooring.co.uk/

19. anonymous -- 2018-01-27 12:14

Good information for me that For the simple case and normalized vectors, this can be carried out with just a single dot product, a single cross product and a single square root.Thanks for sharing it. To get strongest motorcycle jeans Click here http://resurgencegear.com/

20. alicianewman148@gmail.com -- 2018-02-14 00:51

I bookmark this blog because very impressive points I’ve learned from this blog and the post is written in very a good manner, and very informative and helpful blog I've ever found while searching UK dissertation help on web, and i am very pleased with the overall work, everything is well researched and maintained. I’ve been learned lot more good things from here, will surely recommend this blog to my friends, also thanks for this helpful post!

21. Android Application Development Service -- 2018-02-14 08:51

I Personally Like Your Post; You Have Shared Good Insights And Experiences. Keep It Up. https://www.theappgods.com/

22. Case Studies Analysis -- 2018-02-14 08:53

Hi Buddy, Your Blog' S Design Is Simple And Clean And I Like It. Your Blog Posts About Online Writing Help Are Superb. Please Keep Them Coming. Greets! https://www.thecasesolutions.com/

23. Economics Project Help -- 2018-02-24 13:10

Get the dissertation writing service students look for these days with the prime focus being creating a well researched and lively content on any topic. https://economicskey.com/

24. netflixlogin470@gmail.com -- 2018-02-27 10:34

Though the only thing which might work for you is a legitimate & active Netflix account login and passwords. Moreover,https://netflixloginhelp.com/netflix-login-sign-in-account-netflix-com-sign-up/ this would even keep you away from harmful and malicious programs which you might encounter on your hunt

25. -- 2018-03-02 15:47

Thanks to your explanations, even such a beginner like me figured out this article. Thank you, I really like your blog and the quality of the material that you post http://19216811-admin.com/

26. EdwardPeter0010@gmail.com -- 2018-03-28 13:55

I have perused your first article and your second article also. There are great educational learning partook in this post. A debt of gratitude is in order for it. To get london block Click here. Buy Dissertation UK

27. anonymous -- 2018-04-03 16:52

Nice article to read.. looking for more..

take my online class Online Class Help

28. Nelson Lott -- 2018-04-06 08:49

Thanks for sharing this blog its very informative and useful for us http://www.google.com

29. Olivia malik -- 2018-04-16 13:38

I swear that I did nothing but my blog appears to lose the announcement about newly added blog comment for my entries. I myself can not see my own entries either. It appears "Start a Blog" although if I click in I can see my posted entries. In my Home Page, it only http://promoforsales.com/ has Messages, and Page Views (the Blog Comment and Visitor Views are blown up !!). Moreover, despite my choice of publicizing my entries, but others are impaired to see. WHAT HAS HAPPENED TO MY BLOG? IS THERE ANYONE WHO CAN HELP ME?

30. anonymous -- 2018-04-24 09:21

i really struggled with this..i was not able to understand how to build a quaternion from two arbitrary direction vectors that will transform one of the directions into the other. this blog really helped me finding the solution..thanks fr sharing this.. https://www.certvalue.com/iso-9001-certification-in-saudi-arabia/ https://www.certvalue.com/iso-22000-certification-dubai/

31. owengarner21@gmail.com -- 2018-04-28 07:45

When you are scratching your head in the middle of the night while hopelessly attempting to write your essays, just remember that help can be obtained by simply calling Best Essays Writing and telling them Top Essay Writing Service. So contact us now and get all of your essays completed by professionals.

32. anonymousOwen Potts -- 2018-04-28 09:02

All through the auto, arrange exists together with the capacity to help a more conceivable future. For instance, twofold as warmth sinks, keeping down the temperature of the auto's batteries and drivetrain. Black Leather Jacket

33. Dubai Electrical Plumber -- 2018-04-30 09:37

Such an instructive common contention for me I have taken an extreme number of central focuses and data in this discourse. Which will genuinely help me in my future. Handyman Dubai

34. anonymous -- 2018-04-30 12:02

Hello Thanks a lot for sharing such an amazing post. It was helpful and informative. For essay and assignment writing go to <a href="https://www.customessayswriter.co.uk/">Cheap Essay Writing Service UK</a>

35. Stella Ban -- 2018-05-07 09:14

As an example, twofold as warmth sinks, preserving down the temperature of the auto's batteries and drivetrain. There are super educational getting to know partook on this put up.Essay A debt of gratitude is in order for it. thank you, I really like your weblog and the satisfactory of the cloth which you post.

36. Kelp -- 2018-05-10 12:02

It is extremely nice to see the greatest details presented in an easy and understanding manner. Kelp

37. Curry Powder -- 2018-05-10 17:07

I see some amazingly important and kept up to length of your strength searching for in your on the site Curry Powder

38. Calcium -- 2018-05-10 20:10

I have read your article; it is very informative and helpful for me. I admire the valuable information you offer in your articles. Thanks for posting it. Calcium

39. Jackfruit -- 2018-05-10 20:45

I must say, I thought this was a pretty interesting read when it comes to this topic. Liked the material. . . . . Jackfruit

40. Coconut Oil -- 2018-05-11 12:12

I don’t think many of websites provide this type of information. Coconut Oil

41. Turnips -- 2018-05-11 16:51

Thank you for your post, I look for such article along time, today i find it finally. this post give me lots of advise it is very useful for me. Turnips

42. anonymous -- 2018-05-18 13:36

The impressive information you can share about Quaternion from two vectors: the final version. Keep it up you are doing a good job https://www.webhostingonedollar.com/

43. CarolineWebb -- 2018-05-21 09:16

Very friendly you explaining here on how to quickly pick an orthogonal vector which can be easily understand by everyone. Thanks for sharing the complete version from nonunit vectors.

Best Regards, http://www.personalstatementfolks.co.uk/psychology-personal-statement/

44. anonymous -- 2018-05-22 07:45

That's really important to know how you can watch free football game of Russia football world cup without TV cable. If you are in office or travel you should not miss those game right? So Let's see how you can watch fifa world cup live streaming for free in hd. Click this website https://wc2018live.org/fifa-world-cup-2018-live-stream/

45. adama.storey@gmail.com -- 2018-05-22 10:08

By Visiting This Site I Found Cool Stuff Here Keep It Up. https://accountingassignment.xyz/

46. GeneLReyes@rhyta.com -- 2018-05-22 14:23
47. Do My C++ Homework -- 2018-05-23 11:36

Pretty Helpful Material, Much Thanks For This Article. https://www.chelponline.com/c-plus-plus-assignment-help-13067

48. anonymous -- 2018-05-24 07:18
49. JAVA ASSIGNMENT HELP -- 2018-05-30 08:14

We know that your tight schedules may keep you away from the pending assignments. But you must take the front seat while hiring an assignment help service provider. Customize your assignments to get the best results. Let them do the toil for you but do not forget to control their minds. Be open about how you want the assignment to look, the objective of it and the outcome you are aiming. This will help them produce on point assignments in a short span of time.


50. flightsonline@tutanota.com -- 2018-06-11 13:34

While perusing airfares, look for the one in green. That's the flight Google has identified as the single best deal. It's a small thing, but also a handy time-saver. Note, however, that this flight won't always be contained in the "best flights" box, which represents the best combination of price and flight time. Book google flights on https://www.google-flights.online

51. aisha oberoi -- 2018-06-12 10:55

Commendable job with the article! It was really informative and enriching. Looking forward to more such posts in the future. Keep us updated with what’s in store! Will surely keep frequenting this website. <a href=https://www.makemyassignments.com/au/marketing-assignment-help-australia/ > marketing assignment help Australia</a>

52. anonymous -- 2018-06-13 10:14

Wow amazing post, I am interested to write on https://www.abbraci.com/

53. anonymous -- 2018-06-13 10:15

That's great, This is the really great post, I like it https://www.usajacket.com/blog/

54. Janice Lewis -- 2018-06-14 13:29

Games are our best https://www.apkcoach.win/ companion for leisure hours ,in fact, some games are so addictive that we find spare hours out of our busy schedule just to play them

55. wegi@o3enzyme.com -- 2018-06-18 07:38

KFC is the second-largest restaurant chain all over the world. You can install the KFC App on your phone.


56. Marshal John -- 2018-06-20 13:07

We Also Share Some Information About Our Business Matlab Expert

57. Philip Kayden -- 2018-06-20 14:13

This Is Really Great Work. Thank You For Sharing Such A Good And Useful Information Here In The Blog For Students. SPSS Assignment Help

58. jasonmorgan8750 -- 2018-06-21 12:32

Very friendly you explaining here on the way to quickly choose AN orthogonal vector which may be simply perceive by everybody. I see some surprisingly necessary and maintained to length of your strength checking out in your on the location. Bespoke Leather Jacket

59. davidfre -- 2018-06-22 07:05

Hiring genuinely cheap assignment writing service only makes when the academic specialist also delivers the content as per the client’s expectations. However, easier said than done, but 100% possible when clients decide to hire our writing specialists at PA. We apply a unique strategic approach for each of your project without compromising the quality of the content and your limited budget allowed.

60. addisonadney789@gmail.com -- 2018-06-22 08:56

Science Channel’s Are Giving A Complete Knowledge To Its Viewers About Every Thing Students Write Done Dissertation On This Subjects And Show Its Importance. <a href="https://civilengineeringaid.xyz/">Online Civil Engineering Homework Help</a>

61. operationsmanagement321@gmail.com -- 2018-06-22 12:12

Ivey Case Study Analysis <a href="https://casestudyaids.xyz">Ivey Case Study Analysis</a>

Add New Comment