[1963] | 1 | /* |
---|
| 2 | Bullet Continuous Collision Detection and Physics Library |
---|
[7983] | 3 | Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org |
---|
[1963] | 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 | |
---|
[7983] | 16 | |
---|
| 17 | |
---|
[1963] | 18 | #include "btMultiSphereShape.h" |
---|
| 19 | #include "BulletCollision/CollisionShapes/btCollisionMargin.h" |
---|
| 20 | #include "LinearMath/btQuaternion.h" |
---|
[7983] | 21 | #include "LinearMath/btSerializer.h" |
---|
[1963] | 22 | |
---|
[7983] | 23 | btMultiSphereShape::btMultiSphereShape (const btVector3* positions,const btScalar* radi,int numSpheres) |
---|
| 24 | :btConvexInternalAabbCachingShape () |
---|
[1963] | 25 | { |
---|
| 26 | m_shapeType = MULTI_SPHERE_SHAPE_PROXYTYPE; |
---|
[7983] | 27 | //btScalar startMargin = btScalar(BT_LARGE_FLOAT); |
---|
[1963] | 28 | |
---|
[7983] | 29 | m_localPositionArray.resize(numSpheres); |
---|
| 30 | m_radiArray.resize(numSpheres); |
---|
| 31 | for (int i=0;i<numSpheres;i++) |
---|
[1963] | 32 | { |
---|
[7983] | 33 | m_localPositionArray[i] = positions[i]; |
---|
| 34 | m_radiArray[i] = radi[i]; |
---|
| 35 | |
---|
[1963] | 36 | } |
---|
| 37 | |
---|
[7983] | 38 | recalcLocalAabb(); |
---|
| 39 | |
---|
[1963] | 40 | } |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | btVector3 btMultiSphereShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0)const |
---|
| 44 | { |
---|
| 45 | int i; |
---|
| 46 | btVector3 supVec(0,0,0); |
---|
| 47 | |
---|
[7983] | 48 | btScalar maxDot(btScalar(-BT_LARGE_FLOAT)); |
---|
[1963] | 49 | |
---|
| 50 | |
---|
| 51 | btVector3 vec = vec0; |
---|
| 52 | btScalar lenSqr = vec.length2(); |
---|
| 53 | if (lenSqr < (SIMD_EPSILON*SIMD_EPSILON)) |
---|
| 54 | { |
---|
| 55 | vec.setValue(1,0,0); |
---|
| 56 | } else |
---|
| 57 | { |
---|
| 58 | btScalar rlen = btScalar(1.) / btSqrt(lenSqr ); |
---|
| 59 | vec *= rlen; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | btVector3 vtx; |
---|
| 63 | btScalar newDot; |
---|
| 64 | |
---|
[7983] | 65 | const btVector3* pos = &m_localPositionArray[0]; |
---|
| 66 | const btScalar* rad = &m_radiArray[0]; |
---|
| 67 | int numSpheres = m_localPositionArray.size(); |
---|
[1963] | 68 | |
---|
[7983] | 69 | for (i=0;i<numSpheres;i++) |
---|
[1963] | 70 | { |
---|
| 71 | vtx = (*pos) +vec*m_localScaling*(*rad) - vec * getMargin(); |
---|
| 72 | pos++; |
---|
| 73 | rad++; |
---|
| 74 | newDot = vec.dot(vtx); |
---|
| 75 | if (newDot > maxDot) |
---|
| 76 | { |
---|
| 77 | maxDot = newDot; |
---|
| 78 | supVec = vtx; |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | return supVec; |
---|
| 83 | |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | void btMultiSphereShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const |
---|
| 87 | { |
---|
| 88 | |
---|
| 89 | for (int j=0;j<numVectors;j++) |
---|
| 90 | { |
---|
[7983] | 91 | btScalar maxDot(btScalar(-BT_LARGE_FLOAT)); |
---|
[1963] | 92 | |
---|
| 93 | const btVector3& vec = vectors[j]; |
---|
| 94 | |
---|
| 95 | btVector3 vtx; |
---|
| 96 | btScalar newDot; |
---|
| 97 | |
---|
[7983] | 98 | const btVector3* pos = &m_localPositionArray[0]; |
---|
| 99 | const btScalar* rad = &m_radiArray[0]; |
---|
| 100 | int numSpheres = m_localPositionArray.size(); |
---|
| 101 | for (int i=0;i<numSpheres;i++) |
---|
[1963] | 102 | { |
---|
| 103 | vtx = (*pos) +vec*m_localScaling*(*rad) - vec * getMargin(); |
---|
| 104 | pos++; |
---|
| 105 | rad++; |
---|
| 106 | newDot = vec.dot(vtx); |
---|
| 107 | if (newDot > maxDot) |
---|
| 108 | { |
---|
| 109 | maxDot = newDot; |
---|
| 110 | supportVerticesOut[j] = vtx; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | void btMultiSphereShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const |
---|
| 124 | { |
---|
| 125 | //as an approximation, take the inertia of the box that bounds the spheres |
---|
| 126 | |
---|
[7983] | 127 | btVector3 localAabbMin,localAabbMax; |
---|
| 128 | getCachedLocalAabb(localAabbMin,localAabbMax); |
---|
| 129 | btVector3 halfExtents = (localAabbMax-localAabbMin)*btScalar(0.5); |
---|
[1963] | 130 | |
---|
[7983] | 131 | btScalar lx=btScalar(2.)*(halfExtents.x()); |
---|
| 132 | btScalar ly=btScalar(2.)*(halfExtents.y()); |
---|
| 133 | btScalar lz=btScalar(2.)*(halfExtents.z()); |
---|
[1963] | 134 | |
---|
[7983] | 135 | inertia.setValue(mass/(btScalar(12.0)) * (ly*ly + lz*lz), |
---|
| 136 | mass/(btScalar(12.0)) * (lx*lx + lz*lz), |
---|
| 137 | mass/(btScalar(12.0)) * (lx*lx + ly*ly)); |
---|
[1963] | 138 | |
---|
[7983] | 139 | } |
---|
[1963] | 140 | |
---|
| 141 | |
---|
[7983] | 142 | ///fills the dataBuffer and returns the struct name (and 0 on failure) |
---|
| 143 | const char* btMultiSphereShape::serialize(void* dataBuffer, btSerializer* serializer) const |
---|
| 144 | { |
---|
| 145 | btMultiSphereShapeData* shapeData = (btMultiSphereShapeData*) dataBuffer; |
---|
| 146 | btConvexInternalShape::serialize(&shapeData->m_convexInternalShapeData, serializer); |
---|
[1963] | 147 | |
---|
[7983] | 148 | int numElem = m_localPositionArray.size(); |
---|
| 149 | shapeData->m_localPositionArrayPtr = numElem ? (btPositionAndRadius*)serializer->getUniquePointer((void*)&m_localPositionArray[0]): 0; |
---|
| 150 | |
---|
| 151 | shapeData->m_localPositionArraySize = numElem; |
---|
| 152 | if (numElem) |
---|
| 153 | { |
---|
| 154 | btChunk* chunk = serializer->allocate(sizeof(btPositionAndRadius),numElem); |
---|
| 155 | btPositionAndRadius* memPtr = (btPositionAndRadius*)chunk->m_oldPtr; |
---|
| 156 | for (int i=0;i<numElem;i++,memPtr++) |
---|
| 157 | { |
---|
| 158 | m_localPositionArray[i].serializeFloat(memPtr->m_pos); |
---|
| 159 | memPtr->m_radius = float(m_radiArray[i]); |
---|
| 160 | } |
---|
| 161 | serializer->finalizeChunk(chunk,"btPositionAndRadius",BT_ARRAY_CODE,(void*)&m_localPositionArray[0]); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | return "btMultiSphereShapeData"; |
---|
[1963] | 165 | } |
---|
| 166 | |
---|
| 167 | |
---|