Quaternion from Vector v1 to Vector v2

A short reminder to compute a useful Quaternion!

While working on the WearHap project in 2015-2016, I prepared a demo to showcase the features of the HEXOTRAC hand exoskeleton designed and developed in our laboratory. The demo idea was that of showing the tracking and force feedback capabilities of the device.

I chose to create a VR environment where it was possible to manipulate objects by directly using the fingertips, with the possibility to freely grasp them and rotate them. The logic and math behind the grasping solution are strongly based on this great article, but required some adaptation:
Multi-Contact Grasp Interaction for Virtual Environments.

One of the geometry calculi I needed the most to achieve something similar to what’s described in the article is the computation of the Quaternion which rotates vector V1 into vector V2. This is a calculus I need from time to time, for virtual objects manipulation or other things. Here’s the pseudo code to do that! It has some limitations, which are listed below.

Quaternion q; vector a = crossproduct(v1, v2);
q.xyz = a; 
q.w = sqrt((v1.Length ^ 2) * (v2.Length ^ 2)) + dotproduct(v1, v2); q.normalize();

For a proper implementation, see the one of the Ogre engine: https://bitbucket.org/sinbad/ogre/src/9db75e3ba05c/OgreMain/include/OgreVector3.h?fileviewer=file-view-default#cl-651

It might be interesting to investigate the following article as well, which definitely goes more in depth about the topic: http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors

This video shows the grasping solution at work in Unity. The white spheres represent fingertips of index, middle and thumb fingers, as tracked with the HEXOTRAC hand exoskeleton. Force feedback is provided while manipulating the virtual objects:

dario mazzanti, 2021

Up ↑