Bullet Collision Detection & Physics Library
btCollisionWorldImporter.cpp
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2014 Erwin Coumans http://bulletphysics.org
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 
18 #include "LinearMath/btSerializer.h" //for btBulletSerializedArrays definition
19 
20 #ifdef SUPPORT_GIMPACT_SHAPE_IMPORT
22 #endif //SUPPORT_GIMPACT_SHAPE_IMPORT
23 
25 :m_collisionWorld(world),
26 m_verboseMode(0)
27 {
28 
29 }
30 
32 {
33 }
34 
35 
36 
37 
38 
40 {
41 
42  m_shapeMap.clear();
43  m_bodyMap.clear();
44 
45  int i;
46 
47  for (i=0;i<arrays->m_bvhsDouble.size();i++)
48  {
50  btQuantizedBvhDoubleData* bvhData = arrays->m_bvhsDouble[i];
51  bvh->deSerializeDouble(*bvhData);
52  m_bvhMap.insert(arrays->m_bvhsDouble[i],bvh);
53  }
54  for (i=0;i<arrays->m_bvhsFloat.size();i++)
55  {
57  btQuantizedBvhFloatData* bvhData = arrays->m_bvhsFloat[i];
58  bvh->deSerializeFloat(*bvhData);
59  m_bvhMap.insert(arrays->m_bvhsFloat[i],bvh);
60  }
61 
62 
63 
64 
65 
66  for (i=0;i<arrays->m_colShapeData.size();i++)
67  {
68  btCollisionShapeData* shapeData = arrays->m_colShapeData[i];
69  btCollisionShape* shape = convertCollisionShape(shapeData);
70  if (shape)
71  {
72  // printf("shapeMap.insert(%x,%x)\n",shapeData,shape);
73  m_shapeMap.insert(shapeData,shape);
74  }
75 
76  if (shape&& shapeData->m_name)
77  {
78  char* newname = duplicateName(shapeData->m_name);
79  m_objectNameMap.insert(shape,newname);
80  m_nameShapeMap.insert(newname,shape);
81  }
82  }
83 
84 
85  for (i=0;i<arrays->m_collisionObjectDataDouble.size();i++)
86  {
88  btCollisionShape** shapePtr = m_shapeMap.find(colObjData->m_collisionShape);
89  if (shapePtr && *shapePtr)
90  {
91  btTransform startTransform;
92  colObjData->m_worldTransform.m_origin.m_floats[3] = 0.f;
93  startTransform.deSerializeDouble(colObjData->m_worldTransform);
94 
95  btCollisionShape* shape = (btCollisionShape*)*shapePtr;
96  btCollisionObject* body = createCollisionObject(startTransform,shape,colObjData->m_name);
97  body->setFriction(btScalar(colObjData->m_friction));
98  body->setRestitution(btScalar(colObjData->m_restitution));
99 
100 #ifdef USE_INTERNAL_EDGE_UTILITY
102  {
104  if (trimesh->getTriangleInfoMap())
105  {
107  }
108  }
109 #endif //USE_INTERNAL_EDGE_UTILITY
110  m_bodyMap.insert(colObjData,body);
111  } else
112  {
113  printf("error: no shape found\n");
114  }
115  }
116  for (i=0;i<arrays->m_collisionObjectDataFloat.size();i++)
117  {
119  btCollisionShape** shapePtr = m_shapeMap.find(colObjData->m_collisionShape);
120  if (shapePtr && *shapePtr)
121  {
122  btTransform startTransform;
123  colObjData->m_worldTransform.m_origin.m_floats[3] = 0.f;
124  startTransform.deSerializeFloat(colObjData->m_worldTransform);
125 
126  btCollisionShape* shape = (btCollisionShape*)*shapePtr;
127  btCollisionObject* body = createCollisionObject(startTransform,shape,colObjData->m_name);
128 
129 #ifdef USE_INTERNAL_EDGE_UTILITY
131  {
133  if (trimesh->getTriangleInfoMap())
134  {
136  }
137  }
138 #endif //USE_INTERNAL_EDGE_UTILITY
139  m_bodyMap.insert(colObjData,body);
140  } else
141  {
142  printf("error: no shape found\n");
143  }
144  }
145 
146  return true;
147 }
148 
149 
150 
152 {
153  int i;
154 
155  for (i=0;i<m_allocatedCollisionObjects.size();i++)
156  {
157  if(m_collisionWorld)
159  delete m_allocatedCollisionObjects[i];
160  }
161 
163 
164 
165  for (i=0;i<m_allocatedCollisionShapes.size();i++)
166  {
167  delete m_allocatedCollisionShapes[i];
168  }
170 
171 
172  for (i=0;i<m_allocatedBvhs.size();i++)
173  {
174  delete m_allocatedBvhs[i];
175  }
177 
178  for (i=0;i<m_allocatedTriangleInfoMaps.size();i++)
179  {
180  delete m_allocatedTriangleInfoMaps[i];
181  }
183  for (i=0;i<m_allocatedTriangleIndexArrays.size();i++)
184  {
186  }
188  for (i=0;i<m_allocatedNames.size();i++)
189  {
190  delete[] m_allocatedNames[i];
191  }
193 
195  {
197 
198  for(int a = 0;a < curData->m_numMeshParts;a++)
199  {
200  btMeshPartData* curPart = &curData->m_meshPartsPtr[a];
201  if(curPart->m_vertices3f)
202  delete [] curPart->m_vertices3f;
203 
204  if(curPart->m_vertices3d)
205  delete [] curPart->m_vertices3d;
206 
207  if(curPart->m_indices32)
208  delete [] curPart->m_indices32;
209 
210  if(curPart->m_3indices16)
211  delete [] curPart->m_3indices16;
212 
213  if(curPart->m_indices16)
214  delete [] curPart->m_indices16;
215 
216  if (curPart->m_3indices8)
217  delete [] curPart->m_3indices8;
218 
219  }
220  delete [] curData->m_meshPartsPtr;
221  delete curData;
222  }
224 
225  for (i=0;i<m_indexArrays.size();i++)
226  {
228  }
230 
231  for (i=0;i<m_shortIndexArrays.size();i++)
232  {
234  }
236 
237  for (i=0;i<m_charIndexArrays.size();i++)
238  {
240  }
242 
243  for (i=0;i<m_floatVertexArrays.size();i++)
244  {
246  }
248 
249  for (i=0;i<m_doubleVertexArrays.size();i++)
250  {
252  }
254 
255 
256 }
257 
258 
259 
261 {
262  btCollisionShape* shape = 0;
263 
264  switch (shapeData->m_shapeType)
265  {
267  {
268  btStaticPlaneShapeData* planeData = (btStaticPlaneShapeData*)shapeData;
269  btVector3 planeNormal,localScaling;
270  planeNormal.deSerializeFloat(planeData->m_planeNormal);
271  localScaling.deSerializeFloat(planeData->m_localScaling);
272  shape = createPlaneShape(planeNormal,planeData->m_planeConstant);
273  shape->setLocalScaling(localScaling);
274 
275  break;
276  }
278  {
280  btCollisionShapeData* colShapeData = (btCollisionShapeData*) &scaledMesh->m_trimeshShapeData;
282  btCollisionShape* childShape = convertCollisionShape(colShapeData);
283  btBvhTriangleMeshShape* meshShape = (btBvhTriangleMeshShape*)childShape;
284  btVector3 localScaling;
285  localScaling.deSerializeFloat(scaledMesh->m_localScaling);
286 
287  shape = createScaledTrangleMeshShape(meshShape, localScaling);
288  break;
289  }
290 #ifdef SUPPORT_GIMPACT_SHAPE_IMPORT
292  {
293  btGImpactMeshShapeData* gimpactData = (btGImpactMeshShapeData*) shapeData;
294  if (gimpactData->m_gimpactSubType == CONST_GIMPACT_TRIMESH_SHAPE)
295  {
297  btTriangleIndexVertexArray* meshInterface = createMeshInterface(*interfaceData);
298 
299 
300  btGImpactMeshShape* gimpactShape = createGimpactShape(meshInterface);
301  btVector3 localScaling;
302  localScaling.deSerializeFloat(gimpactData->m_localScaling);
303  gimpactShape->setLocalScaling(localScaling);
304  gimpactShape->setMargin(btScalar(gimpactData->m_collisionMargin));
305  gimpactShape->updateBound();
306  shape = gimpactShape;
307  } else
308  {
309  printf("unsupported gimpact sub type\n");
310  }
311  break;
312  }
313 #endif //SUPPORT_GIMPACT_SHAPE_IMPORT
314  //The btCapsuleShape* API has issue passing the margin/scaling/halfextents unmodified through the API
315  //so deal with this
317  {
318  btCapsuleShapeData* capData = (btCapsuleShapeData*)shapeData;
319 
320 
321  switch (capData->m_upAxis)
322  {
323  case 0:
324  {
325  shape = createCapsuleShapeX(1,1);
326  break;
327  }
328  case 1:
329  {
330  shape = createCapsuleShapeY(1,1);
331  break;
332  }
333  case 2:
334  {
335  shape = createCapsuleShapeZ(1,1);
336  break;
337  }
338  default:
339  {
340  printf("error: wrong up axis for btCapsuleShape\n");
341  }
342 
343 
344  };
345  if (shape)
346  {
347  btCapsuleShape* cap = (btCapsuleShape*) shape;
348  cap->deSerializeFloat(capData);
349  }
350  break;
351  }
354  case BOX_SHAPE_PROXYTYPE:
358  {
360  btVector3 implicitShapeDimensions;
361  implicitShapeDimensions.deSerializeFloat(bsd->m_implicitShapeDimensions);
362  btVector3 localScaling;
363  localScaling.deSerializeFloat(bsd->m_localScaling);
365  switch (shapeData->m_shapeType)
366  {
367  case BOX_SHAPE_PROXYTYPE:
368  {
369  btBoxShape* box= (btBoxShape*)createBoxShape(implicitShapeDimensions/localScaling+margin);
370  //box->initializePolyhedralFeatures();
371  shape = box;
372 
373  break;
374  }
376  {
377  shape = createSphereShape(implicitShapeDimensions.getX());
378  break;
379  }
380 
382  {
383  btCylinderShapeData* cylData = (btCylinderShapeData*) shapeData;
384  btVector3 halfExtents = implicitShapeDimensions+margin;
385  switch (cylData->m_upAxis)
386  {
387  case 0:
388  {
389  shape = createCylinderShapeX(halfExtents.getY(),halfExtents.getX());
390  break;
391  }
392  case 1:
393  {
394  shape = createCylinderShapeY(halfExtents.getX(),halfExtents.getY());
395  break;
396  }
397  case 2:
398  {
399  shape = createCylinderShapeZ(halfExtents.getX(),halfExtents.getZ());
400  break;
401  }
402  default:
403  {
404  printf("unknown Cylinder up axis\n");
405  }
406 
407  };
408 
409 
410 
411  break;
412  }
414  {
415  btConeShapeData* conData = (btConeShapeData*) shapeData;
416  btVector3 halfExtents = implicitShapeDimensions;//+margin;
417  switch (conData->m_upIndex)
418  {
419  case 0:
420  {
421  shape = createConeShapeX(halfExtents.getY(),halfExtents.getX());
422  break;
423  }
424  case 1:
425  {
426  shape = createConeShapeY(halfExtents.getX(),halfExtents.getY());
427  break;
428  }
429  case 2:
430  {
431  shape = createConeShapeZ(halfExtents.getX(),halfExtents.getZ());
432  break;
433  }
434  default:
435  {
436  printf("unknown Cone up axis\n");
437  }
438 
439  };
440 
441 
442 
443  break;
444  }
446  {
448  int numSpheres = mss->m_localPositionArraySize;
449 
452  radii.resize(numSpheres);
453  tmpPos.resize(numSpheres);
454  int i;
455  for ( i=0;i<numSpheres;i++)
456  {
457  tmpPos[i].deSerializeFloat(mss->m_localPositionArrayPtr[i].m_pos);
458  radii[i] = mss->m_localPositionArrayPtr[i].m_radius;
459  }
460  shape = createMultiSphereShape(&tmpPos[0],&radii[0],numSpheres);
461  break;
462  }
464  {
465  // int sz = sizeof(btConvexHullShapeData);
466  // int sz2 = sizeof(btConvexInternalShapeData);
467  // int sz3 = sizeof(btCollisionShapeData);
468  btConvexHullShapeData* convexData = (btConvexHullShapeData*)bsd;
469  int numPoints = convexData->m_numUnscaledPoints;
470 
472  tmpPoints.resize(numPoints);
473  int i;
474  for ( i=0;i<numPoints;i++)
475  {
476 #ifdef BT_USE_DOUBLE_PRECISION
477  if (convexData->m_unscaledPointsDoublePtr)
478  tmpPoints[i].deSerialize(convexData->m_unscaledPointsDoublePtr[i]);
479  if (convexData->m_unscaledPointsFloatPtr)
480  tmpPoints[i].deSerializeFloat(convexData->m_unscaledPointsFloatPtr[i]);
481 #else
482  if (convexData->m_unscaledPointsFloatPtr)
483  tmpPoints[i].deSerialize(convexData->m_unscaledPointsFloatPtr[i]);
484  if (convexData->m_unscaledPointsDoublePtr)
485  tmpPoints[i].deSerializeDouble(convexData->m_unscaledPointsDoublePtr[i]);
486 #endif //BT_USE_DOUBLE_PRECISION
487  }
489  for (i=0;i<numPoints;i++)
490  {
491  hullShape->addPoint(tmpPoints[i]);
492  }
493  hullShape->setMargin(bsd->m_collisionMargin);
494  //hullShape->initializePolyhedralFeatures();
495  shape = hullShape;
496  break;
497  }
498  default:
499  {
500  printf("error: cannot create shape type (%d)\n",shapeData->m_shapeType);
501  }
502  }
503 
504  if (shape)
505  {
506  shape->setMargin(bsd->m_collisionMargin);
507 
508  btVector3 localScaling;
509  localScaling.deSerializeFloat(bsd->m_localScaling);
510  shape->setLocalScaling(localScaling);
511 
512  }
513  break;
514  }
516  {
517  btTriangleMeshShapeData* trimesh = (btTriangleMeshShapeData*)shapeData;
519  btTriangleIndexVertexArray* meshInterface = createMeshInterface(*interfaceData);
520  if (!meshInterface->getNumSubParts())
521  {
522  return 0;
523  }
524 
525  btVector3 scaling; scaling.deSerializeFloat(trimesh->m_meshInterface.m_scaling);
526  meshInterface->setScaling(scaling);
527 
528 
529  btOptimizedBvh* bvh = 0;
530 #if 1
531  if (trimesh->m_quantizedFloatBvh)
532  {
533  btOptimizedBvh** bvhPtr = m_bvhMap.find(trimesh->m_quantizedFloatBvh);
534  if (bvhPtr && *bvhPtr)
535  {
536  bvh = *bvhPtr;
537  } else
538  {
539  bvh = createOptimizedBvh();
540  bvh->deSerializeFloat(*trimesh->m_quantizedFloatBvh);
541  }
542  }
543  if (trimesh->m_quantizedDoubleBvh)
544  {
545  btOptimizedBvh** bvhPtr = m_bvhMap.find(trimesh->m_quantizedDoubleBvh);
546  if (bvhPtr && *bvhPtr)
547  {
548  bvh = *bvhPtr;
549  } else
550  {
551  bvh = createOptimizedBvh();
552  bvh->deSerializeDouble(*trimesh->m_quantizedDoubleBvh);
553  }
554  }
555 #endif
556 
557 
558  btBvhTriangleMeshShape* trimeshShape = createBvhTriangleMeshShape(meshInterface,bvh);
559  trimeshShape->setMargin(trimesh->m_collisionMargin);
560  shape = trimeshShape;
561 
562  if (trimesh->m_triangleInfoMap)
563  {
565  map->deSerialize(*trimesh->m_triangleInfoMap);
566  trimeshShape->setTriangleInfoMap(map);
567 
568 #ifdef USE_INTERNAL_EDGE_UTILITY
569  gContactAddedCallback = btAdjustInternalEdgeContactsCallback;
570 #endif //USE_INTERNAL_EDGE_UTILITY
571 
572  }
573 
574  //printf("trimesh->m_collisionMargin=%f\n",trimesh->m_collisionMargin);
575  break;
576  }
578  {
579  btCompoundShapeData* compoundData = (btCompoundShapeData*)shapeData;
580  btCompoundShape* compoundShape = createCompoundShape();
581 
582  //btCompoundShapeChildData* childShapeDataArray = &compoundData->m_childShapePtr[0];
583 
584 
586  for (int i=0;i<compoundData->m_numChildShapes;i++)
587  {
588  //btCompoundShapeChildData* ptr = &compoundData->m_childShapePtr[i];
589 
590  btCollisionShapeData* cd = compoundData->m_childShapePtr[i].m_childShape;
591 
592  btCollisionShape* childShape = convertCollisionShape(cd);
593  if (childShape)
594  {
595  btTransform localTransform;
596  localTransform.deSerializeFloat(compoundData->m_childShapePtr[i].m_transform);
597  compoundShape->addChildShape(localTransform,childShape);
598  } else
599  {
600 #ifdef _DEBUG
601  printf("error: couldn't create childShape for compoundShape\n");
602 #endif
603  }
604 
605  }
606  shape = compoundShape;
607 
608  break;
609  }
611  {
612  return 0;
613  }
614  default:
615  {
616 #ifdef _DEBUG
617  printf("unsupported shape type (%d)\n",shapeData->m_shapeType);
618 #endif
619  }
620  }
621 
622  return shape;
623 
624 }
625 
626 
627 
629 {
630  if (name)
631  {
632  int l = (int)strlen(name);
633  char* newName = new char[l+1];
634  memcpy(newName,name,l);
635  newName[l] = 0;
636  m_allocatedNames.push_back(newName);
637  return newName;
638  }
639  return 0;
640 }
641 
642 
643 
644 
645 
646 
647 
648 
649 
650 
651 
653 {
655 
656  for (int i=0;i<meshData.m_numMeshParts;i++)
657  {
658  btIndexedMesh meshPart;
659  meshPart.m_numTriangles = meshData.m_meshPartsPtr[i].m_numTriangles;
660  meshPart.m_numVertices = meshData.m_meshPartsPtr[i].m_numVertices;
661 
662 
663  if (meshData.m_meshPartsPtr[i].m_indices32)
664  {
665  meshPart.m_indexType = PHY_INTEGER;
666  meshPart.m_triangleIndexStride = 3*sizeof(int);
667  int* indexArray = (int*)btAlignedAlloc(sizeof(int)*3*meshPart.m_numTriangles,16);
668  m_indexArrays.push_back(indexArray);
669  for (int j=0;j<3*meshPart.m_numTriangles;j++)
670  {
671  indexArray[j] = meshData.m_meshPartsPtr[i].m_indices32[j].m_value;
672  }
673  meshPart.m_triangleIndexBase = (const unsigned char*)indexArray;
674  } else
675  {
676  if (meshData.m_meshPartsPtr[i].m_3indices16)
677  {
678  meshPart.m_indexType = PHY_SHORT;
679  meshPart.m_triangleIndexStride = sizeof(short int)*3;//sizeof(btShortIntIndexTripletData);
680 
681  short int* indexArray = (short int*)btAlignedAlloc(sizeof(short int)*3*meshPart.m_numTriangles,16);
682  m_shortIndexArrays.push_back(indexArray);
683 
684  for (int j=0;j<meshPart.m_numTriangles;j++)
685  {
686  indexArray[3*j] = meshData.m_meshPartsPtr[i].m_3indices16[j].m_values[0];
687  indexArray[3*j+1] = meshData.m_meshPartsPtr[i].m_3indices16[j].m_values[1];
688  indexArray[3*j+2] = meshData.m_meshPartsPtr[i].m_3indices16[j].m_values[2];
689  }
690 
691  meshPart.m_triangleIndexBase = (const unsigned char*)indexArray;
692  }
693  if (meshData.m_meshPartsPtr[i].m_indices16)
694  {
695  meshPart.m_indexType = PHY_SHORT;
696  meshPart.m_triangleIndexStride = 3*sizeof(short int);
697  short int* indexArray = (short int*)btAlignedAlloc(sizeof(short int)*3*meshPart.m_numTriangles,16);
698  m_shortIndexArrays.push_back(indexArray);
699  for (int j=0;j<3*meshPart.m_numTriangles;j++)
700  {
701  indexArray[j] = meshData.m_meshPartsPtr[i].m_indices16[j].m_value;
702  }
703 
704  meshPart.m_triangleIndexBase = (const unsigned char*)indexArray;
705  }
706 
707  if (meshData.m_meshPartsPtr[i].m_3indices8)
708  {
709  meshPart.m_indexType = PHY_UCHAR;
710  meshPart.m_triangleIndexStride = sizeof(unsigned char)*3;
711 
712  unsigned char* indexArray = (unsigned char*)btAlignedAlloc(sizeof(unsigned char)*3*meshPart.m_numTriangles,16);
713  m_charIndexArrays.push_back(indexArray);
714 
715  for (int j=0;j<meshPart.m_numTriangles;j++)
716  {
717  indexArray[3*j] = meshData.m_meshPartsPtr[i].m_3indices8[j].m_values[0];
718  indexArray[3*j+1] = meshData.m_meshPartsPtr[i].m_3indices8[j].m_values[1];
719  indexArray[3*j+2] = meshData.m_meshPartsPtr[i].m_3indices8[j].m_values[2];
720  }
721 
722  meshPart.m_triangleIndexBase = (const unsigned char*)indexArray;
723  }
724  }
725 
726  if (meshData.m_meshPartsPtr[i].m_vertices3f)
727  {
728  meshPart.m_vertexType = PHY_FLOAT;
729  meshPart.m_vertexStride = sizeof(btVector3FloatData);
731  m_floatVertexArrays.push_back(vertices);
732 
733  for (int j=0;j<meshPart.m_numVertices;j++)
734  {
735  vertices[j].m_floats[0] = meshData.m_meshPartsPtr[i].m_vertices3f[j].m_floats[0];
736  vertices[j].m_floats[1] = meshData.m_meshPartsPtr[i].m_vertices3f[j].m_floats[1];
737  vertices[j].m_floats[2] = meshData.m_meshPartsPtr[i].m_vertices3f[j].m_floats[2];
738  vertices[j].m_floats[3] = meshData.m_meshPartsPtr[i].m_vertices3f[j].m_floats[3];
739  }
740  meshPart.m_vertexBase = (const unsigned char*)vertices;
741  } else
742  {
743  meshPart.m_vertexType = PHY_DOUBLE;
744  meshPart.m_vertexStride = sizeof(btVector3DoubleData);
745 
746 
749 
750  for (int j=0;j<meshPart.m_numVertices;j++)
751  {
752  vertices[j].m_floats[0] = meshData.m_meshPartsPtr[i].m_vertices3d[j].m_floats[0];
753  vertices[j].m_floats[1] = meshData.m_meshPartsPtr[i].m_vertices3d[j].m_floats[1];
754  vertices[j].m_floats[2] = meshData.m_meshPartsPtr[i].m_vertices3d[j].m_floats[2];
755  vertices[j].m_floats[3] = meshData.m_meshPartsPtr[i].m_vertices3d[j].m_floats[3];
756  }
757  meshPart.m_vertexBase = (const unsigned char*)vertices;
758  }
759 
760  if (meshPart.m_triangleIndexBase && meshPart.m_vertexBase)
761  {
762  meshInterface->addIndexedMesh(meshPart,meshPart.m_indexType);
763  }
764  }
765 
766  return meshInterface;
767 }
768 
769 
771 {
772  //create a new btStridingMeshInterfaceData that is an exact copy of shapedata and store it in the WorldImporter
774 
775  newData->m_scaling = interfaceData->m_scaling;
776  newData->m_numMeshParts = interfaceData->m_numMeshParts;
777  newData->m_meshPartsPtr = new btMeshPartData[newData->m_numMeshParts];
778 
779  for(int i = 0;i < newData->m_numMeshParts;i++)
780  {
781  btMeshPartData* curPart = &interfaceData->m_meshPartsPtr[i];
782  btMeshPartData* curNewPart = &newData->m_meshPartsPtr[i];
783 
784  curNewPart->m_numTriangles = curPart->m_numTriangles;
785  curNewPart->m_numVertices = curPart->m_numVertices;
786 
787  if(curPart->m_vertices3f)
788  {
789  curNewPart->m_vertices3f = new btVector3FloatData[curNewPart->m_numVertices];
790  memcpy(curNewPart->m_vertices3f,curPart->m_vertices3f,sizeof(btVector3FloatData) * curNewPart->m_numVertices);
791  }
792  else
793  curNewPart->m_vertices3f = NULL;
794 
795  if(curPart->m_vertices3d)
796  {
797  curNewPart->m_vertices3d = new btVector3DoubleData[curNewPart->m_numVertices];
798  memcpy(curNewPart->m_vertices3d,curPart->m_vertices3d,sizeof(btVector3DoubleData) * curNewPart->m_numVertices);
799  }
800  else
801  curNewPart->m_vertices3d = NULL;
802 
803  int numIndices = curNewPart->m_numTriangles * 3;
806  bool uninitialized3indices8Workaround =false;
807 
808  if(curPart->m_indices32)
809  {
810  uninitialized3indices8Workaround=true;
811  curNewPart->m_indices32 = new btIntIndexData[numIndices];
812  memcpy(curNewPart->m_indices32,curPart->m_indices32,sizeof(btIntIndexData) * numIndices);
813  }
814  else
815  curNewPart->m_indices32 = NULL;
816 
817  if(curPart->m_3indices16)
818  {
819  uninitialized3indices8Workaround=true;
820  curNewPart->m_3indices16 = new btShortIntIndexTripletData[curNewPart->m_numTriangles];
821  memcpy(curNewPart->m_3indices16,curPart->m_3indices16,sizeof(btShortIntIndexTripletData) * curNewPart->m_numTriangles);
822  }
823  else
824  curNewPart->m_3indices16 = NULL;
825 
826  if(curPart->m_indices16)
827  {
828  uninitialized3indices8Workaround=true;
829  curNewPart->m_indices16 = new btShortIntIndexData[numIndices];
830  memcpy(curNewPart->m_indices16,curPart->m_indices16,sizeof(btShortIntIndexData) * numIndices);
831  }
832  else
833  curNewPart->m_indices16 = NULL;
834 
835  if(!uninitialized3indices8Workaround && curPart->m_3indices8)
836  {
837  curNewPart->m_3indices8 = new btCharIndexTripletData[curNewPart->m_numTriangles];
838  memcpy(curNewPart->m_3indices8,curPart->m_3indices8,sizeof(btCharIndexTripletData) * curNewPart->m_numTriangles);
839  }
840  else
841  curNewPart->m_3indices8 = NULL;
842 
843  }
844 
846 
847  return(newData);
848 }
849 
850 #ifdef USE_INTERNAL_EDGE_UTILITY
852 
853 static bool btAdjustInternalEdgeContactsCallback(btManifoldPoint& cp, const btCollisionObject* colObj0,int partId0,int index0,const btCollisionObject* colObj1,int partId1,int index1)
854 {
855 
856  btAdjustInternalEdgeContacts(cp,colObj1,colObj0, partId1,index1);
857  //btAdjustInternalEdgeContacts(cp,colObj1,colObj0, partId1,index1, BT_TRIANGLE_CONVEX_BACKFACE_MODE);
858  //btAdjustInternalEdgeContacts(cp,colObj1,colObj0, partId1,index1, BT_TRIANGLE_CONVEX_DOUBLE_SIDED+BT_TRIANGLE_CONCAVE_DOUBLE_SIDED);
859  return true;
860 }
861 #endif //USE_INTERNAL_EDGE_UTILITY
862 
863 
864 /*
865 btRigidBody* btWorldImporter::createRigidBody(bool isDynamic, btScalar mass, const btTransform& startTransform,btCollisionShape* shape,const char* bodyName)
866 {
867  btVector3 localInertia;
868  localInertia.setZero();
869 
870  if (mass)
871  shape->calculateLocalInertia(mass,localInertia);
872 
873  btRigidBody* body = new btRigidBody(mass,0,shape,localInertia);
874  body->setWorldTransform(startTransform);
875 
876  if (m_dynamicsWorld)
877  m_dynamicsWorld->addRigidBody(body);
878 
879  if (bodyName)
880  {
881  char* newname = duplicateName(bodyName);
882  m_objectNameMap.insert(body,newname);
883  m_nameBodyMap.insert(newname,body);
884  }
885  m_allocatedRigidBodies.push_back(body);
886  return body;
887 
888 }
889 */
890 
892 {
893  btCollisionObject** bodyPtr = m_nameColObjMap.find(name);
894  if (bodyPtr && *bodyPtr)
895  {
896  return *bodyPtr;
897  }
898  return 0;
899 }
900 
902 {
903  btCollisionObject* colObj = new btCollisionObject();
904  colObj->setWorldTransform(startTransform);
905  colObj->setCollisionShape(shape);
906  m_collisionWorld->addCollisionObject(colObj);//todo: flags etc
907 
908  if (bodyName)
909  {
910  char* newname = duplicateName(bodyName);
911  m_objectNameMap.insert(colObj,newname);
912  m_nameColObjMap.insert(newname,colObj);
913  }
915 
916  return colObj;
917 }
918 
919 
920 
922 {
923  btStaticPlaneShape* shape = new btStaticPlaneShape(planeNormal,planeConstant);
925  return shape;
926 }
928 {
929  btBoxShape* shape = new btBoxShape(halfExtents);
931  return shape;
932 }
934 {
935  btSphereShape* shape = new btSphereShape(radius);
937  return shape;
938 }
939 
940 
942 {
943  btCapsuleShapeX* shape = new btCapsuleShapeX(radius,height);
945  return shape;
946 }
947 
949 {
950  btCapsuleShape* shape = new btCapsuleShape(radius,height);
952  return shape;
953 }
954 
956 {
957  btCapsuleShapeZ* shape = new btCapsuleShapeZ(radius,height);
959  return shape;
960 }
961 
963 {
964  btCylinderShapeX* shape = new btCylinderShapeX(btVector3(height,radius,radius));
966  return shape;
967 }
968 
970 {
971  btCylinderShape* shape = new btCylinderShape(btVector3(radius,height,radius));
973  return shape;
974 }
975 
977 {
978  btCylinderShapeZ* shape = new btCylinderShapeZ(btVector3(radius,radius,height));
980  return shape;
981 }
982 
984 {
985  btConeShapeX* shape = new btConeShapeX(radius,height);
987  return shape;
988 }
989 
991 {
992  btConeShape* shape = new btConeShape(radius,height);
994  return shape;
995 }
996 
998 {
999  btConeShapeZ* shape = new btConeShapeZ(radius,height);
1001  return shape;
1002 }
1003 
1005 {
1008  return in;
1009 }
1010 
1012 {
1013  btOptimizedBvh* bvh = new btOptimizedBvh();
1015  return bvh;
1016 }
1017 
1018 
1020 {
1021  btTriangleInfoMap* tim = new btTriangleInfoMap();
1023  return tim;
1024 }
1025 
1027 {
1028  if (bvh)
1029  {
1030  btBvhTriangleMeshShape* bvhTriMesh = new btBvhTriangleMeshShape(trimesh,bvh->isQuantized(), false);
1031  bvhTriMesh->setOptimizedBvh(bvh);
1033  return bvhTriMesh;
1034  }
1035 
1036  btBvhTriangleMeshShape* ts = new btBvhTriangleMeshShape(trimesh,true);
1038  return ts;
1039 
1040 }
1042 {
1043  return 0;
1044 }
1045 #ifdef SUPPORT_GIMPACT_SHAPE_IMPORT
1046 btGImpactMeshShape* btCollisionWorldImporter::createGimpactShape(btStridingMeshInterface* trimesh)
1047 {
1048  btGImpactMeshShape* shape = new btGImpactMeshShape(trimesh);
1050  return shape;
1051 
1052 }
1053 #endif //SUPPORT_GIMPACT_SHAPE_IMPORT
1054 
1056 {
1057  btConvexHullShape* shape = new btConvexHullShape();
1059  return shape;
1060 }
1061 
1063 {
1064  btCompoundShape* shape = new btCompoundShape();
1066  return shape;
1067 }
1068 
1069 
1071 {
1072  btScaledBvhTriangleMeshShape* shape = new btScaledBvhTriangleMeshShape(meshShape,localScaling);
1074  return shape;
1075 }
1076 
1078 {
1079  btMultiSphereShape* shape = new btMultiSphereShape(positions, radi, numSpheres);
1081  return shape;
1082 }
1083 
1084 
1085 
1086  // query for data
1088 {
1090 }
1091 
1093 {
1094  return m_allocatedCollisionShapes[index];
1095 }
1096 
1098 {
1099  btCollisionShape** shapePtr = m_nameShapeMap.find(name);
1100  if (shapePtr&& *shapePtr)
1101  {
1102  return *shapePtr;
1103  }
1104  return 0;
1105 }
1106 
1107 
1108 const char* btCollisionWorldImporter::getNameForPointer(const void* ptr) const
1109 {
1110  const char*const * namePtr = m_objectNameMap.find(ptr);
1111  if (namePtr && *namePtr)
1112  return *namePtr;
1113  return 0;
1114 }
1115 
1116 
1118 {
1119  return m_allocatedRigidBodies.size();
1120 }
1121 
1123 {
1124  return m_allocatedRigidBodies[index];
1125 }
1126 
1127 
1129 {
1130  return m_allocatedBvhs.size();
1131 }
1133 {
1134  return m_allocatedBvhs[index];
1135 }
1136 
1138 {
1140 }
1141 
1143 {
1144  return m_allocatedTriangleInfoMaps[index];
1145 }
1146 
1147 
void clear()
Definition: btHashMap.h:460
btTransformFloatData m_worldTransform
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
void push_back(const T &_Val)
btCompoundShapeChildData * m_childShapePtr
int getShapeType() const
btAlignedObjectArray< btTriangleIndexVertexArray * > m_allocatedTriangleIndexArrays
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
void deSerializeFloat(const struct btTransformFloatData &dataIn)
Definition: btTransform.h:286
btAlignedObjectArray< btCollisionShape * > m_allocatedCollisionShapes
virtual btOptimizedBvh * createOptimizedBvh()
acceleration and connectivity structures
btCollisionObject * getRigidBodyByIndex(int index) const
const btTriangleInfoMap * getTriangleInfoMap() const
The btIndexedMesh indexes a single vertex and index array.
btAlignedObjectArray< int * > m_indexArrays
virtual btCollisionObject * createCollisionObject(const btTransform &startTransform, btCollisionShape *shape, const char *bodyName)
those virtuals are called by load and can be overridden by the user
virtual void setLocalScaling(const btVector3 &scaling)
virtual btCollisionShape * createCylinderShapeZ(btScalar radius, btScalar height)
virtual btTriangleIndexVertexArray * createMeshInterface(btStridingMeshInterfaceData &meshData)
btAlignedObjectArray< btVector3DoubleData * > m_doubleVertexArrays
double m_floats[4]
Definition: btVector3.h:1320
virtual btCollisionShape * createCylinderShapeX(btScalar radius, btScalar height)
char * duplicateName(const char *name)
virtual void addCollisionObject(btCollisionObject *collisionObject, int collisionFilterGroup=btBroadphaseProxy::DefaultFilter, int collisionFilterMask=btBroadphaseProxy::AllFilter)
virtual btTriangleInfoMap * createTriangleInfoMap()
btAlignedObjectArray< struct btQuantizedBvhFloatData * > m_bvhsFloat
Definition: btSerializer.h:150
btAlignedObjectArray< struct btQuantizedBvhDoubleData * > m_bvhsDouble
Definition: btSerializer.h:149
The btMultiSphereShape represents the convex hull of a collection of spheres.
virtual btCollisionShape * createSphereShape(btScalar radius)
The btCapsuleShape represents a capsule around the Y axis, there is also the btCapsuleShapeX aligned ...
btVector3DoubleData * m_unscaledPointsDoublePtr
btAlignedObjectArray< btStridingMeshInterfaceData * > m_allocatedbtStridingMeshInterfaceDatas
The btCollisionShape class provides an interface for collision shapes that can be shared among btColl...
btCapsuleShapeX represents a capsule around the Z axis the total height is height+2*radius, so the height is just the height between the center of each &#39;sphere&#39; of the capsule caps.
btConeShape implements a Cone shape, around the X axis
Definition: btConeShape.h:113
virtual btCollisionShape * createCapsuleShapeX(btScalar radius, btScalar height)
The btSphereShape implements an implicit sphere, centered around a local origin with radius...
Definition: btSphereShape.h:22
void setScaling(const btVector3 &scaling)
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
ManifoldContactPoint collects and maintains persistent contactpoints.
int getCollisionFlags() const
btStridingMeshInterfaceData m_meshInterface
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
virtual class btConvexHullShape * createConvexHullShape()
btVector3FloatData * m_vertices3f
void addChildShape(const btTransform &localTransform, btCollisionShape *shape)
This class manages a mesh supplied by the btStridingMeshInterface interface.
Used for GIMPACT Trimesh integration.
const char * getNameForPointer(const void *ptr) const
btTriangleInfoMapData * m_triangleInfoMap
virtual void setMargin(btScalar collisionMargin)
virtual btCollisionShape * createPlaneShape(const btVector3 &planeNormal, btScalar planeConstant)
shapes
btHashMap< btHashString, btCollisionObject * > m_nameColObjMap
virtual btBvhTriangleMeshShape * createBvhTriangleMeshShape(btStridingMeshInterface *trimesh, btOptimizedBvh *bvh)
btHashMap< btHashPtr, btCollisionObject * > m_bodyMap
void setRestitution(btScalar rest)
btAlignedObjectArray< char * > m_allocatedNames
PHY_ScalarType m_indexType
const btScalar & getZ() const
Return the z value.
Definition: btVector3.h:577
btShortIntIndexData * m_indices16
void clear()
clear the array, deallocated memory. Generally it is better to use array.resize(0), to reduce performance overhead of run-time memory (de)allocations.
btTriangleInfoMap * getTriangleInfoMapByIndex(int index) const
btAlignedObjectArray< unsigned char * > m_charIndexArrays
btCharIndexTripletData * m_3indices8
virtual void setCollisionShape(btCollisionShape *collisionShape)
The btBvhTriangleMeshShape is a static-triangle mesh shape, it can only be used for fixed/non-moving ...
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
void btAdjustInternalEdgeContacts(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, const btCollisionObjectWrapper *colObj1Wrap, int partId0, int index0, int normalAdjustFlags)
Changes a btManifoldPoint collision normal to the normal from the mesh.
virtual class btCompoundShape * createCompoundShape()
The btTriangleIndexVertexArray allows to access multiple triangle meshes, by indexing into existing t...
virtual void setMargin(btScalar margin)
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
virtual btCollisionShape * createCapsuleShapeZ(btScalar radius, btScalar height)
int size() const
return the number of elements in the array
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
btShortIntIndexTripletData * m_3indices16
void addIndexedMesh(const btIndexedMesh &mesh, PHY_ScalarType indexType=PHY_INTEGER)
btAlignedObjectArray< struct btCollisionObjectFloatData * > m_collisionObjectDataFloat
Definition: btSerializer.h:157
btVector3FloatData m_origin
Definition: btTransform.h:256
void setFriction(btScalar frict)
virtual class btScaledBvhTriangleMeshShape * createScaledTrangleMeshShape(btBvhTriangleMeshShape *meshShape, const btVector3 &localScalingbtBvhTriangleMeshShape)
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
Definition: btConeShape.h:153
virtual void deSerializeDouble(struct btQuantizedBvhDoubleData &quantizedBvhDoubleData)
btAlignedObjectArray< struct btCollisionShapeData * > m_colShapeData
Definition: btSerializer.h:151
The btConeShape implements a cone shape primitive, centered around the origin and aligned with the Y ...
Definition: btConeShape.h:23
btPositionAndRadius * m_localPositionArrayPtr
btStridingMeshInterfaceData m_meshInterface
btTransformFloatData m_transform
const btScalar & getY() const
Return the y value.
Definition: btVector3.h:575
#define btAlignedFree(ptr)
btVector3FloatData m_pos
btCollisionShape * convertCollisionShape(btCollisionShapeData *shapeData)
btCollisionObject can be used to manage collision detection objects.
void setOptimizedBvh(btOptimizedBvh *bvh, const btVector3 &localScaling=btVector3(1, 1, 1))
btAlignedObjectArray< btVector3FloatData * > m_floatVertexArrays
const btScalar & getX() const
Return the x value.
Definition: btVector3.h:573
void insert(const Key &key, const Value &value)
Definition: btHashMap.h:262
The btOptimizedBvh extends the btQuantizedBvh to create AABB tree for triangle meshes, through the btStridingMeshInterface.
virtual void removeCollisionObject(btCollisionObject *collisionObject)
virtual void setMargin(btScalar margin)=0
const unsigned char * m_triangleIndexBase
void setWorldTransform(const btTransform &worldTrans)
btVector3FloatData m_implicitShapeDimensions
btVector3FloatData * m_unscaledPointsFloatPtr
virtual void setLocalScaling(const btVector3 &scaling)=0
btQuantizedBvhDoubleData * m_quantizedDoubleBvh
btAlignedObjectArray< btCollisionObject * > m_allocatedRigidBodies
btHashMap< btHashPtr, btCollisionShape * > m_shapeMap
The btBoxShape is a box primitive around the origin, its sides axis aligned with length specified by ...
Definition: btBoxShape.h:26
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
const Value * find(const Key &key) const
Definition: btHashMap.h:422
bool(* ContactAddedCallback)(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1)
btHashMap< btHashString, btCollisionShape * > m_nameShapeMap
btAlignedObjectArray< btCollisionObject * > m_allocatedCollisionObjects
btHashMap< btHashPtr, const char * > m_objectNameMap
The btTransform class supports rigid transforms with only translation and rotation and no scaling/she...
Definition: btTransform.h:34
btAlignedObjectArray< struct btCollisionObjectDoubleData * > m_collisionObjectDataDouble
Definition: btSerializer.h:156
The btStridingMeshInterface is the interface class for high performance generic access to triangle me...
CollisionWorld is interface and container for the collision detection.
btConeShapeZ implements a Cone shape, around the Z axis
Definition: btConeShape.h:133
btCollisionShape * getCollisionShapeByIndex(int index)
void setCollisionFlags(int flags)
virtual btCollisionShape * createConeShapeY(btScalar radius, btScalar height)
virtual btStridingMeshInterfaceData * createStridingMeshInterfaceData(btStridingMeshInterfaceData *interfaceData)
btTransformDoubleData m_worldTransform
virtual void deSerializeFloat(struct btQuantizedBvhFloatData &quantizedBvhFloatData)
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
btAlignedObjectArray< btTriangleInfoMap * > m_allocatedTriangleInfoMaps
bool convertAllObjects(btBulletSerializedArrays *arrays)
void resize(int newsize, const T &fillData=T())
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
virtual class btMultiSphereShape * createMultiSphereShape(const btVector3 *positions, const btScalar *radi, int numSpheres)
virtual btCollisionShape * createBoxShape(const btVector3 &halfExtents)
virtual btCollisionShape * createCylinderShapeY(btScalar radius, btScalar height)
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
void deSerialize(struct btTriangleInfoMapData &data)
fills the dataBuffer and returns the struct name (and 0 on failure)
btVector3FloatData m_localScaling
The btCylinderShape class implements a cylinder shape primitive, centered around the origin...
virtual int getNumSubParts() const
getNumSubParts returns the number of seperate subparts each subpart has a continuous array of vertice...
ContactAddedCallback gContactAddedCallback
This is to allow MaterialCombiner/Custom Friction/Restitution values.
virtual class btTriangleIndexVertexArray * createTriangleMeshContainer()
virtual btCollisionShape * createConeShapeZ(btScalar radius, btScalar height)
The btScaledBvhTriangleMeshShape allows to instance a scaled version of an existing btBvhTriangleMesh...
virtual void deleteAllData()
delete all memory collision shapes, rigid bodies, constraints etc.
#define btAlignedAlloc(size, alignment)
btHashMap< btHashPtr, btOptimizedBvh * > m_bvhMap
btCapsuleShapeZ represents a capsule around the Z axis the total height is height+2*radius, so the height is just the height between the center of each &#39;sphere&#39; of the capsule caps.
btIntIndexData * m_indices32
void deSerializeFloat(struct btCapsuleShapeData *dataBuffer)
void setTriangleInfoMap(btTriangleInfoMap *triangleInfoMap)
btVector3FloatData m_planeNormal
void addPoint(const btVector3 &point, bool recalculateLocalAabb=true)
The btConvexHullShape implements an implicit convex hull of an array of vertices. ...
virtual btCollisionShape * createConeShapeX(btScalar radius, btScalar height)
The btTriangleInfoMap stores edge angle information for some triangles. You can compute this informat...
btQuantizedBvhFloatData * m_quantizedFloatBvh
btOptimizedBvh * getBvhByIndex(int index) const
The btCompoundShape allows to store multiple other btCollisionShapes This allows for moving concave c...
void updateBound()
performs refit operation
void deSerializeFloat(const struct btVector3FloatData &dataIn)
Definition: btVector3.h:1331
btCollisionShapeData * m_childShape
The btStaticPlaneShape simulates an infinite non-moving (static) collision plane. ...
void deSerializeDouble(const struct btTransformDoubleData &dataIn)
Definition: btTransform.h:292
btAlignedObjectArray< short int * > m_shortIndexArrays
virtual btCollisionShape * createCapsuleShapeY(btScalar radius, btScalar height)
btVector3FloatData m_localScaling
btCollisionObject * getCollisionObjectByName(const char *name)
btVector3DoubleData m_origin
Definition: btTransform.h:262
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
btCollisionShape * getCollisionShapeByName(const char *name)
do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 ...
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292
PHY_ScalarType m_vertexType
virtual btCollisionShape * createConvexTriangleMeshShape(btStridingMeshInterface *trimesh)
const unsigned char * m_vertexBase
btCollisionWorldImporter(btCollisionWorld *world)
btAlignedObjectArray< btOptimizedBvh * > m_allocatedBvhs
virtual void setMargin(btScalar margin)
btVector3DoubleData * m_vertices3d