tags:

views:

755

answers:

3

I am new to dealing with 3D, and even simple stuff makes my head spin around. Sorry for the newbie question.

Lets say I have 2 vectors:

a(2,5,1)
b(1,-1,3)

These vectors "generate" a plane. How can I get a third vector perpendicular to both a and b?

I can do this in 2D using a vector c(A,B) and turning it into c'(-B,A).

Thanks for the help.

+11  A: 

Use the cross product.

That is, a vector perpendicular to a and b is given by ( a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x).

moonshadow
+6  A: 

You take the cross multiplication of these two vectors to get a third perpendicular vector to the plane they generate:

P = A * B

Which is:

<xp, yp, zp> = |i   j   k |
               |xa  ya  za| // The determinant
               |xb  yb  zb|

All what you have to do is to solve this determinant or just look it up in Wikipedia :)

AraK
I like this answer better because it explains where the answer comes from. Not just a random formula to remember.
Robert Massaioli
Thanks, I would accept this too : )
nonammeN
A: 

For what it's worth, here's a cross product function from Quake 3, with vec3_t defined as an array of three floats for x, y, and z:

void CrossProduct( const vec3_t v1, const vec3_t v2, vec3_t cross ) {
    cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
    cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
    cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
}
Preston