| [1963] | 1 | /* | 
|---|
|  | 2 | Bullet Continuous Collision Detection and Physics Library | 
|---|
|  | 3 | Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/ | 
|---|
|  | 4 |  | 
|---|
|  | 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: | 
|---|
|  | 10 |  | 
|---|
|  | 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. | 
|---|
|  | 14 | */ | 
|---|
|  | 15 |  | 
|---|
|  | 16 | //#include <stdio.h> | 
|---|
|  | 17 |  | 
|---|
|  | 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" | 
|---|
|  | 25 |  | 
|---|
| [2882] | 26 | btTriangleRaycastCallback::btTriangleRaycastCallback(const btVector3& from,const btVector3& to, unsigned int flags) | 
|---|
| [1963] | 27 | : | 
|---|
|  | 28 | m_from(from), | 
|---|
|  | 29 | m_to(to), | 
|---|
| [2882] | 30 | //@BP Mod | 
|---|
|  | 31 | m_flags(flags), | 
|---|
| [1963] | 32 | m_hitFraction(btScalar(1.)) | 
|---|
|  | 33 | { | 
|---|
|  | 34 |  | 
|---|
|  | 35 | } | 
|---|
|  | 36 |  | 
|---|
|  | 37 |  | 
|---|
|  | 38 |  | 
|---|
|  | 39 | void btTriangleRaycastCallback::processTriangle(btVector3* triangle,int partId, int triangleIndex) | 
|---|
|  | 40 | { | 
|---|
|  | 41 | const btVector3 &vert0=triangle[0]; | 
|---|
|  | 42 | const btVector3 &vert1=triangle[1]; | 
|---|
|  | 43 | const btVector3 &vert2=triangle[2]; | 
|---|
|  | 44 |  | 
|---|
|  | 45 | btVector3 v10; v10 = vert1 - vert0 ; | 
|---|
|  | 46 | btVector3 v20; v20 = vert2 - vert0 ; | 
|---|
|  | 47 |  | 
|---|
|  | 48 | btVector3 triangleNormal; triangleNormal = v10.cross( v20 ); | 
|---|
|  | 49 |  | 
|---|
|  | 50 | const btScalar dist = vert0.dot(triangleNormal); | 
|---|
|  | 51 | btScalar dist_a = triangleNormal.dot(m_from) ; | 
|---|
|  | 52 | dist_a-= dist; | 
|---|
|  | 53 | btScalar dist_b = triangleNormal.dot(m_to); | 
|---|
|  | 54 | dist_b -= dist; | 
|---|
|  | 55 |  | 
|---|
|  | 56 | if ( dist_a * dist_b >= btScalar(0.0) ) | 
|---|
|  | 57 | { | 
|---|
|  | 58 | return ; // same sign | 
|---|
|  | 59 | } | 
|---|
| [2882] | 60 | //@BP Mod - Backface filtering | 
|---|
|  | 61 | if (((m_flags & kF_FilterBackfaces) != 0) && (dist_a > btScalar(0.0))) | 
|---|
|  | 62 | { | 
|---|
|  | 63 | // Backface, skip check | 
|---|
|  | 64 | return; | 
|---|
|  | 65 | } | 
|---|
| [1963] | 66 |  | 
|---|
|  | 67 | const btScalar proj_length=dist_a-dist_b; | 
|---|
|  | 68 | const btScalar distance = (dist_a)/(proj_length); | 
|---|
|  | 69 | // Now we have the intersection point on the plane, we'll see if it's inside the triangle | 
|---|
|  | 70 | // Add an epsilon as a tolerance for the raycast, | 
|---|
|  | 71 | // in case the ray hits exacly on the edge of the triangle. | 
|---|
|  | 72 | // It must be scaled for the triangle size. | 
|---|
|  | 73 |  | 
|---|
|  | 74 | if(distance < m_hitFraction) | 
|---|
|  | 75 | { | 
|---|
|  | 76 |  | 
|---|
|  | 77 |  | 
|---|
|  | 78 | btScalar edge_tolerance =triangleNormal.length2(); | 
|---|
|  | 79 | edge_tolerance *= btScalar(-0.0001); | 
|---|
|  | 80 | btVector3 point; point.setInterpolate3( m_from, m_to, distance); | 
|---|
|  | 81 | { | 
|---|
|  | 82 | btVector3 v0p; v0p = vert0 - point; | 
|---|
|  | 83 | btVector3 v1p; v1p = vert1 - point; | 
|---|
|  | 84 | btVector3 cp0; cp0 = v0p.cross( v1p ); | 
|---|
|  | 85 |  | 
|---|
|  | 86 | if ( (btScalar)(cp0.dot(triangleNormal)) >=edge_tolerance) | 
|---|
|  | 87 | { | 
|---|
|  | 88 |  | 
|---|
|  | 89 |  | 
|---|
|  | 90 | btVector3 v2p; v2p = vert2 -  point; | 
|---|
|  | 91 | btVector3 cp1; | 
|---|
|  | 92 | cp1 = v1p.cross( v2p); | 
|---|
|  | 93 | if ( (btScalar)(cp1.dot(triangleNormal)) >=edge_tolerance) | 
|---|
|  | 94 | { | 
|---|
|  | 95 | btVector3 cp2; | 
|---|
|  | 96 | cp2 = v2p.cross(v0p); | 
|---|
|  | 97 |  | 
|---|
|  | 98 | if ( (btScalar)(cp2.dot(triangleNormal)) >=edge_tolerance) | 
|---|
|  | 99 | { | 
|---|
| [2882] | 100 | //@BP Mod | 
|---|
|  | 101 | // Triangle normal isn't normalized | 
|---|
|  | 102 | triangleNormal.normalize(); | 
|---|
| [1963] | 103 |  | 
|---|
| [2882] | 104 | //@BP Mod - Allow for unflipped normal when raycasting against backfaces | 
|---|
|  | 105 | if (((m_flags & kF_KeepUnflippedNormal) != 0) || (dist_a <= btScalar(0.0))) | 
|---|
| [1963] | 106 | { | 
|---|
| [2882] | 107 | m_hitFraction = reportHit(-triangleNormal,distance,partId,triangleIndex); | 
|---|
| [1963] | 108 | } | 
|---|
|  | 109 | else | 
|---|
|  | 110 | { | 
|---|
| [2882] | 111 | m_hitFraction = reportHit(triangleNormal,distance,partId,triangleIndex); | 
|---|
| [1963] | 112 | } | 
|---|
|  | 113 | } | 
|---|
|  | 114 | } | 
|---|
|  | 115 | } | 
|---|
|  | 116 | } | 
|---|
|  | 117 | } | 
|---|
|  | 118 | } | 
|---|
|  | 119 |  | 
|---|
|  | 120 |  | 
|---|
|  | 121 | btTriangleConvexcastCallback::btTriangleConvexcastCallback (const btConvexShape* convexShape, const btTransform& convexShapeFrom, const btTransform& convexShapeTo, const btTransform& triangleToWorld, const btScalar triangleCollisionMargin) | 
|---|
|  | 122 | { | 
|---|
|  | 123 | m_convexShape = convexShape; | 
|---|
|  | 124 | m_convexShapeFrom = convexShapeFrom; | 
|---|
|  | 125 | m_convexShapeTo = convexShapeTo; | 
|---|
|  | 126 | m_triangleToWorld = triangleToWorld; | 
|---|
| [8393] | 127 | m_hitFraction = 1.0f; | 
|---|
|  | 128 | m_triangleCollisionMargin = triangleCollisionMargin; | 
|---|
|  | 129 | m_allowedPenetration = 0.f; | 
|---|
| [1963] | 130 | } | 
|---|
|  | 131 |  | 
|---|
|  | 132 | void | 
|---|
|  | 133 | btTriangleConvexcastCallback::processTriangle (btVector3* triangle, int partId, int triangleIndex) | 
|---|
|  | 134 | { | 
|---|
|  | 135 | btTriangleShape triangleShape (triangle[0], triangle[1], triangle[2]); | 
|---|
|  | 136 | triangleShape.setMargin(m_triangleCollisionMargin); | 
|---|
|  | 137 |  | 
|---|
|  | 138 | btVoronoiSimplexSolver  simplexSolver; | 
|---|
|  | 139 | btGjkEpaPenetrationDepthSolver  gjkEpaPenetrationSolver; | 
|---|
|  | 140 |  | 
|---|
|  | 141 | //#define  USE_SUBSIMPLEX_CONVEX_CAST 1 | 
|---|
|  | 142 | //if you reenable USE_SUBSIMPLEX_CONVEX_CAST see commented out code below | 
|---|
|  | 143 | #ifdef USE_SUBSIMPLEX_CONVEX_CAST | 
|---|
|  | 144 | btSubsimplexConvexCast convexCaster(m_convexShape, &triangleShape, &simplexSolver); | 
|---|
|  | 145 | #else | 
|---|
|  | 146 | //btGjkConvexCast       convexCaster(m_convexShape,&triangleShape,&simplexSolver); | 
|---|
|  | 147 | btContinuousConvexCollision convexCaster(m_convexShape,&triangleShape,&simplexSolver,&gjkEpaPenetrationSolver); | 
|---|
|  | 148 | #endif //#USE_SUBSIMPLEX_CONVEX_CAST | 
|---|
|  | 149 |  | 
|---|
|  | 150 | btConvexCast::CastResult castResult; | 
|---|
|  | 151 | castResult.m_fraction = btScalar(1.); | 
|---|
| [8393] | 152 | castResult.m_allowedPenetration = m_allowedPenetration; | 
|---|
| [1963] | 153 | if (convexCaster.calcTimeOfImpact(m_convexShapeFrom,m_convexShapeTo,m_triangleToWorld, m_triangleToWorld, castResult)) | 
|---|
|  | 154 | { | 
|---|
|  | 155 | //add hit | 
|---|
|  | 156 | if (castResult.m_normal.length2() > btScalar(0.0001)) | 
|---|
|  | 157 | { | 
|---|
|  | 158 | if (castResult.m_fraction < m_hitFraction) | 
|---|
|  | 159 | { | 
|---|
|  | 160 | /* btContinuousConvexCast's normal is already in world space */ | 
|---|
|  | 161 | /* | 
|---|
|  | 162 | #ifdef USE_SUBSIMPLEX_CONVEX_CAST | 
|---|
|  | 163 | //rotate normal into worldspace | 
|---|
|  | 164 | castResult.m_normal = m_convexShapeFrom.getBasis() * castResult.m_normal; | 
|---|
|  | 165 | #endif //USE_SUBSIMPLEX_CONVEX_CAST | 
|---|
|  | 166 | */ | 
|---|
|  | 167 | castResult.m_normal.normalize(); | 
|---|
|  | 168 |  | 
|---|
|  | 169 | reportHit (castResult.m_normal, | 
|---|
|  | 170 | castResult.m_hitPoint, | 
|---|
|  | 171 | castResult.m_fraction, | 
|---|
|  | 172 | partId, | 
|---|
|  | 173 | triangleIndex); | 
|---|
|  | 174 | } | 
|---|
|  | 175 | } | 
|---|
|  | 176 | } | 
|---|
|  | 177 | } | 
|---|