# Perpendicular 3D vector

This topic is 3638 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Is there any way to obtain a 3D unit vector that's perfectly perpendicular to another 3D unit vector without randomness and conditional statements? In other words, I only have one 3D unit vector. I have nothing else to go on. I need to find another arbitrary 3D unit vector that is perpendicular to this one. It seems that the standard is to test two hard-coded vectors (such as up=0,1,0 and right=1,0,0) to see which best fits the current vector to perform a cross product. I'm working in a shader where conditional branches will be overkill, so I can't afford to test this way. Are there any other methods to do this? Any help is appreciated.

##### Share on other sites
I don't know if there are better methods but I think it's a correct one.

Let u be your unit vector and i, j, k the canonical bases of R3. Using the Gram-Schmidt process you can orthogonalize the vectors {u, i, j} obtaining {u, e1, e2} where only one of the two vectors e1 or e2 can be zero. e1 and e2 are perpendicular and span a subspace of dimension 1 or 2 perpendicular to u. Every vector obtained by normalize(αe1 + βe2) with α != 0 and β != 0 is a non-zero vector perpendicular to u.

##### Share on other sites
That doesn't seem to work if u is aligned with (1,0,0). For instance, if u=(1,0,0), e1=(0,0,0) and computing e2 requires a division by 0.

I don't see how you can get away without a conditional at some point...

Edit: Actually, there is no continuous function that maps unit-vectors into vector that are perpendicular to the input (see the hairy-ball theorem). This means that you have to introduce a discontinuity somehow. If you can't use a conditional, you need some other means of making the mapping discontinuous.

##### Share on other sites
Quote:
 Original post by alvaroThat doesn't seem to work if u is aligned with (1,0,0). For instance, if u=(1,0,0), e1=(0,0,0) and computing e2 requires a division by 0.I don't see how you can get away without a conditional at some point...Edit: Actually, there is no continuous function that maps unit-vectors into vector that are perpendicular to the input (see the hairy-ball theorem). This means that you have to introduce a discontinuity somehow. If you can't use a conditional, you need some other means of making the mapping discontinuous.

[embarrass] You are right... Maybe using a texture?

##### Share on other sites
Yeah, you can compute two perpendicular vectors in such a way that they won't both be 0 at the same time, and then return a weighted average of them, where the weights are controlled by a tiny texture. A cubic texture with no interpolation would do.

##### Share on other sites
I have access to min(a,b) and max(a,b) in the shader. Those can sometimes be used in a clever way to compensate for no conditions. Would that be possible here? Some way to cross the existing vector with two others (like 0,1,0 and 1,0,0) and use min/max with the float results to secure something to work with?

##### Share on other sites
floor' or sign' are better candidates.

##### Share on other sites
this might help:

http://www.akiti.ca/RotateTrans.html

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 11
• 13
• 87
• 11
• 10
• ### Forum Statistics

• Total Topics
632973
• Total Posts
3009615
• ### Who's Online (See full list)

There are no registered users currently online

×