"Hard" maths: angle between 2 triangles ??
Blitz3D Forums/Blitz3D Programming/"Hard" maths: angle between 2 triangles ??
| ||
Hi working on my stencil shadow system its getting late and im a bit tired so if there are math people out there that could help would be nice. If not i'll probably solve it myself tomorrow. I need an angle between two triangles that share one edge. So they have 4 verts that can move anywhere in XYZ space, and they share 2 of those verts. Both facing same direction. Each triangle has its plane and its normal defined by the 3 verts that make it up. I need to know the angle (0 to 360) between each of the planes or normals (whichever you want). I was looking into cross product but dont have time tonight so maybe someone can give a hint? This is for a predefined list of triangles and edges that is made for each shadow caster so that when two triangles form a cavity (angle smaller than 180) the edge between them is ignored and in this way less calculation and volume updating has to be done each loop because a cavity never casts a shadow, its always "inside". Most systems dont have this optimisation but since blitz is slow enough as it is, i think we really need it. BTW any helpful soul will be included in credits :P thanks |
| ||
No actually this optimisation isnt crucial so i might leave it for later. Better finish the primary stuff and optimise it after. So this question stays open. I figured out the theory of it but putting down the equations, im not so keen on doing. |
| ||
I believe the dot product of the two normals of the planes is what you want. |
| ||
Hmm i was heading in that direction as well but i will try it later. I think this wont be in the first release, but it is a crucial feature that could speed the system up by 140%. If someone can just gimmedacodez that would be sweet :) i have enough theoretic ideas as it is. |
| ||
I think this is right .... assuming p & q are vectors MagP = sqr( px * px + py * py + pz * pz ) MagQ = sqr( qx * qx + qy * qy + qz * qz ) cos( theta ) = ( px*qx + py*qy + pz*qz ) / ( MagP * MagQ ) => Theta = Acos( ( px*qx + py*qy + pz*qz ) / ( MagP * MagQ ) ) Stevie |
| ||
:o Looks good to me. But i suppose P and Q are vectors of normals (or probably crossproduct)? I'll test. |
| ||
Hmm it works but the problem is it is always giving a positive angle because of Squareing and Squarerooting. I worked out a similar problem before so i will work this one out. It's just a matter of comparing signs of the vectors. Thanks Stevie! Here's the code i have now(mess left over from previous tests): Use WASDFC to move camera, and insert,home,pgup,pgdn,delete,end to move the 4th vertex. |
| ||
Ok i solved it. If anyone needs a function to check the angle between triangles, or rather the angle between the normals of two triangles, it is contained in there. I would have spent much more time if it wasn't for you Stevie G, so thanks again. I'm not sure this is useful enough to post it in the code archives :) |
| ||
I'm not sure this is useful enough to post it in the code archives If it's not already in there, it's certainly worth adding. ;) |
| ||
I would have spent much more time if it wasn't for you Stevie G, so thanks again. No problem, anything to speed up a demo of your shadow thing :) p.s. If you're only using normals then no reason to calculate the vector magnitude ... will always be 1? |