2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
18 #include "BulletCollision/CollisionShapes/btConvexShape.h"
19 #include "BulletCollision/CollisionShapes/btTriangleShape.h"
20 #include "BulletCollision/NarrowPhaseCollision/btSubSimplexConvexCast.h"
21 #include "BulletCollision/NarrowPhaseCollision/btGjkConvexCast.h"
22 #include "BulletCollision/NarrowPhaseCollision/btContinuousConvexCollision.h"
23 #include "BulletCollision/NarrowPhaseCollision/btGjkEpaPenetrationDepthSolver.h"
24 #include "btRaycastCallback.h"
26 btTriangleRaycastCallback::btTriangleRaycastCallback(const btVector3& from, const btVector3& to, unsigned int flags)
31 m_hitFraction(btScalar(1.))
35 void btTriangleRaycastCallback::processTriangle(btVector3* triangle, int partId, int triangleIndex)
37 const btVector3& vert0 = triangle[0];
38 const btVector3& vert1 = triangle[1];
39 const btVector3& vert2 = triangle[2];
46 btVector3 triangleNormal;
47 triangleNormal = v10.cross(v20);
49 const btScalar dist = vert0.dot(triangleNormal);
50 btScalar dist_a = triangleNormal.dot(m_from);
52 btScalar dist_b = triangleNormal.dot(m_to);
55 if (dist_a * dist_b >= btScalar(0.0))
60 if (((m_flags & kF_FilterBackfaces) != 0) && (dist_a <= btScalar(0.0)))
62 // Backface, skip check
66 const btScalar proj_length = dist_a - dist_b;
67 const btScalar distance = (dist_a) / (proj_length);
68 // Now we have the intersection point on the plane, we'll see if it's inside the triangle
69 // Add an epsilon as a tolerance for the raycast,
70 // in case the ray hits exacly on the edge of the triangle.
71 // It must be scaled for the triangle size.
73 if (distance < m_hitFraction)
75 btScalar edge_tolerance = triangleNormal.length2();
76 edge_tolerance *= btScalar(-0.0001);
78 point.setInterpolate3(m_from, m_to, distance);
87 if ((btScalar)(cp0.dot(triangleNormal)) >= edge_tolerance)
93 if ((btScalar)(cp1.dot(triangleNormal)) >= edge_tolerance)
98 if ((btScalar)(cp2.dot(triangleNormal)) >= edge_tolerance)
101 // Triangle normal isn't normalized
102 triangleNormal.normalize();
104 //@BP Mod - Allow for unflipped normal when raycasting against backfaces
105 if (((m_flags & kF_KeepUnflippedNormal) == 0) && (dist_a <= btScalar(0.0)))
107 m_hitFraction = reportHit(-triangleNormal, distance, partId, triangleIndex);
111 m_hitFraction = reportHit(triangleNormal, distance, partId, triangleIndex);
120 btTriangleConvexcastCallback::btTriangleConvexcastCallback(const btConvexShape* convexShape, const btTransform& convexShapeFrom, const btTransform& convexShapeTo, const btTransform& triangleToWorld, const btScalar triangleCollisionMargin)
122 m_convexShape = convexShape;
123 m_convexShapeFrom = convexShapeFrom;
124 m_convexShapeTo = convexShapeTo;
125 m_triangleToWorld = triangleToWorld;
126 m_hitFraction = 1.0f;
127 m_triangleCollisionMargin = triangleCollisionMargin;
128 m_allowedPenetration = 0.f;
131 void btTriangleConvexcastCallback::processTriangle(btVector3* triangle, int partId, int triangleIndex)
133 btTriangleShape triangleShape(triangle[0], triangle[1], triangle[2]);
134 triangleShape.setMargin(m_triangleCollisionMargin);
136 btVoronoiSimplexSolver simplexSolver;
137 btGjkEpaPenetrationDepthSolver gjkEpaPenetrationSolver;
139 //#define USE_SUBSIMPLEX_CONVEX_CAST 1
140 //if you reenable USE_SUBSIMPLEX_CONVEX_CAST see commented out code below
141 #ifdef USE_SUBSIMPLEX_CONVEX_CAST
142 btSubsimplexConvexCast convexCaster(m_convexShape, &triangleShape, &simplexSolver);
144 //btGjkConvexCast convexCaster(m_convexShape,&triangleShape,&simplexSolver);
145 btContinuousConvexCollision convexCaster(m_convexShape, &triangleShape, &simplexSolver, &gjkEpaPenetrationSolver);
146 #endif //#USE_SUBSIMPLEX_CONVEX_CAST
148 btConvexCast::CastResult castResult;
149 castResult.m_fraction = btScalar(1.);
150 castResult.m_allowedPenetration = m_allowedPenetration;
151 if (convexCaster.calcTimeOfImpact(m_convexShapeFrom, m_convexShapeTo, m_triangleToWorld, m_triangleToWorld, castResult))
154 if (castResult.m_normal.length2() > btScalar(0.0001))
156 if (castResult.m_fraction < m_hitFraction)
158 /* btContinuousConvexCast's normal is already in world space */
160 #ifdef USE_SUBSIMPLEX_CONVEX_CAST
161 //rotate normal into worldspace
162 castResult.m_normal = m_convexShapeFrom.getBasis() * castResult.m_normal;
163 #endif //USE_SUBSIMPLEX_CONVEX_CAST
165 castResult.m_normal.normalize();
167 reportHit(castResult.m_normal,
168 castResult.m_hitPoint,
169 castResult.m_fraction,