# Vector relative to another vector

I have a direction vector (x/y/z) that I need to express relative to the normal vector of a surface, such that the direction vector is "local" to the normal vector.

I've read that I need to create a basis using the normal vector (up/right/forward) and use the dot product to find each new vector element.

1.) Is this the most efficient way to calculate the localized direction vector?

2.) How do I then transform the direction vector back into its original basis (after it is altered in local space)?

• The approach you describes seems like generally the right one, but it would be easier to give a good answer if you gave some more specifics about your application (and perhaps establish some notation). Aug 19, 2016 at 20:34

Is this the most efficient way to calculate the localized direction vector?

It depends on how you define "efficient". In general, this is the simplest way, and definitely sufficiently fast in a computer program, even in a game. Whether it is the fastest, depends on the hardware, memory structures available, and so on. This is definitely the way I'd generally do it, for sure.

If you are asking about the nitty gritty details, I'd say you could make the surface normal vector unit vector (by always scaling it to unit length, i.e. 1), and also store the two other basis vector with each surface. That is, rather than calculate them again and again. I'd definitely do this myself.

Let's say you have $\hat{n}$ as the normal vector, perpendicular to the surface, with $\hat{u}$ and $\hat{v}$ parallel to the surface; with all three unit vectors (length $1$), and all three vectors perpendicular to each other. Furthermore, the point on the surface is $\vec{p}$, and you wish to find the vector $\vec{r}$ from point $\vec{p}$ to $\vec{q}$, but relative to the point $\vec{p}$ on the surface. This is simple: $$\vec{r} = ( x, y, z ) = \left ( \hat{u} \cdot \left ( \vec{q} - \vec{p} \right ), \; \hat{v} \cdot \left ( \vec{q} - \vec{p} \right ), \; \hat{n} \cdot \left ( \vec{q} - \vec{p} \right ) \right )$$

Here, $x$ and $y$ are along the surface, with $z$ increasing above the surface if your normal points outwards; if your normals point inwards, then $z$ decreases as you go further above the surface. $z = 0$ is on the surface in any case.

How do I then transform the direction vector back into its original basis (after it is altered in local space)?

Converting $\vec{r}$ back to the global coordinate system is just as easy, $$\vec{q} = \vec{p} + x \hat{u} + y \hat{v} + z \hat{n}$$

As to computational efficiency, each conversion requires nine multiplications and nine additions or subtractions, if I calculated right. I would consider that quite efficient; I don't really know how you could reduce that in any way that would matter in practice.