Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

CollisionRectangle.cpp

Go to the documentation of this file.
00001 
00006 #include "CollisionRectangle.h"
00007 #include <stdlib.h>
00008 using namespace GraphicsLib;
00009 using namespace ParticleSystems;
00010 
00011 CollisionRectangle::CollisionRectangle( Point3 *c1, Point3 *c2, Point3 *c3, Point3 *c4) : CollisionObject() {
00012     corner1 = c1;
00013     corner2 = c2;
00014     corner3 = c3;
00015     corner4 = c4;
00016     normal = CalcNormal( corner4, corner3, corner2 );
00017     //calcCenter();
00018     //normal.x+=center.x;
00019     //normal.y+=center.y;
00020     //normal.z+=center.z;
00021         #ifdef __COLLISION_PLANE__
00022         std::cout << center << std::endl;
00023         #endif
00024 
00025 }
00026 
00027 CollisionRectangle::~CollisionRectangle() {
00028 }
00029 
00030 void CollisionRectangle::collide(Particle *p) {
00031     distance = getDistance( &normal,&(p->position));
00032         float ext[6];
00033         ext[0]=ext[1]=(BBheight()/2.0f);//y
00034         #ifdef __COLLISION_PLANE__
00035         //std::cout << "height " << BBheight() << std::endl;
00036         #endif
00037         ext[2]=ext[3]=(BBwidth()/2.0f);//x
00038         #ifdef __COLLISION_PLANE__
00039         //std::cout << "width " << BBwidth() << std::endl;
00040         #endif
00041         ext[4]=ext[5]=(BBdepth()/2.0f);//z
00042         #ifdef __COLLISION_PLANE__
00043         //std::cout << "depth " << BBdepth() << std::endl;
00044         #endif
00045     #ifdef __COLLISION__
00046     //std::cout << "Particle position" << " " << p->position << std::endl;
00047         //std::cout << "Particle velocity" << " " << p->velocity << std::endl;
00048         //std::cout << "Plane normal" << " " << normal << std::endl;
00049         //std::cout << "Distance" << " " << distance << std::endl;
00050     #endif
00051         
00052         //check width, height, depth
00053         //width = -x+x
00054         //height = -y+y
00055         //depth = z values
00056         for(int i =0; i < 6; i++) {
00057         #ifdef __COLLISION_PLANE__
00058         //std::cout << "ext[i]" << " " << ext[i] << std::endl;
00059         //std::cout << "Distance" << " " << distance << std::endl;
00060         #endif
00061                 if(distance <= BBheight() ) {
00062                         #ifdef __COLLISION_PLANE__
00063                         //std::cout << "ext[i]" << " " << ext[i] << std::endl;
00064                         std::cout << "CRec: Distance" << " " << distance << std::endl;
00065                         #endif
00066                         //Dot the direction of the particle with the normal
00067                         GLfloat x = 2*((p->velocity).dot(normal));
00068                         Vector newDir = normal*x;
00069                         #ifdef __COLLISION__
00070             std::cout << "CRec: Velocity " << p->velocity<< std::endl;
00071                         std::cout << "CRec: X " << x<< std::endl;
00072                         std::cout << "Plane normal " << " " << normal << std::endl;
00073                         std::cout << "CRec: New Velocity " <<newDir<< std::endl;
00074                         #endif
00075                         p->velocity = (p->velocity)-newDir;
00076                         #ifdef __COLLISION__
00077             std::cout << "CRec: Final Velocity " << p->velocity<< std::endl;
00078                         #endif
00079                         p->dead = true;
00080 
00081     }
00082         }
00083 }
00084 
00085 void CollisionRectangle::calcCenter() {
00086 
00087     //(c1-c2)-(c4-c3)
00088     //(c1-c4)-(c2-c3)
00089         Real x;
00090         if(corner1.x == corner3.x) {
00091                 x = corner1.x;
00092         } else {
00093                 x = corner3.x+corner1.x;
00094         }
00095         Real y;
00096         if(corner1.y == corner3.y) {
00097                 y = corner1.y;
00098         } else {
00099                 y = corner3.y+corner1.y;
00100         }
00101         Real z;
00102         if(corner1.z == corner3.z) {
00103                 z = corner1.z;
00104         } else {
00105                 z = corner3.z+corner1.z;
00106         }
00107         
00108    center = new Point3(x,y,z);
00109 
00110 
00111     //return center;
00112 }
00113 
00114 Real CollisionRectangle::getDistance(Vector *v, Point3 *p) {
00115     return (v->x * p->x + v->y * p->y + v->z * p->z);
00116 }
00117 
00118 void CollisionRectangle::draw() {
00119 glBegin(GL_LINE_LOOP);
00120         glVertex3f(corner1.x, corner1.y, corner1.z);
00121         glVertex3f(corner2.x, corner2.y, corner2.z);
00122     glVertex3f(corner3.x, corner3.y, corner3.z);
00123     glVertex3f(corner4.x, corner4.y, corner4.z);
00124 glEnd();
00125 
00126 }
00127 
00128 float CollisionRectangle::BBheight() {
00129         float maxVal = max(corner1.y,(max(corner2.y,(max(corner3.y,corner4.y)))));
00130         float minVal = min(corner1.y,(min(corner2.y,(min(corner3.y,corner4.y)))));
00131         #ifdef __COLLISION_PLANE__
00132         //std::cout << maxVal << " " << minVal << std::endl;
00133         #endif
00134         return fabs(minVal-maxVal);
00135 }
00136 
00137 float CollisionRectangle::BBwidth() {
00138         float maxVal = max(corner1.x,(max(corner2.x,(max(corner3.x,corner4.x)))));
00139         float minVal = min(corner1.x,(min(corner2.x,(min(corner3.x,corner4.x)))));
00140         #ifdef __COLLISION_PLANE__
00141         //std::cout << maxVal << " " << minVal << std::endl;
00142         #endif
00143         return fabs(minVal-maxVal);
00144 }
00145 
00146 float CollisionRectangle::BBdepth() {
00147         float maxVal = max(corner1.z,(max(corner2.z,(max(corner3.z,corner4.z)))));
00148         float minVal = min(corner1.z,(min(corner2.z,(min(corner3.z,corner4.z)))));
00149         #ifdef __COLLISION_PLANE__
00150         //std::cout << maxVal << " " << minVal << std::endl;
00151         #endif
00152         return fabs(minVal-maxVal);
00153 }
00154 float CollisionRectangle::max(float a, float b) {
00155         if(a < b) {
00156                 return b;
00157         } else {
00158                 return a;
00159         }
00160 }
00161 
00162 float CollisionRectangle::min(float a, float b) {
00163         if(a > b) {
00164                 return b;
00165         } else {
00166                 return a;
00167         }
00168 }
00169 
00170 void CollisionRectangle::translate() {}
00171 void CollisionRectangle::rotate() {}
00172 void CollisionRectangle::scale() {}

Generated on Thu Mar 24 11:05:02 2005 for ParticleSystem by  doxygen 1.4.1