1 /*! \file btGImpactShape.h 2 \author Francisco Leon Najera 3 */ 4 /* 5 This source file is part of GIMPACT Library. 6 7 For the latest info, see http://gimpact.sourceforge.net/ 8 9 Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371. 10 email: projectileman@yahoo.com 11 12 13 This software is provided 'as-is', without any express or implied warranty. 14 In no event will the authors be held liable for any damages arising from the use of this software. 15 Permission is granted to anyone to use this software for any purpose, 16 including commercial applications, and to alter it and redistribute it freely, 17 subject to the following restrictions: 18 19 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. 20 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 21 3. This notice may not be removed or altered from any source distribution. 22 */ 23 24 25 #ifndef GIMPACT_TRIANGLE_SHAPE_EX_H 26 #define GIMPACT_TRIANGLE_SHAPE_EX_H 27 28 #include "BulletCollision/CollisionShapes/btCollisionShape.h" 29 #include "BulletCollision/CollisionShapes/btTriangleShape.h" 30 #include "btBoxCollision.h" 31 #include "btClipPolygon.h" 32 #include "btGeometryOperations.h" 33 34 35 #define MAX_TRI_CLIPPING 16 36 37 //! Structure for collision 38 struct GIM_TRIANGLE_CONTACT 39 { 40 btScalar m_penetration_depth; 41 int m_point_count; 42 btVector4 m_separating_normal; 43 btVector3 m_points[MAX_TRI_CLIPPING]; 44 copy_fromGIM_TRIANGLE_CONTACT45 SIMD_FORCE_INLINE void copy_from(const GIM_TRIANGLE_CONTACT& other) 46 { 47 m_penetration_depth = other.m_penetration_depth; 48 m_separating_normal = other.m_separating_normal; 49 m_point_count = other.m_point_count; 50 int i = m_point_count; 51 while(i--) 52 { 53 m_points[i] = other.m_points[i]; 54 } 55 } 56 GIM_TRIANGLE_CONTACTGIM_TRIANGLE_CONTACT57 GIM_TRIANGLE_CONTACT() 58 { 59 } 60 GIM_TRIANGLE_CONTACTGIM_TRIANGLE_CONTACT61 GIM_TRIANGLE_CONTACT(const GIM_TRIANGLE_CONTACT& other) 62 { 63 copy_from(other); 64 } 65 66 //! classify points that are closer 67 void merge_points(const btVector4 & plane, 68 btScalar margin, const btVector3 * points, int point_count); 69 70 }; 71 72 73 74 class btPrimitiveTriangle 75 { 76 public: 77 btVector3 m_vertices[3]; 78 btVector4 m_plane; 79 btScalar m_margin; 80 btScalar m_dummy; btPrimitiveTriangle()81 btPrimitiveTriangle():m_margin(0.01f) 82 { 83 84 } 85 86 buildTriPlane()87 SIMD_FORCE_INLINE void buildTriPlane() 88 { 89 btVector3 normal = (m_vertices[1]-m_vertices[0]).cross(m_vertices[2]-m_vertices[0]); 90 normal.normalize(); 91 m_plane.setValue(normal[0],normal[1],normal[2],m_vertices[0].dot(normal)); 92 } 93 94 //! Test if triangles could collide 95 bool overlap_test_conservative(const btPrimitiveTriangle& other); 96 97 //! Calcs the plane which is paralele to the edge and perpendicular to the triangle plane 98 /*! 99 \pre this triangle must have its plane calculated. 100 */ get_edge_plane(int edge_index,btVector4 & plane)101 SIMD_FORCE_INLINE void get_edge_plane(int edge_index, btVector4 &plane) const 102 { 103 const btVector3 & e0 = m_vertices[edge_index]; 104 const btVector3 & e1 = m_vertices[(edge_index+1)%3]; 105 bt_edge_plane(e0,e1,m_plane,plane); 106 } 107 applyTransform(const btTransform & t)108 void applyTransform(const btTransform& t) 109 { 110 m_vertices[0] = t(m_vertices[0]); 111 m_vertices[1] = t(m_vertices[1]); 112 m_vertices[2] = t(m_vertices[2]); 113 } 114 115 //! Clips the triangle against this 116 /*! 117 \pre clipped_points must have MAX_TRI_CLIPPING size, and this triangle must have its plane calculated. 118 \return the number of clipped points 119 */ 120 int clip_triangle(btPrimitiveTriangle & other, btVector3 * clipped_points ); 121 122 //! Find collision using the clipping method 123 /*! 124 \pre this triangle and other must have their triangles calculated 125 */ 126 bool find_triangle_collision_clip_method(btPrimitiveTriangle & other, GIM_TRIANGLE_CONTACT & contacts); 127 }; 128 129 130 131 //! Helper class for colliding Bullet Triangle Shapes 132 /*! 133 This class implements a better getAabb method than the previous btTriangleShape class 134 */ 135 class btTriangleShapeEx: public btTriangleShape 136 { 137 public: 138 btTriangleShapeEx()139 btTriangleShapeEx():btTriangleShape(btVector3(0,0,0),btVector3(0,0,0),btVector3(0,0,0)) 140 { 141 } 142 btTriangleShapeEx(const btVector3 & p0,const btVector3 & p1,const btVector3 & p2)143 btTriangleShapeEx(const btVector3& p0,const btVector3& p1,const btVector3& p2): btTriangleShape(p0,p1,p2) 144 { 145 } 146 btTriangleShapeEx(const btTriangleShapeEx & other)147 btTriangleShapeEx(const btTriangleShapeEx & other): btTriangleShape(other.m_vertices1[0],other.m_vertices1[1],other.m_vertices1[2]) 148 { 149 } 150 getAabb(const btTransform & t,btVector3 & aabbMin,btVector3 & aabbMax)151 virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax)const 152 { 153 btVector3 tv0 = t(m_vertices1[0]); 154 btVector3 tv1 = t(m_vertices1[1]); 155 btVector3 tv2 = t(m_vertices1[2]); 156 157 btAABB trianglebox(tv0,tv1,tv2,m_collisionMargin); 158 aabbMin = trianglebox.m_min; 159 aabbMax = trianglebox.m_max; 160 } 161 applyTransform(const btTransform & t)162 void applyTransform(const btTransform& t) 163 { 164 m_vertices1[0] = t(m_vertices1[0]); 165 m_vertices1[1] = t(m_vertices1[1]); 166 m_vertices1[2] = t(m_vertices1[2]); 167 } 168 buildTriPlane(btVector4 & plane)169 SIMD_FORCE_INLINE void buildTriPlane(btVector4 & plane) const 170 { 171 btVector3 normal = (m_vertices1[1]-m_vertices1[0]).cross(m_vertices1[2]-m_vertices1[0]); 172 normal.normalize(); 173 plane.setValue(normal[0],normal[1],normal[2],m_vertices1[0].dot(normal)); 174 } 175 176 bool overlap_test_conservative(const btTriangleShapeEx& other); 177 }; 178 179 180 #endif //GIMPACT_TRIANGLE_MESH_SHAPE_H 181