   planes perpendicular to polygons
  Can anyone give me the code to calculate a perpendicular plane for the side of a polygon? This would be for a bounding sphere collision test in which the distance to each of the perpendicular side planes and the polygon''s plane are checked to see if a collision with the polygon is reached?Thanks, please help! 
  The side normal is the cross product of the poly normal and the edge in question. Whether you normalize it or not depends on what you're going to use it for.So if n is the poly normal and v[] are the poly verts:edgeNormal = n.Cross(v[(i + 1) % numVerts] - v);<br>edgeNormal.Normalize();<br><br>You may have to do the cross product in reverse order depending on your plane convention.   </i>    <br><br><SPAN CLASS=editedby>[edited by - jyk on May 24, 2004 7:46:26 PM]</SPAN>;  Don''t know what''s up with the italics... ;  I''m not sure about this...My plane structure consists of a normal (x,y,z) and a distance...I calculate the plane of the poly (which i can do), so "n" will be the plane''s normal (the same as the poly''s normal)?I now need to calculate the side plane. The plane''s normal is the same as your "edge normal", right?What i don''t really understand (a lot of it) is how to get the "edge normal"? Is this the cross product of two of the poly''s verts? What is going on in "n.Cross(v[(i + 1) % numVerts] - v);" are you subtracting the two verts???What it looks like is the crossproduct of the (poly''s normal) and (one verts x,y,z subtracted from the other)...I''m very confused...If it helps, I have these functions that i know work:point3d crossproduct(point3d,point3d)plane getplane(point3d,point3d,point3d)and a plane structure:struct point3d{    float x,y,z;};struct plane{point3d normal;float distance;};aaarg! please help! thanks.  ;  This is tricky stuff for those that haven''t taken much math in school yet (trust me I know ), listen up!The plane is defined by a normal and a distance, you got that right.The cross product of two vectors is the vector perpendicular to them. So the cross product of (1,0,0) and (0,1,0) = (0,0,1). Get it?So if you take two adjacent verts in the poly, and subtract them, and take the cross product of that and the poly plane normal, you have the normal of the edge.The distance for the edge can be computed by taking either of those two verts and getting the dot product of that and the edge normal.Hope this helps!   --------------------------------------------------------Life would be so much easier if we could just get the source code. ;  Hi,Vampire explained it correctly, and I''ll elaborate a little as well - between the two of us, hopefully you''ll have enough info to figure it out."I calculate the plane of the poly (which i can do), so ''n'' will be the plane''s normal (the same as the poly''s normal)?"Yes."I now need to calculate the side plane. The plane''s normal is the same as your ''edge normal'', right?"Yes, the ''edge normal'' is the same as the ''side plane normal''."What i don''t really understand (a lot of it) is how to get the ''edge normal''?"Let''s take this step by step. As Vampire said, the cross product of two vectors is perpendicular to the plane in which the two vectors lie. In this case, the edge normal is the (normalized) cross product of the edge and the poly normal. Think about it or draw a picture and you should see why this is so.We have the normal, so we need to find the edge. The edge can be found by subtracting one vert from the next. This obviously must be done for each edge of the poly.The simple way to write this is edge = vert - vert. However, the last edge goes from the first vert (vert[0]) to the last vert, so we have to take this into account. That bit of code that confused you is just a tricky way of allowing for this without having a bunch of if statements. Look up the modulo operator and you should be able to figure out why it works.<br><br>So once you have the edge, then:<br><br>edgeNormal = normal.Cross(edge);<br><br>Again, you may have to reverse the order of the cross product.<br><br>Finally, you need the distance for the plane. Vert will always be a point on the plane inhabited by edge, so you can use this to find your distance:<br><br>distance = vert.Dot(edgeNormal);<br><br>I think I got that all right.<br><br>Let us know if you have any more questions :-)  </i>  ;  Just as an extra tidbit, using triangles instead of n-sided polys helps for point-in-poly tests. If its just a point-in-triangle test you don''t need to do anything with edge planes if you don''t want to. Look up barycentric space, or post something here and I''ll explain it.   --------------------------------------------------------Life would be so much easier if we could just get the source code. ;  Some very tricky stuff there! But I think I get the general idea...does this look like it might work? vector3d subvector3d(vector3d v1,vector3d v2){vector3d v3;v3.x=v1.x-v2.x;v3.y=v1.y-v2.y;v3.z=v1.z-v2.z;return v3;}bool sphere_hit_poly(bound_sphere *sphere,polygon *poly){plane poly_plane=getplane(&poly->ver[0]->wrld,&poly->ver[1]->wrld,&poly->ver[2]->wrld);if(fabs(distance_to_plane(sphere->pivot.wrld,&poly_plane))<sphere->radius){//if sphere hits poly's planefor(int a=0;a<poly->vcount;a++){plane edge;   edge.normal=crossproduct(poly_plane.normal,subvector3d(poly->ver[(a+1)%poly->vcount]->wrld,poly->ver[a]->wrld));   normalize_vector3d(&edge.normal);edge.distance=dotproduct(poly->ver[a]->wrld,edge.normal);if(fabs(distance_to_plane(sphere->pivot.wrld,&edge))<sphere->radius){return true;}}}return false;}It doesn't seem to work...but the problem could be somewhere else... Thanks! [edited by - DTMdan on May 26, 2004 10:18:13 AM];  "Some very tricky stuff there! But I think I get the general idea...does this look like it might work?"I''d try and answer, but for some reason my browser won''t display the code blocks :-( Maybe someone else will help you out, though... ;  Well here it is again without...if anyone can help? vector3d subvector3d(vector3d v1,vector3d v2){vector3d v3;v3.x=v1.x-v2.x;v3.y=v1.y-v2.y;v3.z=v1.z-v2.z;return v3;}bool sphere_hit_poly(bound_sphere *sphere,polygon *poly){plane poly_plane=getplane(&poly->ver[0]->wrld,&poly->ver[1]->wrld,&poly->ver[2]->wrld);if(fabs(distance_to_plane(sphere->pivot.wrld,&poly_plane))<sphere->radius){//if sphere hits poly''s planefor(int a=0;a<poly->vcount;a++){plane edge;   edge.normal=crossproduct(poly_plane.normal,subvector3d(poly->ver[(a+1)%poly->vcount]->wrld,poly->verLink