Jump to content
  • Advertisement
Sign in to follow this  
Kest

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by alvaro
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.

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!