Al's Programming Resource Homepage  Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

polygon.cpp

Go to the documentation of this file.
00001 #include <windows.h>
00002 
00003 
00004 #include "polygon.h"
00005 #include "general.h"
00006 
00007 POLYGON::POLYGON()
00008 {
00009 }
00010 
00011 POLYGON::~POLYGON()
00012 {
00013 }
00014 
00015 VECTOR POLYGON::GetNormal()
00016 {
00017         VECTOR temp; 
00018         float ux;
00019         float uy;
00020         float uz;
00021         float vx;
00022         float vy;
00023         float vz;
00024           ux = Vertex[1].x - Vertex[0].x;
00025           uy = Vertex[1].y - Vertex[0].y;
00026           uz = Vertex[1].z - Vertex[0].z;
00027           vx = Vertex[2].x - Vertex[0].x;
00028           vy = Vertex[2].y - Vertex[0].y;
00029           vz = Vertex[2].z - Vertex[0].z;
00030           temp.x = (uy*vz)-(vy*uz);
00031           temp.y = (uz*vx)-(vz*ux);
00032           temp.z = (ux*vy)-(vx*uy);
00033         return temp;
00034 }
00035 
00036 void POLYGON::SetNormal()
00037 {
00038         float ux;
00039         float uy;
00040         float uz;
00041         float vx;
00042         float vy;
00043         float vz;
00044           ux = Vertex[1].x - Vertex[0].x;
00045           uy = Vertex[1].y - Vertex[0].y;
00046           uz = Vertex[1].z - Vertex[0].z;
00047           vx = Vertex[2].x - Vertex[0].x;
00048           vy = Vertex[2].y - Vertex[0].y;
00049           vz = Vertex[2].z - Vertex[0].z;
00050           Vertex[0].nx = (uy*vz)-(vy*uz);
00051           Vertex[0].ny = (uz*vx)-(vz*ux);
00052           Vertex[0].nz = (ux*vy)-(vx*uy);
00053         Vertex[1].nx = Vertex[0].nx;
00054           Vertex[1].ny = Vertex[0].ny;
00055           Vertex[1].nz = Vertex[0].nz;
00056           Vertex[2].nx = Vertex[0].nx;
00057           Vertex[2].ny = Vertex[0].ny;
00058           Vertex[2].nz = Vertex[0].nz;
00059 }
00060 
00061 VECTOR POLYGON::GetMidPoint()
00062 {
00063     VECTOR temp;
00064     temp.x = (Vertex[0].x + Vertex[1].x + Vertex[2].x)/3;
00065     temp.y = (Vertex[0].y + Vertex[1].y + Vertex[2].y)/3;
00066     temp.z = (Vertex[0].z + Vertex[1].z + Vertex[2].z)/3;
00067     return temp;
00068 }
00069 
00070 bool POLYGON::CheckForCollision(CAMERA* pLastCam, CAMERA* pCam)
00071 {
00072   float Radius = 0.5;                            //Collision sphere radius
00073   VECTOR Vect1;                                    //Change vertex representation of data to vectors
00074   Vect1.x = Vertex[0].x;
00075   Vect1.y = Vertex[0].y;
00076   Vect1.z = Vertex[0].z;
00077   VECTOR Vect2;
00078   Vect2.x = Vertex[1].x;
00079   Vect2.y = Vertex[1].y;
00080   Vect2.z = Vertex[1].z;
00081   VECTOR Vect3;
00082   Vect3.x = Vertex[2].x;
00083   Vect3.y = Vertex[2].y;
00084   Vect3.z = Vertex[2].z;
00085   float Flag = 0;                                //Collision flag, 1 = collision occured
00086   float CamDistance;                                //Camera distance from plane
00087   float LastCamDistance;                                //LastCamera distance from plane
00088   VECTOR CameraPos;                                //Vector for the cameras position
00089   VECTOR LastCameraPos;                                //Vector for the cameras position
00090   VECTOR TempVect;                                 //Work variables
00091   float TempFloat;
00092   VECTOR Normal = GetNormal();                     //Get this polygons face normal
00093   Normal.x = Normal.x * -1;                        //Invert normal. Not sure if this is a problem with my code or an ogl thing.
00094   Normal.y = Normal.y * -1;
00095   Normal.z = Normal.z * -1;
00096   Normal = GetUnitVector(Normal);                  //Convert to a unit vector
00097   TempFloat = -1 * DotProduct(Normal, Vect1);      // Take the dot product of the face normal and any point on the face, and negate that. This will be your d (distance offset) component of the plane equation.
00098   CameraPos.x = -1 * pCam->Position.x;             //Get inverted current camera position
00099   CameraPos.y = -1 * pCam->Position.y;
00100   CameraPos.z = -1 * pCam->Position.z;
00101   CamDistance = DotProduct(Normal, CameraPos) - TempFloat;  //Distance from polygons plane
00102   LastCameraPos.x = -1 * pLastCam->Position.x;             //Get inverted last camera position
00103   LastCameraPos.y = -1 * pLastCam->Position.y;
00104   LastCameraPos.z = -1 * pLastCam->Position.z;
00105   LastCamDistance = DotProduct(Normal, LastCameraPos) - TempFloat;  //Distance from polygons plane
00106   if(!(CamDistance < Radius && LastCamDistance >= Radius))             //If CamDistance < Radius and LastCamDistance >= Radius then it passed through the polygon
00107     goto Skip;                                     //If not, end the collision test
00108   TempVect = GetEdgeVector(Vect1, Vect2);          //Create edge vector
00109   TempVect = CrossVector(Normal, TempVect);       //Cross product of edge vector and polygons normal gives us the anti-planes normal
00110   TempVect = GetUnitVector(TempVect);              //Reduce anti-plane normal to a unit vector
00111   TempFloat = -1 * DotProduct(TempVect, Vect1);    //Distance offset to plane equation
00112   TempFloat = DotProduct(TempVect, CameraPos) - TempFloat; //Find distance to the plane
00113   if(TempFloat >= Radius)// - 0.2)                    //Reducing the radius here makes sure the sphere is well on the polygon before colliding
00114     goto Skip;
00115   TempVect = GetEdgeVector(Vect2, Vect3);
00116   TempVect = CrossVector(Normal, TempVect);
00117   TempVect = GetUnitVector(TempVect);
00118   TempFloat = -1 * DotProduct(TempVect, Vect2);
00119   TempFloat = DotProduct(TempVect, CameraPos) - TempFloat;
00120   if(TempFloat >= Radius)// - 0.2)
00121     goto Skip;
00122   TempVect = GetEdgeVector(Vect3, Vect1);
00123   TempVect = CrossVector(Normal, TempVect);
00124   TempVect = GetUnitVector(TempVect);
00125   TempFloat = -1 * DotProduct(TempVect, Vect3);
00126   TempFloat = DotProduct(TempVect, CameraPos) - TempFloat;
00127   if(TempFloat >= Radius)// - 0.2)
00128     goto Skip;
00129   TempFloat = (Radius - CamDistance)/MagnitudeVector(Normal);  //Calculate new position and distance of slide
00130   pCam->Position.x -= (TempFloat * Normal.x);               //Update the camera position
00131   pCam->Position.y -= (TempFloat * Normal.y);
00132   pCam->Position.z -= (TempFloat * Normal.z);
00133   Flag = 1;                                    //Set collision flag to true
00134   return Flag;
00135 Skip:
00136   return Flag;
00137 }
00138 
00139 
00140 

Generated on Fri Dec 23 05:19:34 2005 for OpenGL MDI 2 by doxygen1.2.15