Bullet Collision Detection & Physics Library: btRaycastCallback.cpp Source File

Bullet Collision Detection & Physics Library

btRaycastCallback.cpp
Go to the documentation of this file.
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. 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
26btTriangleRaycastCallback::btTriangleRaycastCallback(const btVector3& from, const btVector3& to, unsigned int flags)
27 : m_from(from),
28 m_to(to),
29 //@BP Mod
30 m_flags(flags),
31 m_hitFraction(btScalar(1.))
32{
33}
34
35 void btTriangleRaycastCallback::processTriangle(btVector3* triangle, int partId, int triangleIndex)
36{
37 const btVector3& vert0 = triangle[0];
38 const btVector3& vert1 = triangle[1];
39 const btVector3& vert2 = triangle[2];
40
41 btVector3 v10;
42 v10 = vert1 - vert0;
43 btVector3 v20;
44 v20 = vert2 - vert0;
45
46 btVector3 triangleNormal;
47 triangleNormal = v10.cross(v20);
48
49 const btScalar dist = vert0.dot(triangleNormal);
50 btScalar dist_a = triangleNormal.dot(m_from);
51 dist_a -= dist;
52 btScalar dist_b = triangleNormal.dot(m_to);
53 dist_b -= dist;
54
55 if (dist_a * dist_b >= btScalar(0.0))
56 {
57 return; // same sign
58 }
59
60 if (((m_flags & kF_FilterBackfaces) != 0) && (dist_a <= btScalar(0.0)))
61 {
62 // Backface, skip check
63 return;
64 }
65
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.
72
73 if (distance < m_hitFraction)
74 {
75 btScalar edge_tolerance = triangleNormal.length2();
76 edge_tolerance *= btScalar(-0.0001);
77 btVector3 point;
78 point.setInterpolate3(m_from, m_to, distance);
79 {
80 btVector3 v0p;
81 v0p = vert0 - point;
82 btVector3 v1p;
83 v1p = vert1 - point;
84 btVector3 cp0;
85 cp0 = v0p.cross(v1p);
86
87 if ((btScalar)(cp0.dot(triangleNormal)) >= edge_tolerance)
88 {
89 btVector3 v2p;
90 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 {
100 //@BP Mod
101 // Triangle normal isn't normalized
102 triangleNormal.normalize();
103
104 //@BP Mod - Allow for unflipped normal when raycasting against backfaces
105 if (((m_flags & kF_KeepUnflippedNormal) == 0) && (dist_a <= btScalar(0.0)))
106 {
107 m_hitFraction = reportHit(-triangleNormal, distance, partId, triangleIndex);
108 }
109 else
110 {
111 m_hitFraction = reportHit(triangleNormal, distance, partId, triangleIndex);
112 }
113 }
114 }
115 }
116 }
117 }
118}
119
120btTriangleConvexcastCallback::btTriangleConvexcastCallback(const btConvexShape* convexShape, const btTransform& convexShapeFrom, const btTransform& convexShapeTo, const btTransform& triangleToWorld, const btScalar triangleCollisionMargin)
121{
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;
129}
130
131 void btTriangleConvexcastCallback::processTriangle(btVector3* triangle, int partId, int triangleIndex)
132{
133 btTriangleShape triangleShape(triangle[0], triangle[1], triangle[2]);
134 triangleShape.setMargin(m_triangleCollisionMargin);
135
136 btVoronoiSimplexSolver simplexSolver;
137 btGjkEpaPenetrationDepthSolver gjkEpaPenetrationSolver;
138
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);
143#else
144 //btGjkConvexCast convexCaster(m_convexShape,&triangleShape,&simplexSolver);
145 btContinuousConvexCollision convexCaster(m_convexShape, &triangleShape, &simplexSolver, &gjkEpaPenetrationSolver);
146#endif //#USE_SUBSIMPLEX_CONVEX_CAST
147
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))
152 {
153 //add hit
154 if (castResult.m_normal.length2() > btScalar(0.0001))
155 {
156 if (castResult.m_fraction < m_hitFraction)
157 {
158 /* btContinuousConvexCast's normal is already in world space */
159 /*
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
164*/
165 castResult.m_normal.normalize();
166
167 reportHit(castResult.m_normal,
168 castResult.m_hitPoint,
169 castResult.m_fraction,
170 partId,
171 triangleIndex);
172 }
173 }
174 }
175}
btContinuousConvexCollision.h
btConvexShape.h
btGjkConvexCast.h
btGjkEpaPenetrationDepthSolver.h
btRaycastCallback.h
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
btSubSimplexConvexCast.h
btTriangleShape.h
btContinuousConvexCollision
btContinuousConvexCollision implements angular and linear time of impact for convex objects.
Definition: btContinuousConvexCollision.h:30
btConvexInternalShape::setMargin
virtual void setMargin(btScalar margin)
Definition: btConvexInternalShape.h:102
btConvexShape
The btConvexShape is an abstract shape interface, implemented by all convex shapes such as btBoxShape...
Definition: btConvexShape.h:33
btGjkEpaPenetrationDepthSolver
EpaPenetrationDepthSolver uses the Expanding Polytope Algorithm to calculate the penetration depth be...
Definition: btGjkEpaPenetrationDepthSolver.h:25
btSubsimplexConvexCast
btSubsimplexConvexCast implements Gino van den Bergens' paper "Ray Casting against bteral Convex Obje...
Definition: btSubSimplexConvexCast.h:28
btSubsimplexConvexCast::calcTimeOfImpact
virtual bool calcTimeOfImpact(const btTransform &fromA, const btTransform &toA, const btTransform &fromB, const btTransform &toB, CastResult &result)
SimsimplexConvexCast calculateTimeOfImpact calculates the time of impact+normal for the linear cast (...
Definition: btSubSimplexConvexCast.cpp:32
btTransform
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:30
btTriangleConvexcastCallback::processTriangle
virtual void processTriangle(btVector3 *triangle, int partId, int triangleIndex)
Definition: btRaycastCallback.cpp:131
btTriangleConvexcastCallback::m_hitFraction
btScalar m_hitFraction
Definition: btRaycastCallback.h:61
btTriangleConvexcastCallback::m_triangleToWorld
btTransform m_triangleToWorld
Definition: btRaycastCallback.h:60
btTriangleConvexcastCallback::m_convexShapeTo
btTransform m_convexShapeTo
Definition: btRaycastCallback.h:59
btTriangleConvexcastCallback::reportHit
virtual btScalar reportHit(const btVector3 &hitNormalLocal, const btVector3 &hitPointLocal, btScalar hitFraction, int partId, int triangleIndex)=0
btTriangleConvexcastCallback::m_convexShape
const btConvexShape * m_convexShape
Definition: btRaycastCallback.h:57
btTriangleConvexcastCallback::m_convexShapeFrom
btTransform m_convexShapeFrom
Definition: btRaycastCallback.h:58
btTriangleConvexcastCallback::m_triangleCollisionMargin
btScalar m_triangleCollisionMargin
Definition: btRaycastCallback.h:62
btTriangleConvexcastCallback::m_allowedPenetration
btScalar m_allowedPenetration
Definition: btRaycastCallback.h:63
btTriangleConvexcastCallback::btTriangleConvexcastCallback
btTriangleConvexcastCallback(const btConvexShape *convexShape, const btTransform &convexShapeFrom, const btTransform &convexShapeTo, const btTransform &triangleToWorld, const btScalar triangleCollisionMargin)
Definition: btRaycastCallback.cpp:120
btTriangleRaycastCallback::m_hitFraction
btScalar m_hitFraction
Definition: btRaycastCallback.h:45
btTriangleRaycastCallback::kF_FilterBackfaces
@ kF_FilterBackfaces
Definition: btRaycastCallback.h:35
btTriangleRaycastCallback::kF_KeepUnflippedNormal
@ kF_KeepUnflippedNormal
Definition: btRaycastCallback.h:36
btTriangleRaycastCallback::m_flags
unsigned int m_flags
Definition: btRaycastCallback.h:43
btTriangleRaycastCallback::m_to
btVector3 m_to
Definition: btRaycastCallback.h:29
btTriangleRaycastCallback::m_from
btVector3 m_from
Definition: btRaycastCallback.h:28
btTriangleRaycastCallback::processTriangle
virtual void processTriangle(btVector3 *triangle, int partId, int triangleIndex)
Definition: btRaycastCallback.cpp:35
btTriangleRaycastCallback::btTriangleRaycastCallback
btTriangleRaycastCallback(const btVector3 &from, const btVector3 &to, unsigned int flags=0)
Definition: btRaycastCallback.cpp:26
btTriangleRaycastCallback::reportHit
virtual btScalar reportHit(const btVector3 &hitNormalLocal, btScalar hitFraction, int partId, int triangleIndex)=0
btTriangleShape
Definition: btTriangleShape.h:24
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:82
btVector3::setInterpolate3
void setInterpolate3(const btVector3 &v0, const btVector3 &v1, btScalar rt)
Definition: btVector3.h:492
btVector3::cross
btVector3 cross(const btVector3 &v) const
Return the cross product between this and another vector.
Definition: btVector3.h:380
btVector3::dot
btScalar dot(const btVector3 &v) const
Return the dot product.
Definition: btVector3.h:229
btVector3::length2
btScalar length2() const
Return the length of the vector squared.
Definition: btVector3.h:251
btVector3::normalize
btVector3 & normalize()
Normalize this vector x^2 + y^2 + z^2 = 1.
Definition: btVector3.h:303
btVoronoiSimplexSolver
btVoronoiSimplexSolver is an implementation of the closest point distance algorithm from a 1-4 points...
Definition: btVoronoiSimplexSolver.h:99
btConvexCast::CastResult
RayResult stores the closest result alternatively, add a callback method to decide about closest/all ...
Definition: btConvexCast.h:47
btConvexCast::CastResult::m_normal
btVector3 m_normal
Definition: btConvexCast.h:70
btConvexCast::CastResult::m_fraction
btScalar m_fraction
Definition: btConvexCast.h:72
btConvexCast::CastResult::m_allowedPenetration
btScalar m_allowedPenetration
Definition: btConvexCast.h:74
btConvexCast::CastResult::m_hitPoint
btVector3 m_hitPoint
Definition: btConvexCast.h:71