[Math] How to rotate / transform a vector so that it is parallel to a plane

plane-geometryvectors

My background:
I have taken an introductory class or two in linear algebra at university, but it was a few years ago and I have not practiced it since. My terminology might be off, but I believe my text should be clear enoungh for everyone to understand.

Problem:
I am trying to build a 3-dimensional controller for a game and have run into an issue that I cannot solve. I need to find a 3-dimensional direction vector to use for moving an object along a plane, parallel to the plane. The directional vector should be based on a 2-dimensional input vector along the x- and z-axis, in combination with the rotation (any / all of the three axes) of the plane.

I think the easiest way for me to explain the problem that needs to be solved is to try to illustrate it using two examples.

Assumptions:
Left-handed coordinate system. We have a sphere with a radius of 0.5 with its center located at (0, 0.5, 0). I guess we can consider the center of the sphere the only thing of importance, but maybe a sphere can help build a visual image. The input vector used to translate the sphere with in both examples below is (1, 0, 1)

Examples:

  1. There is a xz-plane located at y = 0, so the sphere is in contact with the surface of the plane. After moving the sphere along the input vector, the sphere will be located at (0, 0.5, 0) + (1, 0, 1) = (1, 0.5, 1)
  2. The plane from example 1 has been rotated 89 degress around the z-axis and moved ~0.5 on the x-axis (not using 90 degrees rotation and exact movement since it would make it a bit harder to illustrate why the sphere should be moved the way I want it to move). The sphere is still in contact with the surface of the plane, but from the side. If the sphere were to be moved using the input vector it would move through the plane, but I want the sphere to move parallel to the plane. I want to … rotate? the input vector somehow. The final position of the sphere should be roughly (0.05, 0.95, 1). The location of the sphere after the movement should be the same as if we were to rotate the final location of the sphere from example 1 around the world z-axis the same number of degrees as the plane is rotated around the z-axis in this example.

Available data:
The data available is the location of the sphere, the input direction vector and the normal of the plane. Is this information enough to be able to solve the problem, and if so, how? If not, what other data is required?

Best Answer

Okay - I get now that you talking about changing the ground. But still this princicple applies: the same transformation that you applied to the ground is the transformation that applies to the ball.

You rotated the plane. I don't know how you have accomplished that rotation in your program, but mathematically it is expressed as a matrix $M$ that satisfies $M^TM = I$, the identity matrix (where $M^T$ is the transpose of $M$). If $\bf n$ is the normal vector to the original plane, then $M\bf n$ is the normal vector to the rotated plane.

You also said you moved the plane (in the example, along the $x$-axis). This means that the new plane no longer passes through the origin (at least in your example, the original point did). This is a translation, but we need to be careful which translation we use. Suppose the new plane must pass through some point $\bf p$. In your example you indicated that it passes through $(\sim 0.5, 0, 0)$. You can use that to be the point $\bf p$. Since the normal vector is $M\bf n$ and it passed through $\bf p$, a point $\bf r$ is on the new plane if $$(M{\bf n})^T{\bf r} = {\bf n}^TM^T{\bf r} = d$$ where $d = {\bf n}^TM^T\bf p$. The translation vector we need is ${\bf b} = dM\bf n$, which is the closest point on the new plane to the origin.

So the tranformation to the original plane ${\bf n}^T{\bf r} = 0$ to get the new plane ${\bf n}^TM^T{\bf r} = d$ is to rotate by $M$, then add the translation vector $b$:

$$ {\bf r} \mapsto M{\bf r} + \bf b$$

(In case you don't know, $\mapsto$ means "maps to". I.e., the original value on the left is transformed into the value on the right.) This is the same transformation you need to apply to your sphere to get it's new location after the transformation. If $\bf c$ is the original center of the sphere, the transformed center will be $${\bf c} \mapsto M{\bf c} + \bf b$$

Let $\bf v$ be the "input direction vector" of the horizontal ball movement. Since $\bf v$ is a direction, not a point, it does not translate. So the new direction vector that the ball will travel up the sloped ground will be given by $${\bf v} \mapsto M\bf v$$

So the inputs you need to solve your problem are the rotation vector $M$, and a point $\bf p$ that the new plane passes through. If you have these, then you can use them to transform the ground plane, the sphere's position, and the direction of movement, all three.