I have a 2D square ABCD in a 3D space, with side length 2s, that is represented by four vectors (one for each vertex) and a fifth vector v for the center point.

The square lies standing upright on the plane z = -1 (xy plane).

v  = < 0, 0, -1>
OA = <-s, s, -1>
OB = < s, s, -1>
OC = <-s,-s, -1>
OD = < s,-s, -1>

Now, consider point P anywhere in the 3D space. I want to rotate the square around the origin, such that v aligns with OP. The result that I want is mainly the resulting rotated OA, OB, OC and OD vectors.

Screenshot of scenario.

The point P is arbitrary and can be any point in the 3D space. Any help is greatly appreciated!

  • 1
    $\begingroup$ Currently your problem is not 100% defined, because you could roll around the vector OP and still have to rotated square such that v aligns with OP. $\endgroup$ – fibonatic May 4 '15 at 21:02
  • $\begingroup$ Roll is actually not a problem in my particular scenario. Only the pan and tilt. $\endgroup$ – Mecha May 4 '15 at 22:45

You should take a look at the Wikipedia page on rotation matrices, specifically the section on forming a rotation matrix from an axis and an angle. In your case, you have a vector $\vec{v}$ that you would like to align with another vector, $\vec{OP}$. The axis of rotation should be the unit vector $\hat{u}$ normal to these two vectors given by $$\vec{n}=\vec{v}\times\vec{OP}, ~~\hat{u}=\dfrac{\vec{n}}{||\vec{n}||}. $$ The order of the cross product is important.

The angle between the two vectors is given by $$\theta = \operatorname{acos}\left(\dfrac{\vec{v}\cdot\vec{OP}}{\left|\left|\vec{v}\right|\right|~\left|\left|\vec{OP}\right|\right|}\right) $$ where $\theta\in[0,\pi)$.

Once you have an angle and an axis of rotation you can form the rotation matrix $R$. This is given as
rotation matrix
on the Wikipedia page. To rotate your square (and any other vectors or points of interest) so that $\vec{v}$ is aligned with $\vec{OP}$ simply multiply all the coordinates $\vec{OA}$, $\vec{OB}$, $\vec{OC}$, and $\vec{OD}$ by the rotation matrix $R$. As mentioned in a comment, you could then also rotated the resulting points by any angle you wish about the axis $\vec{v}_{\text{new}}=\vec{OP}$ while still maintaining the same orientation for $\vec{v}_{\text{new}}$.

I believe the only cases where this will fail are if $\vec{OP}=\pm\vec{v}$, in which case the cross product is zero. If $\vec{OP}=\vec{v}$ then there is nothing to do. If $\vec{OP}=-\vec{v}$ then $\theta = \pi$ you can choose $\vec{n} = (a,b,0)$ for any $a$ and $b$ you wish.

  • $\begingroup$ This is exactly what I needed and it worked. Thanks a bunch :) $\endgroup$ – Mecha May 6 '15 at 16:03

The transformation that you are looking for is for a rotation about a fixed axis through a given angle. This transformation can be described using using matrices and vectors. A reference for the formula for the matrix for rotation about an axis is given in the Wikipedia.

The fixed axis is perpendicular to the vector from the origin to P and perpendicular to the vector from the origin to V. Since you want to rotate from V up to P, the fixed vector is in the direction of the cross product $\vec{V} \times \vec{P}$. Since $\vec{V}$ points straight down, the fixed vector will be in the xy-plane. In other words, the variable $u_z$ in the Wikipedia formula will be zero. Note that the formula in the Wikipedia requires a unit vector, so you will have to divide the cross product by the distance from the origin to P to obtain $u_x$ and $u_y$.

The angle of the rotation can be found from the dot product $\vec{V} \cdot \vec{P}$. The cosine on the angle will be the dot product divided by the distance from the origin to P. The sine of the angle can be calculated from the cosine. The cosine of the angle may be positive (P below the xy-plane) or negative (P above the xy-plane), but the sine of the rotation angle will always be positive. That should give you all nine of the terms in the matrix.

Once the matrix elements are calculated, multiply the coordinates of each point on the square by the matrix to get the new coordinates.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.