CollisionDetectionNamespace::Collision_detector_Triangles Class Reference

Collision detector to test 2 Triangles. More...

#include <collision_detectors.h>

Inheritance diagram for CollisionDetectionNamespace::Collision_detector_Triangles:

CollisionDetectionNamespace::Collison_Detector_IFace

List of all members.

Public Member Functions

 Collision_detector_Triangles ()
 Class constructor.
CollisionReport FindCollision (void *pNode1, void *pNode2)

Private Member Functions

bool intersectSegmentTriangle (VECTOR3 p, VECTOR3 q, Triangle *triangle, VECTOR3 &C)


Detailed Description

Collision detector to test 2 Triangles.

Definition at line 78 of file collision_detectors.h.


Constructor & Destructor Documentation

CollisionDetectionNamespace::Collision_detector_Triangles::Collision_detector_Triangles (  )  [inline]

Class constructor.

Definition at line 82 of file collision_detectors.h.

00082 {;}


Member Function Documentation

CollisionReport Collision_detector_Triangles::FindCollision ( void *  pNode1,
void *  pNode2 
) [virtual]

Detect collision

Parameters:
[in] object 1
[in] object 2
Returns:
CollisionReport filled with collision detection results

Implements CollisionDetectionNamespace::Collison_Detector_IFace.

Definition at line 88 of file collision_detectors.cpp.

00089 {
00090        CollisionReport collisionReport;
00091 
00092        Triangle* a = (Triangle*) pNode1;
00093        Triangle* b = (Triangle*) pNode2;
00094        
00095        VECTOR3 intersectionPoint1, intersectionPoint2, intersectionPoint3,intersectionPoint4, intersectionPoint5, intersectionPoint6;
00096        VECTOR3 finalIntersectionPoint;
00097 
00098        int bV1 = (int)intersectSegmentTriangle(a->v1, a->v2, b,intersectionPoint1);
00099        int bV2 = (int)intersectSegmentTriangle(a->v1, a->v3, b,intersectionPoint2);
00100        int bV3 = (int)intersectSegmentTriangle(a->v2, a->v3, b,intersectionPoint3);
00101 
00102        int bV4, bV5, bV6;
00103 
00104        int bV = bV1 + bV2 + bV3;
00105 
00106        switch (bV)
00107        {
00108        case 0:
00109               bV4 = (int)intersectSegmentTriangle(b->v1, b->v2, a,intersectionPoint4);
00110               bV5 = (int)intersectSegmentTriangle(b->v1, b->v3, a,intersectionPoint5);
00111               bV6 = (int)intersectSegmentTriangle(b->v2, b->v3, a,intersectionPoint6);
00112               bV = bV4 + bV5 + bV6;
00113               if (bV == 2)
00114               {
00115                      collisionReport.bCollide = true;
00116 
00117                      if (bV4 == 1 && bV5 == 1)
00118                      {
00119                             finalIntersectionPoint = intersectionPoint4 + (intersectionPoint5 - intersectionPoint4);
00120                      }
00121                      else if (bV4 == 1 && bV6 == 1)
00122                      {
00123                             finalIntersectionPoint = intersectionPoint4 + (intersectionPoint6 - intersectionPoint4);
00124                      }
00125                      else
00126                      {
00127                             finalIntersectionPoint = intersectionPoint6 + (intersectionPoint5 - intersectionPoint6);
00128                      }
00129 
00130               }
00131               break;
00132        case 1:
00133               bV4 = (int)intersectSegmentTriangle(b->v1, b->v2, a,intersectionPoint4);
00134               if (bV4 == 1)
00135               {
00136                      collisionReport.bCollide = true;
00137                      
00138                      if (bV1 == 1)
00139                      {
00140                             finalIntersectionPoint = intersectionPoint1 + (intersectionPoint4 - intersectionPoint1);
00141                      }
00142                      else if (bV2 == 1)
00143                      {
00144                             finalIntersectionPoint = intersectionPoint2 + (intersectionPoint4 - intersectionPoint2);
00145                      }
00146                      else
00147                      {
00148                             finalIntersectionPoint = intersectionPoint3 + (intersectionPoint4 - intersectionPoint3);
00149                      }
00150 
00151                      break;
00152               }
00153 
00154               bV5 = (int)intersectSegmentTriangle(b->v1, b->v3, a,intersectionPoint5);
00155               if (bV5 == 1)
00156               {
00157                      collisionReport.bCollide = true;
00158                      if (bV1 == 1)
00159                      {
00160                             finalIntersectionPoint = intersectionPoint1 + (intersectionPoint5 - intersectionPoint1);
00161                      }
00162                      else if (bV2 == 1)
00163                      {
00164                             finalIntersectionPoint = intersectionPoint2 + (intersectionPoint5 - intersectionPoint2);
00165                      }
00166                      else
00167                      {
00168                             finalIntersectionPoint = intersectionPoint3 + (intersectionPoint5 - intersectionPoint3);
00169                      }
00170                      break;
00171               }
00172 
00173               bV6 = (int)intersectSegmentTriangle(b->v2, b->v3, a,intersectionPoint6);
00174               if (bV6 == 1)
00175               {
00176                      collisionReport.bCollide = true;
00177                      if (bV1 == 1)
00178                      {
00179                             finalIntersectionPoint = intersectionPoint1 + (intersectionPoint6 - intersectionPoint1);
00180                      }
00181                      else if (bV2 == 1)
00182                      {
00183                             finalIntersectionPoint = intersectionPoint2 + (intersectionPoint6 - intersectionPoint2);
00184                      }
00185                      else
00186                      {
00187                             finalIntersectionPoint = intersectionPoint3 + (intersectionPoint6 - intersectionPoint3);
00188                      }
00189               }
00190               break;
00191        case 2:
00192               collisionReport.bCollide = true;
00193               if (bV1 == 1 && bV2 == 1)
00194               {
00195                      finalIntersectionPoint = intersectionPoint1 + (intersectionPoint2 - intersectionPoint1);
00196               }
00197               else if (bV1 == 1 && bV3 == 1)
00198               {
00199                      finalIntersectionPoint = intersectionPoint1 + (intersectionPoint3 - intersectionPoint1);
00200               }
00201               else
00202               {
00203                      finalIntersectionPoint = intersectionPoint2 + (intersectionPoint3 - intersectionPoint2);
00204               }
00205 
00206 
00207               break;
00208        default:
00209               break;
00210        }
00211 
00212        if (collisionReport.bCollide)
00213               collisionReport.collisionPoints.push_back(finalIntersectionPoint);
00214 
00215        return collisionReport;
00216 }

