[Math] How to shift an object to rest on the xy-plane

3dvectors

For example, I have a cube, which exists somewhere in 3-space, that is composed of edge vectors and vertex points. How can I shift these values so that a face of the cube rests on the xy-plane?

I don't want all the vectors to simply be projected onto the xy-plane, since they should retain their magnitude. The cube should appear exactly the same, other than a change in orientation and position.

How can I define a transformation matrix or value to signify how to rotate the top-left cube so that the blue-outlined face is coplanar with the xy-plane (like the bottom-right cube)?

Cube Shift

Best Answer

Yes. You can do this with a translation and a rotation.

Let's start by saying that one of the corners of the blue square that you want to move to the plane is called $A$: then you take all the corners of your cube and subtract $A$ from them, so that $A$ becomes $(0,0,0)$, and the other corners become...some other points. This is "translation" and doesn't change the shape of the cube at all.

Now let's suppose that the two corners of your blue square adjacent to $A$ are called $B$ and $C$, and the third corner adjacent to $A$ (the not-blue one) is called $D$. Compute

$$ u = B - A\\ v = C - A \\ w = D - A $$

each of these is to be computed term-by-term, so if $B = (4, 6, 3)$ and $A = (2, 0, 1)$, then $u = (2, 6, 2)$, ok? If you use post-translation vertex coordinates, then $A = (0,0,0)$, so $u = B - A$ will contain just the (post-translation) coordinates of $B$. It doesn't matter which you use -- you'll get the same value for $u$.

Now, you're going to alter $u, v, w$ slightly:

  • compute $L = \sqrt{u_x^2 + u_y^2 + u_z^2}$
  • replace $u = (u_x, u_y, u_z)$ with $u = (\frac{u_x}{L}, \frac{u_y}{L}, \frac{u_z}{L})$, i.e., divide each coordinate by $L$.

Do a similar operation to $v$ and $w$.

Now you're going to do the following transformation to each $(x,y,z)$ point of the post-translation cube (i.e., each vertex, or each point on any face, whatever). \begin{align} (x, y, z) &\mapsto (x', y', z'), \text{where}\\ x' &= u_x x + u_y y + u_z z\\ y' &= v_x x + v_y y + v_z z\\ z' &= w_x x + w_y y + w_z z. \end{align}

That will send the edge $AB$ to the positive x-axis, the edge $AC$ to the positive $y$ axis, and the edge $AD$ to the positive z-axis.

If you actually know about matrices and linear algebra, this is all a good deal easier: the transformation (using homogeneous coordinates like $(a_x, a_y, a_z, 1)$ for the point $A$), looks like

$$ X \mapsto \mathbf M X $$ where $\mathbf M$ is a matrix whose upper right $3 \times 1$ submatrix contains $-a_x, -a_y, -a_z$. The lower right entry is 1; the bottom left $1 \times 1$ submatrix is all zeroes, i.e., it looks like $$ \mathbf M = \begin{bmatrix} & & & -a_x \\ & * & & -a_y \\ & & & -a_z \\ 0& 0 & 0 & 1 \\ \end{bmatrix}. $$ The upper left $3 \times 3$ block has rows $u', v', w'$, where \begin{align} u' = \frac{B - A}{\| B - A \|} \\ v' = \frac{C - A}{\| C - A \|} \\ w' = \frac{D - A}{\| D - A \|}. \end{align}

And that's it. In this form, the letters $A$, $B$, $C$, and $D$ denot the coordinates of the specified vertices before any operations have taken place.

Addition in response to comments

This works because the vectors $u,v,w$ are pairwise perpendicular, so that inverting a particular matrix amounts to just transposing it.

In general, if you have a basepoint ($A$) and a point $B$ that you want on the $x$-axis, and a point $C$ that you want in the +y halfplane, you can compute

$$ u = B - A\\ v = C - A $$ Then you let $$ \hat{v} = v - \frac{ (u \cdot v) u}{u \cdot u} $$ to make $v$ and $u$ perpendicular, and then let $$ u' = u / \| u \|\\ v' = \hat{v} / \| \hat{v} \| w' = u' \times v' $$ Now once again, these are pairwise orthogonal unit vectors, and the matrix described above will do what you need.