Bullet Collision Detection & Physics Library
btRaycastCallback.cpp
Go to the documentation of this file.
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 
24 #include "btRaycastCallback.h"
25 
27  :
28  m_from(from),
29  m_to(to),
30  //@BP Mod
31  m_flags(flags),
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  }
60 
61  if (((m_flags & kF_FilterBackfaces) != 0) && (dist_a <= btScalar(0.0)))
62  {
63  // Backface, skip check
64  return;
65  }
66 
67 
68  const btScalar proj_length=dist_a-dist_b;
69  const btScalar distance = (dist_a)/(proj_length);
70  // Now we have the intersection point on the plane, we'll see if it's inside the triangle
71  // Add an epsilon as a tolerance for the raycast,
72  // in case the ray hits exacly on the edge of the triangle.
73  // It must be scaled for the triangle size.
74 
75  if(distance < m_hitFraction)
76  {
77 
78 
79  btScalar edge_tolerance =triangleNormal.length2();
80  edge_tolerance *= btScalar(-0.0001);
81  btVector3 point; point.setInterpolate3( m_from, m_to, distance);
82  {
83  btVector3 v0p; v0p = vert0 - point;
84  btVector3 v1p; v1p = vert1 - point;
85  btVector3 cp0; cp0 = v0p.cross( v1p );
86 
87  if ( (btScalar)(cp0.dot(triangleNormal)) >=edge_tolerance)
88  {
89 
90 
91  btVector3 v2p; v2p = vert2 - point;
92  btVector3 cp1;
93  cp1 = v1p.cross( v2p);
94  if ( (btScalar)(cp1.dot(triangleNormal)) >=edge_tolerance)
95  {
96  btVector3 cp2;
97  cp2 = v2p.cross(v0p);
98 
99  if ( (btScalar)(cp2.dot(triangleNormal)) >=edge_tolerance)
100  {
101  //@BP Mod
102  // Triangle normal isn't normalized
103  triangleNormal.normalize();
104 
105  //@BP Mod - Allow for unflipped normal when raycasting against backfaces
106  if (((m_flags & kF_KeepUnflippedNormal) == 0) && (dist_a <= btScalar(0.0)))
107  {
108  m_hitFraction = reportHit(-triangleNormal,distance,partId,triangleIndex);
109  }
110  else
111  {
112  m_hitFraction = reportHit(triangleNormal,distance,partId,triangleIndex);
113  }
114  }
115  }
116  }
117  }
118  }
119 }
120 
121 
122 btTriangleConvexcastCallback::btTriangleConvexcastCallback (const btConvexShape* convexShape, const btTransform& convexShapeFrom, const btTransform& convexShapeTo, const btTransform& triangleToWorld, const btScalar triangleCollisionMargin)
123 {
124  m_convexShape = convexShape;
125  m_convexShapeFrom = convexShapeFrom;
126  m_convexShapeTo = convexShapeTo;
127  m_triangleToWorld = triangleToWorld;
128  m_hitFraction = 1.0f;
129  m_triangleCollisionMargin = triangleCollisionMargin;
130  m_allowedPenetration = 0.f;
131 }
132 
133 void
134 btTriangleConvexcastCallback::processTriangle (btVector3* triangle, int partId, int triangleIndex)
135 {
136  btTriangleShape triangleShape (triangle[0], triangle[1], triangle[2]);
137  triangleShape.setMargin(m_triangleCollisionMargin);
138 
139  btVoronoiSimplexSolver simplexSolver;
140  btGjkEpaPenetrationDepthSolver gjkEpaPenetrationSolver;
141 
142 //#define USE_SUBSIMPLEX_CONVEX_CAST 1
143 //if you reenable USE_SUBSIMPLEX_CONVEX_CAST see commented out code below
144 #ifdef USE_SUBSIMPLEX_CONVEX_CAST
145  btSubsimplexConvexCast convexCaster(m_convexShape, &triangleShape, &simplexSolver);
146 #else
147  //btGjkConvexCast convexCaster(m_convexShape,&triangleShape,&simplexSolver);
148  btContinuousConvexCollision convexCaster(m_convexShape,&triangleShape,&simplexSolver,&gjkEpaPenetrationSolver);
149 #endif //#USE_SUBSIMPLEX_CONVEX_CAST
150 
151  btConvexCast::CastResult castResult;
152  castResult.m_fraction = btScalar(1.);
153  castResult.m_allowedPenetration = m_allowedPenetration;
154  if (convexCaster.calcTimeOfImpact(m_convexShapeFrom,m_convexShapeTo,m_triangleToWorld, m_triangleToWorld, castResult))
155  {
156  //add hit
157  if (castResult.m_normal.length2() > btScalar(0.0001))
158  {
159  if (castResult.m_fraction < m_hitFraction)
160  {
161 /* btContinuousConvexCast's normal is already in world space */
162 /*
163 #ifdef USE_SUBSIMPLEX_CONVEX_CAST
164  //rotate normal into worldspace
165  castResult.m_normal = m_convexShapeFrom.getBasis() * castResult.m_normal;
166 #endif //USE_SUBSIMPLEX_CONVEX_CAST
167 */
168  castResult.m_normal.normalize();
169 
170  reportHit (castResult.m_normal,
171  castResult.m_hitPoint,
172  castResult.m_fraction,
173  partId,
174  triangleIndex);
175  }
176  }
177  }
178 }
virtual void processTriangle(btVector3 *triangle, int partId, int triangleIndex)
btContinuousConvexCollision implements angular and linear time of impact for convex objects...
btTriangleConvexcastCallback(const btConvexShape *convexShape, const btTransform &convexShapeFrom, const btTransform &convexShapeTo, const btTransform &triangleToWorld, const btScalar triangleCollisionMargin)
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition: btVector3.h:235
btVector3 & normalize()
Normalize this vector x^2 + y^2 + z^2 = 1.
Definition: btVector3.h:309
The btConvexShape is an abstract shape interface, implemented by all convex shapes such as btBoxShape...
Definition: btConvexShape.h:31
virtual void setMargin(btScalar margin)
RayResult stores the closest result alternatively, add a callback method to decide about closest/all ...
Definition: btConvexCast.h:36
btVoronoiSimplexSolver is an implementation of the closest point distance algorithm from a 1-4 points...
btVector3 cross(const btVector3 &v) const
Return the cross product between this and another vector.
Definition: btVector3.h:389
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
btScalar length2() const
Return the length of the vector squared.
Definition: btVector3.h:257
virtual btScalar reportHit(const btVector3 &hitNormalLocal, btScalar hitFraction, int partId, int triangleIndex)=0
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
btSubsimplexConvexCast implements Gino van den Bergens&#39; paper "Ray Casting against bteral Convex Obje...
EpaPenetrationDepthSolver uses the Expanding Polytope Algorithm to calculate the penetration depth be...
virtual void processTriangle(btVector3 *triangle, int partId, int triangleIndex)
virtual bool calcTimeOfImpact(const btTransform &fromA, const btTransform &toA, const btTransform &fromB, const btTransform &toB, CastResult &result)
cast a convex against another convex object
btTriangleRaycastCallback(const btVector3 &from, const btVector3 &to, unsigned int flags=0)
void setInterpolate3(const btVector3 &v0, const btVector3 &v1, btScalar rt)
Definition: btVector3.h:503
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292