Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/bullet/BulletCollision/NarrowPhaseCollision/btRaycastCallback.cpp @ 2736

Last change on this file since 2736 was 2662, checked in by rgrieder, 16 years ago

Merged presentation branch back to trunk.

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