#### Archived

This topic is now archived and is closed to further replies.

# Two quads - which direction they're facing

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

## Recommended Posts

hello, I am trying to determine if two planes (trees : quads) are facing in the same direction or not. I have normal vector for each but I am not sure how to detemine if they both facing same direction or the opposite dir. How do I go about it? Can I tell just from the Normal vector xyz values or I need more data like planes points etc.. [edited by - papa on April 14, 2004 9:09:38 AM]

##### Share on other sites
Take the dot product of both normals, if that equals zero i believe they are facing the same direction, 1 if they are opposite, anything in between indicates, anything in between.

edit: Sorry I think you have to take the cosine of the dot product, and maybe flip the 1 and 0 statements. Who knows.

[edited by - honayboyz on April 14, 2004 9:10:54 AM]

##### Share on other sites
The dot product of 2 vectors a,b, with and angle between them of x is
|a|*|b|*cos x
The they are facing exactly the same direction the angle will be 0, if they are completly opposite it will be 180deg or PIrad. The cosine of 0 is 1, and of 180 is -1.
If they are adjacent x will be 90, which has a cosine of 0.

If the dot product of the normal vectors (assuming they are unit length) is 1 ( 1 * 1 * 1) they are facing the same direction, if it is -1 (1 * 1 *-1) they are facing opposite direction. If it is 0 they are exactly adjacent.
If it is greater than 0 but less than 1 they are facing roughly the same direction, but the planes are not parrelel, if it is less than 0 but greater than -1 they are facing opposite directions but are not parrelel.

##### Share on other sites
hm... a bit confusing for me. All the information I have at the moment is two normals from plane 1 & 2. Flax Do I need to normalize normals first, or it is possible to leave them as they are.
Also do I need to calculate angles between normals too?.
I didnt get that....

When you write |a| do you mean vector length as in some books thats how they indicate vector length.

[edited by - papa on April 14, 2004 9:46:19 AM]

##### Share on other sites
Yes, by |a| I ment the length of a. You don''t need to worry about the angle or the cosine etc to work out the dot product though, I was just trying to explain my thinking.

To calculate the dot product when you have the components of the vector you can do
DotProduct = Ax*Bx + Ay*By + Az*Bz
where Ax is the x value of vector A, etc.

If the normal vectors are already unit length you do not have to worry about them, as they have no effect (because |a| and |b| are both 1, you just have the cosine of the angle between them).

If your normal vector is not unit length already I expect it is faster to divide the dot product by |a|*|b| than to normalise both vectors, so you have:
(|a|*|b|*cos x)/(|a|*|b|) = cos x

This then acts in the way I described in my previous post.

##### Share on other sites
ok. thank you Flax. I understand how dot product works (its a projection of 1 vector onto the other from which we get the scalar length) but I dont know how to test if two planes are facing same direction.
The theory you mentioned sounds ok. But I still dont know if I need to calculate angles or not. I dont know what to test for. Perhaps I dont understand your equation written this way, I''ll try to implement it as its always easier to understand for me using C++ syntax. Please correct me if I am wrong.

So I have 2 quads: q1 & q2 and get normals from both quads:

// normals are not normalized
Vector3D N1 = q1.GetNormal();
Vector3D N2 = q2.GetNormal();

float d = Dot(N1,N2) / (N1.Len() * N2.Len());

if( d ... )
else

then what do I do here? I''m not sure, sorry

##### Share on other sites
Do you want to know if they are facing exactly the same direction?
If you do want that test:
if (d == 1){// ... They are facing the same way}else if (d == -1){// ... They are facing opposite}else{// ... They are somewhere in between}

If you just want to know if they are pointing in roughly the same direction (ie, the direction they are facing is closer to being the same, than it is to being opposite), you can do:
if (d > 0){// ... They are facing similar direction}else if (d < 0){// ... They are facing away from each other}else{// ... They are exactly perpendicular, might want to change one of the above signs to //     include 0 and take this as either facing same direction or opposite direction}

[edited by - Flax on April 14, 2004 10:41:26 AM]

##### Share on other sites
Thank you flax.

Does anybody know any other way how to calculate if two planes are facing each other or not?
Is ther any cheaper method than Flax's?.

[edited by - papa on April 15, 2004 12:10:41 PM]

##### Share on other sites
if there is no room for error, just true or false, you could if the x y and z's of te 2 normals.

Edit: to clarify...

if (n1.x == n2.x && n1.y = n2.y) then

[edited by - honayboyz on April 15, 2004 8:32:47 PM]

##### Share on other sites
If two planes are facing each other, you have two cases. Their normals point at each other, or the normals point in the same direction. Both cases mean that the planes are paralell.

When dealing with paralell planes I got a theory, because all you need to prove is that their normals are paralell as well. So what do we know of paralell vectors? Yes, exactly, they are each other''s multiples.

Thus, a simple test like this might do it:

float norm1[3];float norm2[3];norm1 = GetFirstNormal();norm2 = GetSecondNormal();float mult = norm1[0] / norm2[0];float mult2 = norm1[1] / norm2[1];float mult3 = norm1[2] / norm2[2];if(mult == mult2 && mult == mult3)    // The planes are facing each other! Woo!

Now, the comparison above inside the if statement might have to be changed so that you only check the... say... 3 or 4 first significant numbers. Because that method will probably say they are NOT facing even if the angle between them might be something silly like 0.5 degrees, which is in my book (and in a gaming world) parallell.

Anyhow, hope that helped.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633678
• Total Posts
3013290
×