Back to index

supertuxkart  0.5+dfsg1
Public Member Functions | Private Member Functions | Private Attributes
SphereTriangleDetector Struct Reference

sphere-triangle to match the btDiscreteCollisionDetectorInterface More...

#include <SphereTriangleDetector.h>

Inheritance diagram for SphereTriangleDetector:
Inheritance graph
[legend]
Collaboration diagram for SphereTriangleDetector:
Collaboration graph
[legend]

List of all members.

Public Member Functions

virtual void getClosestPoints (const ClosestPointInput &input, Result &output, class btIDebugDraw *debugDraw)
 SphereTriangleDetector (btSphereShape *sphere, btTriangleShape *triangle)
virtual ~SphereTriangleDetector ()

Private Member Functions

bool collide (const btVector3 &sphereCenter, btVector3 &point, btVector3 &resultNormal, btScalar &depth, btScalar &timeOfImpact)
 combined discrete/continuous sphere-triangle
bool pointInTriangle (const btVector3 vertices[], const btVector3 &normal, btVector3 *p)
bool facecontains (const btVector3 &p, const btVector3 *vertices, btVector3 &normal)

Private Attributes

btSphereShape * m_sphere
btTriangleShapem_triangle

Detailed Description

sphere-triangle to match the btDiscreteCollisionDetectorInterface

Definition at line 29 of file SphereTriangleDetector.h.


Constructor & Destructor Documentation

SphereTriangleDetector::SphereTriangleDetector ( btSphereShape *  sphere,
btTriangleShape triangle 
)

Definition at line 22 of file SphereTriangleDetector.cpp.

:m_sphere(sphere),
m_triangle(triangle)
{

}
virtual SphereTriangleDetector::~SphereTriangleDetector ( ) [inline, virtual]

Definition at line 35 of file SphereTriangleDetector.h.

{};

Member Function Documentation

bool SphereTriangleDetector::collide ( const btVector3 sphereCenter,
btVector3 point,
btVector3 resultNormal,
btScalar depth,
btScalar timeOfImpact 
) [private]

combined discrete/continuous sphere-triangle

todo: move this gContactBreakingThreshold into a proper structure

Definition at line 85 of file SphereTriangleDetector.cpp.

