[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 "BulletCollision/CollisionShapes/btPolyhedralConvexShape.h" |
---|
| 17 | |
---|
| 18 | btPolyhedralConvexShape::btPolyhedralConvexShape() :btConvexInternalShape(), |
---|
| 19 | m_localAabbMin(1,1,1), |
---|
| 20 | m_localAabbMax(-1,-1,-1), |
---|
| 21 | m_isLocalAabbValid(false), |
---|
| 22 | m_optionalHull(0) |
---|
| 23 | { |
---|
| 24 | |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | btVector3 btPolyhedralConvexShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0)const |
---|
| 29 | { |
---|
| 30 | int i; |
---|
| 31 | btVector3 supVec(0,0,0); |
---|
| 32 | |
---|
| 33 | btScalar maxDot(btScalar(-1e30)); |
---|
| 34 | |
---|
| 35 | btVector3 vec = vec0; |
---|
| 36 | btScalar lenSqr = vec.length2(); |
---|
| 37 | if (lenSqr < btScalar(0.0001)) |
---|
| 38 | { |
---|
| 39 | vec.setValue(1,0,0); |
---|
| 40 | } else |
---|
| 41 | { |
---|
| 42 | btScalar rlen = btScalar(1.) / btSqrt(lenSqr ); |
---|
| 43 | vec *= rlen; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | btVector3 vtx; |
---|
| 47 | btScalar newDot; |
---|
| 48 | |
---|
| 49 | for (i=0;i<getNumVertices();i++) |
---|
| 50 | { |
---|
| 51 | getVertex(i,vtx); |
---|
| 52 | newDot = vec.dot(vtx); |
---|
| 53 | if (newDot > maxDot) |
---|
| 54 | { |
---|
| 55 | maxDot = newDot; |
---|
| 56 | supVec = vtx; |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | return supVec; |
---|
| 61 | |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | void btPolyhedralConvexShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const |
---|
| 65 | { |
---|
| 66 | int i; |
---|
| 67 | |
---|
| 68 | btVector3 vtx; |
---|
| 69 | btScalar newDot; |
---|
| 70 | |
---|
| 71 | for (i=0;i<numVectors;i++) |
---|
| 72 | { |
---|
| 73 | supportVerticesOut[i][3] = btScalar(-1e30); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | for (int j=0;j<numVectors;j++) |
---|
| 77 | { |
---|
| 78 | |
---|
| 79 | const btVector3& vec = vectors[j]; |
---|
| 80 | |
---|
| 81 | for (i=0;i<getNumVertices();i++) |
---|
| 82 | { |
---|
| 83 | getVertex(i,vtx); |
---|
| 84 | newDot = vec.dot(vtx); |
---|
| 85 | if (newDot > supportVerticesOut[j][3]) |
---|
| 86 | { |
---|
| 87 | //WARNING: don't swap next lines, the w component would get overwritten! |
---|
| 88 | supportVerticesOut[j] = vtx; |
---|
| 89 | supportVerticesOut[j][3] = newDot; |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | void btPolyhedralConvexShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const |
---|
| 98 | { |
---|
| 99 | //not yet, return box inertia |
---|
| 100 | |
---|
| 101 | btScalar margin = getMargin(); |
---|
| 102 | |
---|
| 103 | btTransform ident; |
---|
| 104 | ident.setIdentity(); |
---|
| 105 | btVector3 aabbMin,aabbMax; |
---|
| 106 | getAabb(ident,aabbMin,aabbMax); |
---|
| 107 | btVector3 halfExtents = (aabbMax-aabbMin)*btScalar(0.5); |
---|
| 108 | |
---|
| 109 | btScalar lx=btScalar(2.)*(halfExtents.x()+margin); |
---|
| 110 | btScalar ly=btScalar(2.)*(halfExtents.y()+margin); |
---|
| 111 | btScalar lz=btScalar(2.)*(halfExtents.z()+margin); |
---|
| 112 | const btScalar x2 = lx*lx; |
---|
| 113 | const btScalar y2 = ly*ly; |
---|
| 114 | const btScalar z2 = lz*lz; |
---|
| 115 | const btScalar scaledmass = mass * btScalar(0.08333333); |
---|
| 116 | |
---|
| 117 | inertia = scaledmass * (btVector3(y2+z2,x2+z2,x2+y2)); |
---|
| 118 | |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | void btPolyhedralConvexShape::getAabb(const btTransform& trans,btVector3& aabbMin,btVector3& aabbMax) const |
---|
| 124 | { |
---|
| 125 | getNonvirtualAabb(trans,aabbMin,aabbMax,getMargin()); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | void btPolyhedralConvexShape::setLocalScaling(const btVector3& scaling) |
---|
| 131 | { |
---|
| 132 | btConvexInternalShape::setLocalScaling(scaling); |
---|
| 133 | recalcLocalAabb(); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | void btPolyhedralConvexShape::recalcLocalAabb() |
---|
| 137 | { |
---|
| 138 | m_isLocalAabbValid = true; |
---|
| 139 | |
---|
| 140 | #if 1 |
---|
| 141 | static const btVector3 _directions[] = |
---|
| 142 | { |
---|
| 143 | btVector3( 1., 0., 0.), |
---|
| 144 | btVector3( 0., 1., 0.), |
---|
| 145 | btVector3( 0., 0., 1.), |
---|
| 146 | btVector3( -1., 0., 0.), |
---|
| 147 | btVector3( 0., -1., 0.), |
---|
| 148 | btVector3( 0., 0., -1.) |
---|
| 149 | }; |
---|
| 150 | |
---|
| 151 | btVector3 _supporting[] = |
---|
| 152 | { |
---|
| 153 | btVector3( 0., 0., 0.), |
---|
| 154 | btVector3( 0., 0., 0.), |
---|
| 155 | btVector3( 0., 0., 0.), |
---|
| 156 | btVector3( 0., 0., 0.), |
---|
| 157 | btVector3( 0., 0., 0.), |
---|
| 158 | btVector3( 0., 0., 0.) |
---|
| 159 | }; |
---|
| 160 | |
---|
| 161 | batchedUnitVectorGetSupportingVertexWithoutMargin(_directions, _supporting, 6); |
---|
| 162 | |
---|
| 163 | for ( int i = 0; i < 3; ++i ) |
---|
| 164 | { |
---|
| 165 | m_localAabbMax[i] = _supporting[i][i] + m_collisionMargin; |
---|
| 166 | m_localAabbMin[i] = _supporting[i + 3][i] - m_collisionMargin; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | #else |
---|
| 170 | |
---|
| 171 | for (int i=0;i<3;i++) |
---|
| 172 | { |
---|
| 173 | btVector3 vec(btScalar(0.),btScalar(0.),btScalar(0.)); |
---|
| 174 | vec[i] = btScalar(1.); |
---|
| 175 | btVector3 tmp = localGetSupportingVertex(vec); |
---|
| 176 | m_localAabbMax[i] = tmp[i]+m_collisionMargin; |
---|
| 177 | vec[i] = btScalar(-1.); |
---|
| 178 | tmp = localGetSupportingVertex(vec); |
---|
| 179 | m_localAabbMin[i] = tmp[i]-m_collisionMargin; |
---|
| 180 | } |
---|
| 181 | #endif |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | |
---|