Changeset 7983 for code/branches/kicklib/src/external/bullet/BulletCollision/CollisionShapes/btCompoundShape.cpp
- Timestamp:
- Feb 27, 2011, 7:43:24 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/kicklib/src/external/bullet/BulletCollision/CollisionShapes/btCompoundShape.cpp
r5781 r7983 1 1 /* 2 2 Bullet Continuous Collision Detection and Physics Library 3 Copyright (c) 2003-200 6 Erwin Coumans http://continuousphysics.com/Bullet/3 Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org 4 4 5 5 This software is provided 'as-is', without any express or implied warranty. … … 17 17 #include "btCollisionShape.h" 18 18 #include "BulletCollision/BroadphaseCollision/btDbvt.h" 19 #include "LinearMath/btSerializer.h" 19 20 20 21 btCompoundShape::btCompoundShape(bool enableDynamicAabbTree) 21 : m_localAabbMin(btScalar(1e30),btScalar(1e30),btScalar(1e30)), 22 m_localAabbMax(btScalar(-1e30),btScalar(-1e30),btScalar(-1e30)), 22 : m_localAabbMin(btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT)), 23 m_localAabbMax(btScalar(-BT_LARGE_FLOAT),btScalar(-BT_LARGE_FLOAT),btScalar(-BT_LARGE_FLOAT)), 24 m_dynamicAabbTree(0), 25 m_updateRevision(1), 23 26 m_collisionMargin(btScalar(0.)), 24 m_localScaling(btScalar(1.),btScalar(1.),btScalar(1.)), 25 m_dynamicAabbTree(0), 26 m_updateRevision(1) 27 m_localScaling(btScalar(1.),btScalar(1.),btScalar(1.)) 27 28 { 28 29 m_shapeType = COMPOUND_SHAPE_PROXYTYPE; … … 52 53 //m_childShapes.push_back(shape); 53 54 btCompoundShapeChild child; 55 child.m_node = 0; 54 56 child.m_transform = localTransform; 55 57 child.m_childShape = shape; … … 94 96 m_children[childIndex].m_childShape->getAabb(newChildTransform,localAabbMin,localAabbMax); 95 97 ATTRIBUTE_ALIGNED16(btDbvtVolume) bounds=btDbvtVolume::FromMM(localAabbMin,localAabbMax); 96 int index = m_children.size()-1;98 //int index = m_children.size()-1; 97 99 m_dynamicAabbTree->update(m_children[childIndex].m_node,bounds); 98 100 } … … 110 112 } 111 113 m_children.swap(childShapeIndex,m_children.size()-1); 114 if (m_dynamicAabbTree) 115 m_children[childShapeIndex].m_node->dataAsInt = childShapeIndex; 112 116 m_children.pop_back(); 113 117 … … 125 129 if(m_children[i].m_childShape == shape) 126 130 { 127 m_children.swap(i,m_children.size()-1); 128 m_children.pop_back(); 129 //remove it from the m_dynamicAabbTree too 130 //@todo: this leads to problems due to caching in the btCompoundCollisionAlgorithm 131 //so effectively, removeChildShape is broken at the moment 132 //m_dynamicAabbTree->remove(m_aabbProxies[i]); 133 //m_aabbProxies.swap(i,m_children.size()-1); 134 //m_aabbProxies.pop_back(); 131 removeChildShapeByIndex(i); 135 132 } 136 133 } … … 146 143 // Brute force, it iterates over all the shapes left. 147 144 148 m_localAabbMin = btVector3(btScalar( 1e30),btScalar(1e30),btScalar(1e30));149 m_localAabbMax = btVector3(btScalar(- 1e30),btScalar(-1e30),btScalar(-1e30));145 m_localAabbMin = btVector3(btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT),btScalar(BT_LARGE_FLOAT)); 146 m_localAabbMax = btVector3(btScalar(-BT_LARGE_FLOAT),btScalar(-BT_LARGE_FLOAT),btScalar(-BT_LARGE_FLOAT)); 150 147 151 148 //extend the local aabbMin/aabbMax … … 224 221 for (k = 0; k < n; k++) 225 222 { 223 btAssert(masses[k]>0); 226 224 center += m_children[k].m_transform.getOrigin() * masses[k]; 227 225 totalMass += masses[k]; 228 226 } 227 228 btAssert(totalMass>0); 229 229 230 center /= totalMass; 230 231 principal.setOrigin(center); … … 272 273 273 274 275 void btCompoundShape::setLocalScaling(const btVector3& scaling) 276 { 277 278 for(int i = 0; i < m_children.size(); i++) 279 { 280 btTransform childTrans = getChildTransform(i); 281 btVector3 childScale = m_children[i].m_childShape->getLocalScaling(); 282 // childScale = childScale * (childTrans.getBasis() * scaling); 283 childScale = childScale * scaling / m_localScaling; 284 m_children[i].m_childShape->setLocalScaling(childScale); 285 childTrans.setOrigin((childTrans.getOrigin())*scaling); 286 updateChildTransform(i, childTrans); 287 recalculateLocalAabb(); 288 } 289 m_localScaling = scaling; 290 } 291 292 293 void btCompoundShape::createAabbTreeFromChildren() 294 { 295 if ( !m_dynamicAabbTree ) 296 { 297 void* mem = btAlignedAlloc(sizeof(btDbvt),16); 298 m_dynamicAabbTree = new(mem) btDbvt(); 299 btAssert(mem==m_dynamicAabbTree); 300 301 for ( int index = 0; index < m_children.size(); index++ ) 302 { 303 btCompoundShapeChild &child = m_children[index]; 304 305 //extend the local aabbMin/aabbMax 306 btVector3 localAabbMin,localAabbMax; 307 child.m_childShape->getAabb(child.m_transform,localAabbMin,localAabbMax); 308 309 const btDbvtVolume bounds=btDbvtVolume::FromMM(localAabbMin,localAabbMax); 310 child.m_node = m_dynamicAabbTree->insert(bounds,(void*)index); 311 } 312 } 313 } 314 315 316 ///fills the dataBuffer and returns the struct name (and 0 on failure) 317 const char* btCompoundShape::serialize(void* dataBuffer, btSerializer* serializer) const 318 { 319 320 btCompoundShapeData* shapeData = (btCompoundShapeData*) dataBuffer; 321 btCollisionShape::serialize(&shapeData->m_collisionShapeData, serializer); 322 323 shapeData->m_collisionMargin = float(m_collisionMargin); 324 shapeData->m_numChildShapes = m_children.size(); 325 shapeData->m_childShapePtr = 0; 326 if (shapeData->m_numChildShapes) 327 { 328 btChunk* chunk = serializer->allocate(sizeof(btCompoundShapeChildData),shapeData->m_numChildShapes); 329 btCompoundShapeChildData* memPtr = (btCompoundShapeChildData*)chunk->m_oldPtr; 330 shapeData->m_childShapePtr = (btCompoundShapeChildData*)serializer->getUniquePointer(memPtr); 331 332 for (int i=0;i<shapeData->m_numChildShapes;i++,memPtr++) 333 { 334 memPtr->m_childMargin = float(m_children[i].m_childMargin); 335 memPtr->m_childShape = (btCollisionShapeData*)serializer->getUniquePointer(m_children[i].m_childShape); 336 //don't serialize shapes that already have been serialized 337 if (!serializer->findPointer(m_children[i].m_childShape)) 338 { 339 btChunk* chunk = serializer->allocate(m_children[i].m_childShape->calculateSerializeBufferSize(),1); 340 const char* structType = m_children[i].m_childShape->serialize(chunk->m_oldPtr,serializer); 341 serializer->finalizeChunk(chunk,structType,BT_SHAPE_CODE,m_children[i].m_childShape); 342 } 343 344 memPtr->m_childShapeType = m_children[i].m_childShapeType; 345 m_children[i].m_transform.serializeFloat(memPtr->m_transform); 346 } 347 serializer->finalizeChunk(chunk,"btCompoundShapeChildData",BT_ARRAY_CODE,chunk->m_oldPtr); 348 } 349 return "btCompoundShapeData"; 350 } 351
Note: See TracChangeset
for help on using the changeset viewer.