1
\$\begingroup\$

In my code I creat a Mesh that are composed by multiple tile. those tile can have edge that are shared, and I need to know if the normal of the tile that have shared edge have the same direction, because I calculat the normal of the edge to smooth the light. before I used the Vector.Dot() , but I've sometimes two tile that have a curve that make impossible to use this function as you can see in the 4th image. and the only information that i have in the calcul is the 2 normal of the tile.

enter image description here img 1 : diferent direction diferent direction img 2 : diferent direction Same direction img 3: same direction Same direction img 4: same direction

Same direction img 5: same direction

\$\endgroup\$
  • \$\begingroup\$ Why do you consider img 4 to be in the same direction? and image 5? \$\endgroup\$ – Cedric Martens Jun 26 at 16:55
  • \$\begingroup\$ if you imagine a carpet , if you fold this carpet even if the face is to the other side it's the same face. and I need to have the same principe for my tile. \$\endgroup\$ – Dreugui Jun 27 at 6:58
  • \$\begingroup\$ Ah, for some reason I thought it was in 2D which they made little sense. Thanks \$\endgroup\$ – Cedric Martens Jun 27 at 10:56
3
\$\begingroup\$

Imagine applying a force along those normals to the faces. If the normals both face in the same direction (e.g. outward), then it would have a book closing effect, the faces would get closer. If they faced in different directions, it would just rotate the whole thing.

To make this a little easier to implement, you need to make it more abstract. The concept I described above is torque. Torque relies on the force (normal vector here) and the leverage (the vector from the shared edge to the center of the face here). We don't actually need to calculate torque here, but we can guess the relationship of the two normals based on these two (leverage and normal vector). Let's see an example:

enter image description here

The green vectors are the normals, the red is the leverage. You don't actually need the "leverage" vector to be correct, it just need to be perpendicular to the normal vector and pointing in the same direction. I'll go over this when applying it to 3d.

You can see how in the first image, to rotate the leftmost red vector so that it points in the same direction as the corresponding green (normal) vector while doing the least amount of rotation, you need to rotate it clockwise and to do the same to the other two vectors, you need to rotate that red vector counter-clockwise. On the second image however, you need to rotate both of the red vectors clockwise.

So in short, to find out whether the faces look in the same direction, you need to find the signed angle between the red and green vectors, and if they have opposite signs, they do face the same way, otherwise they don't.

In 3d this would look something like this:

1.) Pick any of the points of the shared edge (marked in orange here):

enter image description here

2.) Subtract the vector pointing to this position from the vectors pointing to a point that isn't part of the shared edge on each triangle to get two vectors (marked in red here):

enter image description here

Now use cross product on these two vectors to get the vector that's perpendicular to both (the order of the two vectors doesn't matter, just keep it consistent for the rest of the steps). The vector is marked with purple:

enter image description here

Based on the answer here, the signed angle as seen from the purple vector is

$$ k:={(\boldsymbol{n}\times\boldsymbol{a})\cdot\boldsymbol{b}\over ab} $$

$$ \cases{ \theta=\arccos{\boldsymbol{a}\cdot\boldsymbol{b}\over ab}, & if $k\ge0$;\\ \theta=2\pi-\arccos{\boldsymbol{a}\cdot\boldsymbol{b}\over ab}, & if $k<0$\\ } $$

where \$a\$ is one of the red vectors, \$b\$ is the corresponding normal vector, \$n\$ is the purple vector and \$\theta\$ is the signed angle.

Get the signed angle from both pairs and if their signs are opposite (one of them is negative, the other is positive), then they face the same direction.

\$\endgroup\$
  • \$\begingroup\$ what is ab ? ||a||*||b|| ? multiply a * b ? \$\endgroup\$ – Dreugui Jun 27 at 7:42
  • \$\begingroup\$ @Dreugui if a and b aren't bold, then it's the length of the two vectors \$\endgroup\$ – Bálint Jun 28 at 1:59
  • \$\begingroup\$ Ok, Thx, I havn't succed to use your technic to resolve my problem, but i've found an other way to do it. but thx any way ! \$\endgroup\$ – Dreugui Jun 28 at 5:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.