In a 3D world, given a box B, a pivot P, and a direction vector V, how can I find out how much to rotate at P such that V points towards an arbitrary point A?
Problem source:
I am a software developer that come across the need to rotate an object in the above manner, where a 3d model need to be rotated in this way for the user to interact with.
Current Attempts:
I tried using an offset between the direction vector and the pivot, and calculate the rotation required between the offseted target and the pivot.
However all my current attempts is done in code, and I left the mathematical calculation to the libraries due to my limited knowledge - which means to be honest I am not very clear what they actually do.
Note:
Bcan be of any arbitrary size,Pcan be anywhere within the boxVcan be anywhere within the boxAcan be anywhere in the world
There is probably a better way of accomplishing this, but this is one method of calculating the quaternion of rotation:
We represent everything as a quaternion. That means instead of 3 coordinates, it will have 4, the first of which is $0$. In handling quaternions, addition and multiplication are defined by $$(a,b,c,d) + (w, x, y, z) = (a + w, b + x, c+y, d + z)$$ $$\begin{align}(a,b,c,d)(w, x, y, z) =\, (&aw - bx - cy - dz, \\&ax + bw + cz - dy, \\&ay - bz + cw + dx, \\&az + by - cx + dw)\end{align}$$Yes, the multiplication formula is a bit of a mess, but you only have to program it once. A real number $t$ is identified with the "scalar" quaternion $(t, 0, 0, 0)$ and a vector $V = (v_1, v_2, v_3)$ is identified with the "vector" quaternion $(0,v_1, v_2, v_3)$. Thus we can say that $$q = (t , v_1, v_2, v_3) = t + v$$where $t$ is the "scalar part of $q$", and $v$ is the "vector part $q$". Quaternions have conjugates defined by negating the vector part: $\bar q = t - v$. You can show that the product $q\bar q$ always has non-negative scalar part, and zero vector part, so we consider it to be just a real number. The absolute value, or norm, of a quaternion is $|q| = \sqrt{q\bar q} = \sqrt{t^2 + v_1^2 + v_2^2 + v_3^2}$. It follows that the inverse of a quaternion $q$ is $$\frac 1q = \frac {\bar q}{q\bar q} = \frac {\bar q}{|q|^2}$$
To rotate an arbitrary vector $S$, the result is just $$S' = qSq^{-1}$$ To rotate a point $B$, the process is $$B' = P + q(B - P)q^{-1}$$