How To Get A Direction Vector Perpendicular to a Direction Vector ?

Started by
8 comments, last by Zakwayda 16 years, 11 months ago
Hello all, Im guessing this is a simple maths question, but I just cant seem to figure it out. I have a direction vector, for example 1.0f, 0.0f, 0.0f, and would like to calculate a direction vector which is perpendicular to this. If that makes sense. So for 1.0f, 0.0f, 0.0f it would become 0.0f, 1.0f, 0.0f. Although it does not matter if its +1.0f or -1.0f on the Y. Is there a simple and quick way to calculate this? Thanks in advance.
Advertisement
Look up "Vector Cross Product" and I think you will have your answer.
If you just need an arbitrary perpendicular vector, you can take the cross product of the input vector and the cardinal basis vector corresponding to the element of the input vector with the least magnitude. For example:
input = (-2,  4,  5) : cross with (1, 0, 0)input = ( 2, -5,  1) : cross with (0, 0, 1)
Provided the magnitude of the input vector is non-zero (or, in practice, greater than a small threshold value), this will always successfully generate a perpendicular vector.

The end results may not always be acceptable, however (for example, if used for billboarding this method can cause the billboard to 'snap' from one orientation to another occasionally).

If you need more info, perhaps you could clarify what you need this perpendicular vector for.
This is what I have so far,
PointA
PointB
Direction = PointA - PointB. ( Normalised )
Now I want to find the direction perpendicular to the direction above.

So the answer would be PointA CrossProduct Direction ?

My vector math is pretty weak so Im just trying to learn a little.

Thanks in advance.
The cross product of two vectors is:
cross.x = vec1.y*vec2.z - vec1.z*vec2.y
cross.y = vec1.z*vec2.x - vec1.x*vec2.z
cross.z = vec1.x*vec2.y - vec1.y*vec2.x

But i dont know how to do it if you only have 1 vector. I suppose you could create a second vector and have them fae each other ie -> <-
Quote:Now I want to find the direction perpendicular to the direction above.
In 3D there is an infinite number of vectors perpendicular to a given vector. You'll have to be more specific.
Quote:So the answer would be PointA CrossProduct Direction?
Strictly speaking, you wouldn't take the cross product of a point and a vector. You could consider PointA as a vector from the origin, but I'm guessing this isn't really what you want.

Again, I'd suggest describing to us what the 'end goal' is, i.e. what you need this perpendicular vector for. That will make it much easier for us to offer useful advice.
You have your direction vector, and a 'hint', that points roughly towards a specific direction (say the 'hint' vector defines the 'up' direction).

Then with a few cross products, you can get a orthogonal frame, with vectros perpendicular to each other.

Everything is better with Metal.

jyk pretty much said it all, but let me clarify. There are an infinite number of perpendicular vectors. If you don't care which one you get, then you can just compute the cross-product of the direction vector and any other vector. The easiest other vector to use is either the X, Y, or Z axis.

However, if you accidently use a vector that is close to (or is) the direction vector, you will run into problems. To avoid that, you pick the axis corresponding to the component of the direction vector that is closest to 0.

If you want a particular perpendicular vector (as opposed to a random perpendicular vector), then you need to choose a different method.

Refer to jyk's posts for the details.

John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
If you don't care which perpendicular vector you get, pick two coordinates which are not both zero and build a perpendicular vector by swapping them, changing the sign of one of them and setting the other coordinate to 0.<br><br>For instance, if your vector is (2,3,5), (-3,2,0) is a vector that is perpendicular.
Quote:Original post by alvaro
If you don't care which perpendicular vector you get, pick two coordinates which are not both zero and build a perpendicular vector by swapping them, changing the sign of one of them and setting the other coordinate to 0.<br><br>For instance, if your vector is (2,3,5), (-3,2,0) is a vector that is perpendicular.<!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–>Just as a point of interest, this is more or less equivalent to the 'cross with the least-aligned cardinal basis vector' method I mentioned earlier (and that JohnBolton elaborated &#111;n).

This topic is closed to new replies.

Advertisement