bool Collision_detector_Triangles::intersectSegmentTriangle ( VECTOR3  p,
VECTOR3  q,
Triangle triangle,
VECTOR3 C 
) [private]

Inetersection test for segment and triangle

Parameters:
[in] start point of segment
[in] end point of segment
[in] pointer to a triangle
[out] point of intersection if intersection exists
Returns:
true if intersection exists

Definition at line 218 of file collision_detectors.cpp.

00219 {
00220        VECTOR3 ab = triangle->v2 - triangle->v1;
00221        VECTOR3 ac = triangle->v3 - triangle->v1;
00222        VECTOR3 qp = p - q;
00223 
00224 
00225        VECTOR3 n = ab.Cross(ac);
00226 
00227        float d =  qp.Dot(n);
00228 
00229        if (d <= 0.0f)
00230               return false;
00231 
00232        VECTOR3 ap = p - triangle->v1;
00233 
00234        float t = ap.Dot(n);
00235        if (t < 0.0f || t > d)
00236               return false;
00237 
00238        VECTOR3 e = qp.Cross(ap);
00239        float v = ac.Dot(e);
00240        if (v < 0.0f || v > d)
00241               return false;
00242 
00243        float w = -ab.Dot(e);
00244        if (w < 0.0f || v + w > d)
00245               return false;
00246 
00247 
00248        // Segment intersect triangle, compute intersetionPoint
00249 
00250        float ood = 1.0f / d;
00251        t *= ood;
00252        v *= ood;
00253        w *= ood;
00254        float u = 1.0f - v -w;
00255 
00256        C = p + t*(q-p);
00257 
00258        
00259 
00260        return true;
00261 }


The documentation for this class was generated from the following files:

Generated on Tue Mar 10 14:41:39 2009 for VRUT by  doxygen 1.5.5