{

       const btVector3* vertices = &m_triangle->getVertexPtr(0);
       const btVector3& c = sphereCenter;
       btScalar r = m_sphere->getRadius();

       btVector3 delta (0,0,0);

       btVector3 normal = (vertices[1]-vertices[0]).cross(vertices[2]-vertices[0]);
       normal.normalize();
       btVector3 p1ToCentre = c - vertices[0];
       btScalar distanceFromPlane = p1ToCentre.dot(normal);

       if (distanceFromPlane < btScalar(0.))
       {
              //triangle facing the other way
       
              distanceFromPlane *= btScalar(-1.);
              normal *= btScalar(-1.);
       }

       extern btScalar gContactBreakingThreshold;

       btScalar contactMargin = gContactBreakingThreshold;
       bool isInsideContactPlane = distanceFromPlane < r + contactMargin;
       bool isInsideShellPlane = distanceFromPlane < r;
       
       btScalar deltaDotNormal = delta.dot(normal);
       if (!isInsideShellPlane && deltaDotNormal >= btScalar(0.0))
              return false;

       // Check for contact / intersection
       bool hasContact = false;
       btVector3 contactPoint;
       if (isInsideContactPlane) {
              if (facecontains(c,vertices,normal)) {
                     // Inside the contact wedge - touches a point on the shell plane
                     hasContact = true;
                     contactPoint = c - normal*distanceFromPlane;
              } else {
                     // Could be inside one of the contact capsules
                     btScalar contactCapsuleRadiusSqr = (r + contactMargin) * (r + contactMargin);
                     btVector3 nearestOnEdge;
                     for (int i = 0; i < m_triangle->getNumEdges(); i++) {
                            
                            btPoint3 pa;
                            btPoint3 pb;
                            
                            m_triangle->getEdge(i,pa,pb);

                            btScalar distanceSqr = SegmentSqrDistance(pa,pb,c, nearestOnEdge);
                            if (distanceSqr < contactCapsuleRadiusSqr) {
                                   // Yep, we're inside a capsule
                                   hasContact = true;
                                   contactPoint = nearestOnEdge;
                            }
                            
                     }
              }
       }

       if (hasContact) {
              btVector3 contactToCentre = c - contactPoint;
              btScalar distanceSqr = contactToCentre.length2();
              if (distanceSqr < (r - MAX_OVERLAP)*(r - MAX_OVERLAP)) {
                     btScalar distance = btSqrt(distanceSqr);
                     resultNormal = contactToCentre;
                     resultNormal.normalize();
                     point = contactPoint;
                     depth = -(r-distance);
                     return true;
              }

              if (delta.dot(contactToCentre) >= btScalar(0.0)) 
                     return false;
              
              // Moving towards the contact point -> collision
              point = contactPoint;
              timeOfImpact = btScalar(0.0);
              return true;
       }
       
       return false;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool SphereTriangleDetector::facecontains ( const btVector3 p,
const btVector3 vertices,
btVector3 normal 
) [private]

Definition at line 77 of file SphereTriangleDetector.cpp.

                                                                                                         {
       btVector3 lp(p);
       btVector3 lnormal(normal);
       
       return pointInTriangle(vertices, lnormal, &lp);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void SphereTriangleDetector::getClosestPoints ( const ClosestPointInput input,
Result output,
class btIDebugDraw debugDraw 
) [virtual]

Implements btDiscreteCollisionDetectorInterface.

Definition at line 29 of file SphereTriangleDetector.cpp.

{

       (void)debugDraw;
       const btTransform& transformA = input.m_transformA;
       const btTransform& transformB = input.m_transformB;

       btVector3 point,normal;
       btScalar timeOfImpact = btScalar(1.);
       btScalar depth = btScalar(0.);
//     output.m_distance = btScalar(1e30);
       //move sphere into triangle space
       btTransform   sphereInTr = transformB.inverseTimes(transformA);

       if (collide(sphereInTr.getOrigin(),point,normal,depth,timeOfImpact))
       {
              output.addContactPoint(transformB.getBasis()*normal,transformB*point,depth);
       }

}

Here is the call graph for this function:

Here is the caller graph for this function:

bool SphereTriangleDetector::pointInTriangle ( const btVector3  vertices[],
const btVector3 normal,
btVector3 p 
) [private]

Definition at line 173 of file SphereTriangleDetector.cpp.

{
       const btVector3* p1 = &vertices[0];
       const btVector3* p2 = &vertices[1];
       const btVector3* p3 = &vertices[2];

       btVector3 edge1( *p2 - *p1 );
       btVector3 edge2( *p3 - *p2 );
       btVector3 edge3( *p1 - *p3 );

       btVector3 p1_to_p( *p - *p1 );
       btVector3 p2_to_p( *p - *p2 );
       btVector3 p3_to_p( *p - *p3 );

       btVector3 edge1_normal( edge1.cross(normal));
       btVector3 edge2_normal( edge2.cross(normal));
       btVector3 edge3_normal( edge3.cross(normal));
       
       btScalar r1, r2, r3;
       r1 = edge1_normal.dot( p1_to_p );
       r2 = edge2_normal.dot( p2_to_p );
       r3 = edge3_normal.dot( p3_to_p );
       if ( ( r1 > 0 && r2 > 0 && r3 > 0 ) ||
            ( r1 <= 0 && r2 <= 0 && r3 <= 0 ) )
              return true;
       return false;

}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

btSphereShape* SphereTriangleDetector::m_sphere [private]

Definition at line 43 of file SphereTriangleDetector.h.

Definition at line 44 of file SphereTriangleDetector.h.


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