btBulletXmlWorldImporter.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2012 Erwin Coumans http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 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.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #include "btBulletXmlWorldImporter.h"
  14. #include "tinyxml2.h"
  15. #include "btBulletDynamicsCommon.h"
  16. #include "string_split.h"
  17. using namespace tinyxml2;
  18. struct MyLocalCaster
  19. {
  20. void* m_ptr;
  21. int m_int;
  22. MyLocalCaster()
  23. :m_ptr(0)
  24. {
  25. }
  26. };
  27. btBulletXmlWorldImporter::btBulletXmlWorldImporter(btDynamicsWorld* world)
  28. :btWorldImporter(world),
  29. m_fileVersion(-1),
  30. m_fileOk(false)
  31. {
  32. }
  33. btBulletXmlWorldImporter::~btBulletXmlWorldImporter()
  34. {
  35. }
  36. #if 0
  37. static int get_double_attribute_by_name(const XMLElement* pElement, const char* attribName,double* value)
  38. {
  39. if ( !pElement )
  40. return 0;
  41. const XMLAttribute* pAttrib=pElement->FirstAttribute();
  42. while (pAttrib)
  43. {
  44. if (pAttrib->Name()==attribName)
  45. if (pAttrib->QueryDoubleValue(value)==TIXML_SUCCESS)
  46. return 1;
  47. pAttrib=pAttrib->Next();
  48. }
  49. return 0;
  50. }
  51. #endif
  52. static int get_int_attribute_by_name(const XMLElement* pElement, const char* attribName,int* value)
  53. {
  54. if ( !pElement )
  55. return 0;
  56. const XMLAttribute* pAttrib=pElement->FirstAttribute();
  57. while (pAttrib)
  58. {
  59. if (!strcmp(pAttrib->Name(),attribName))
  60. if (pAttrib->QueryIntValue(value)==XML_SUCCESS)
  61. return 1;
  62. // if (pAttrib->QueryDoubleValue(&dval)==TIXML_SUCCESS) printf( " d=%1.1f", dval);
  63. pAttrib=pAttrib->Next();
  64. }
  65. return 0;
  66. }
  67. void stringToFloatArray(const std::string& string, btAlignedObjectArray<float>& floats)
  68. {
  69. btAlignedObjectArray<std::string> pieces;
  70. bullet_utils::split( pieces, string, " ");
  71. for ( int i = 0; i < pieces.size(); ++i)
  72. {
  73. btAssert(pieces[i]!="");
  74. floats.push_back((float)atof(pieces[i].c_str()));
  75. }
  76. }
  77. static btVector3FloatData TextToVector3Data(const char* txt)
  78. {
  79. btAssert(txt);
  80. btAlignedObjectArray<float> floats;
  81. stringToFloatArray(txt, floats);
  82. btAssert(floats.size()==4);
  83. btVector3FloatData vec4;
  84. vec4.m_floats[0] = floats[0];
  85. vec4.m_floats[1] = floats[1];
  86. vec4.m_floats[2] = floats[2];
  87. vec4.m_floats[3] = floats[3];
  88. return vec4;
  89. }
  90. void btBulletXmlWorldImporter::deSerializeVector3FloatData(XMLNode* pParent,btAlignedObjectArray<btVector3FloatData>& vectors)
  91. {
  92. XMLNode* flNode = pParent->FirstChildElement("m_floats");
  93. btAssert(flNode);
  94. while (flNode && flNode->FirstChildElement())
  95. {
  96. XMLText* pText = flNode->FirstChildElement()->ToText();
  97. // printf("value = %s\n",pText->Value());
  98. btVector3FloatData vec4 = TextToVector3Data(pText->Value());
  99. vectors.push_back(vec4);
  100. flNode = flNode->NextSibling();
  101. }
  102. }
  103. #define SET_INT_VALUE(xmlnode, targetdata, argname) \
  104. btAssert((xmlnode)->FirstChildElement(#argname) && (xmlnode)->FirstChildElement(#argname)->ToElement());\
  105. if ((xmlnode)->FirstChildElement(#argname) && (xmlnode)->FirstChildElement(#argname)->ToElement())\
  106. (targetdata)->argname= (int)atof(xmlnode->FirstChildElement(#argname)->ToElement()->GetText());
  107. #define SET_FLOAT_VALUE(xmlnode, targetdata, argname) \
  108. btAssert((xmlnode)->FirstChildElement(#argname) && (xmlnode)->FirstChildElement(#argname)->ToElement());\
  109. if ((xmlnode)->FirstChildElement(#argname) && (xmlnode)->FirstChildElement(#argname)->ToElement())\
  110. (targetdata)->argname= (float)atof(xmlnode->FirstChildElement(#argname)->ToElement()->GetText());
  111. #define SET_POINTER_VALUE(xmlnode, targetdata, argname, pointertype) \
  112. {\
  113. XMLNode* node = xmlnode->FirstChildElement(#argname);\
  114. btAssert(node);\
  115. if (node)\
  116. {\
  117. const char* txt = (node)->ToElement()->GetText();\
  118. MyLocalCaster cast;\
  119. cast.m_int = (int) atof(txt);\
  120. (targetdata).argname= (pointertype)cast.m_ptr;\
  121. }\
  122. }
  123. #define SET_VECTOR4_VALUE(xmlnode, targetdata, argname) \
  124. {\
  125. XMLNode* flNode = xmlnode->FirstChildElement(#argname);\
  126. btAssert(flNode);\
  127. if (flNode && flNode->FirstChildElement())\
  128. {\
  129. const char* txt= flNode->FirstChildElement()->ToElement()->GetText();\
  130. btVector3FloatData vec4 = TextToVector3Data(txt);\
  131. (targetdata)->argname.m_floats[0] = vec4.m_floats[0];\
  132. (targetdata)->argname.m_floats[1] = vec4.m_floats[1];\
  133. (targetdata)->argname.m_floats[2] = vec4.m_floats[2];\
  134. (targetdata)->argname.m_floats[3] = vec4.m_floats[3];\
  135. }\
  136. }
  137. #define SET_MATRIX33_VALUE(n, targetdata, argname) \
  138. {\
  139. XMLNode* xmlnode = n->FirstChildElement(#argname);\
  140. btAssert(xmlnode);\
  141. if (xmlnode)\
  142. {\
  143. XMLNode* eleNode = xmlnode->FirstChildElement("m_el");\
  144. btAssert(eleNode);\
  145. if (eleNode&& eleNode->FirstChildElement())\
  146. {\
  147. const char* txt= eleNode->FirstChildElement()->ToElement()->GetText();\
  148. btVector3FloatData vec4 = TextToVector3Data(txt);\
  149. (targetdata)->argname.m_el[0].m_floats[0] = vec4.m_floats[0];\
  150. (targetdata)->argname.m_el[0].m_floats[1] = vec4.m_floats[1];\
  151. (targetdata)->argname.m_el[0].m_floats[2] = vec4.m_floats[2];\
  152. (targetdata)->argname.m_el[0].m_floats[3] = vec4.m_floats[3];\
  153. \
  154. XMLNode* n1 = eleNode->FirstChildElement()->NextSibling();\
  155. btAssert(n1);\
  156. if (n1)\
  157. {\
  158. const char* txt= n1->ToElement()->GetText();\
  159. btVector3FloatData vec4 = TextToVector3Data(txt);\
  160. (targetdata)->argname.m_el[1].m_floats[0] = vec4.m_floats[0];\
  161. (targetdata)->argname.m_el[1].m_floats[1] = vec4.m_floats[1];\
  162. (targetdata)->argname.m_el[1].m_floats[2] = vec4.m_floats[2];\
  163. (targetdata)->argname.m_el[1].m_floats[3] = vec4.m_floats[3];\
  164. \
  165. XMLNode* n2 = n1->NextSibling();\
  166. btAssert(n2);\
  167. if (n2)\
  168. {\
  169. const char* txt= n2->ToElement()->GetText();\
  170. btVector3FloatData vec4 = TextToVector3Data(txt);\
  171. (targetdata)->argname.m_el[2].m_floats[0] = vec4.m_floats[0];\
  172. (targetdata)->argname.m_el[2].m_floats[1] = vec4.m_floats[1];\
  173. (targetdata)->argname.m_el[2].m_floats[2] = vec4.m_floats[2];\
  174. (targetdata)->argname.m_el[2].m_floats[3] = vec4.m_floats[3];\
  175. }\
  176. }\
  177. }\
  178. }\
  179. }\
  180. #define SET_TRANSFORM_VALUE(n, targetdata, argname) \
  181. {\
  182. XMLNode* trNode = n->FirstChildElement(#argname);\
  183. btAssert(trNode);\
  184. if (trNode)\
  185. {\
  186. SET_VECTOR4_VALUE(trNode,&(targetdata)->argname,m_origin)\
  187. SET_MATRIX33_VALUE(trNode, &(targetdata)->argname,m_basis)\
  188. }\
  189. }\
  190. void btBulletXmlWorldImporter::deSerializeCollisionShapeData(XMLNode* pParent, btCollisionShapeData* colShapeData)
  191. {
  192. SET_INT_VALUE(pParent,colShapeData,m_shapeType)
  193. colShapeData->m_name = 0;
  194. }
  195. void btBulletXmlWorldImporter::deSerializeConvexHullShapeData(XMLNode* pParent)
  196. {
  197. MyLocalCaster cast;
  198. get_int_attribute_by_name(pParent->ToElement(),"pointer",&cast.m_int);
  199. btConvexHullShapeData* convexHullData = (btConvexHullShapeData*)btAlignedAlloc(sizeof(btConvexHullShapeData), 16);
  200. XMLNode* xmlConvexInt = pParent->FirstChildElement("m_convexInternalShapeData");
  201. btAssert(xmlConvexInt);
  202. XMLNode* xmlColShape = xmlConvexInt ->FirstChildElement("m_collisionShapeData");
  203. btAssert(xmlColShape);
  204. deSerializeCollisionShapeData(xmlColShape,&convexHullData->m_convexInternalShapeData.m_collisionShapeData);
  205. SET_FLOAT_VALUE(xmlConvexInt,&convexHullData->m_convexInternalShapeData,m_collisionMargin)
  206. SET_VECTOR4_VALUE(xmlConvexInt,&convexHullData->m_convexInternalShapeData,m_localScaling)
  207. SET_VECTOR4_VALUE(xmlConvexInt,&convexHullData->m_convexInternalShapeData,m_implicitShapeDimensions)
  208. //convexHullData->m_unscaledPointsFloatPtr
  209. //#define SET_POINTER_VALUE(xmlnode, targetdata, argname, pointertype)
  210. {
  211. XMLNode* node = pParent->FirstChildElement("m_unscaledPointsFloatPtr");
  212. btAssert(node);
  213. if (node)
  214. {
  215. const char* txt = (node)->ToElement()->GetText();
  216. MyLocalCaster cast;
  217. cast.m_int = (int) atof(txt);
  218. (*convexHullData).m_unscaledPointsFloatPtr= (btVector3FloatData*) cast.m_ptr;
  219. }
  220. }
  221. SET_POINTER_VALUE(pParent,*convexHullData,m_unscaledPointsFloatPtr,btVector3FloatData*);
  222. SET_POINTER_VALUE(pParent,*convexHullData,m_unscaledPointsDoublePtr,btVector3DoubleData*);
  223. SET_INT_VALUE(pParent,convexHullData,m_numUnscaledPoints);
  224. m_collisionShapeData.push_back((btCollisionShapeData*)convexHullData);
  225. m_pointerLookup.insert(cast.m_ptr,convexHullData);
  226. }
  227. void btBulletXmlWorldImporter::deSerializeCompoundShapeChildData(XMLNode* pParent)
  228. {
  229. MyLocalCaster cast;
  230. get_int_attribute_by_name(pParent->ToElement(),"pointer",&cast.m_int);
  231. int numChildren = 0;
  232. btAlignedObjectArray<btCompoundShapeChildData>* compoundChildArrayPtr = new btAlignedObjectArray<btCompoundShapeChildData>;
  233. {
  234. XMLNode* transNode = pParent->FirstChildElement("m_transform");
  235. XMLNode* colShapeNode = pParent->FirstChildElement("m_childShape");
  236. XMLNode* marginNode = pParent->FirstChildElement("m_childMargin");
  237. XMLNode* childTypeNode = pParent->FirstChildElement("m_childShapeType");
  238. int i=0;
  239. while (transNode && colShapeNode && marginNode && childTypeNode)
  240. {
  241. compoundChildArrayPtr->expandNonInitializing();
  242. SET_VECTOR4_VALUE (transNode,&compoundChildArrayPtr->at(i).m_transform,m_origin)
  243. SET_MATRIX33_VALUE(transNode,&compoundChildArrayPtr->at(i).m_transform,m_basis)
  244. const char* txt = (colShapeNode)->ToElement()->GetText();
  245. MyLocalCaster cast;
  246. cast.m_int = (int) atof(txt);
  247. compoundChildArrayPtr->at(i).m_childShape = (btCollisionShapeData*) cast.m_ptr;
  248. btAssert(childTypeNode->ToElement());
  249. if (childTypeNode->ToElement())
  250. {
  251. compoundChildArrayPtr->at(i).m_childShapeType = (int)atof(childTypeNode->ToElement()->GetText());
  252. }
  253. btAssert(marginNode->ToElement());
  254. if (marginNode->ToElement())
  255. {
  256. compoundChildArrayPtr->at(i).m_childMargin = (float)atof(marginNode->ToElement()->GetText());
  257. }
  258. transNode = transNode->NextSiblingElement("m_transform");
  259. colShapeNode = colShapeNode->NextSiblingElement("m_childShape");
  260. marginNode = marginNode->NextSiblingElement("m_childMargin");
  261. childTypeNode = childTypeNode->NextSiblingElement("m_childShapeType");
  262. i++;
  263. }
  264. numChildren = i;
  265. }
  266. btAssert(numChildren);
  267. if (numChildren)
  268. {
  269. m_compoundShapeChildDataArrays.push_back(compoundChildArrayPtr);
  270. btCompoundShapeChildData* cd = &compoundChildArrayPtr->at(0);
  271. m_pointerLookup.insert(cast.m_ptr,cd);
  272. }
  273. }
  274. void btBulletXmlWorldImporter::deSerializeCompoundShapeData(XMLNode* pParent)
  275. {
  276. MyLocalCaster cast;
  277. get_int_attribute_by_name(pParent->ToElement(),"pointer",&cast.m_int);
  278. btCompoundShapeData* compoundData = (btCompoundShapeData*) btAlignedAlloc(sizeof(btCompoundShapeData),16);
  279. XMLNode* xmlColShape = pParent ->FirstChildElement("m_collisionShapeData");
  280. btAssert(xmlColShape);
  281. deSerializeCollisionShapeData(xmlColShape,&compoundData->m_collisionShapeData);
  282. SET_INT_VALUE(pParent, compoundData,m_numChildShapes);
  283. XMLNode* xmlShapeData = pParent->FirstChildElement("m_collisionShapeData");
  284. btAssert(xmlShapeData );
  285. {
  286. XMLNode* node = pParent->FirstChildElement("m_childShapePtr");\
  287. btAssert(node);
  288. while (node)
  289. {
  290. const char* txt = (node)->ToElement()->GetText();
  291. MyLocalCaster cast;
  292. cast.m_int = (int) atof(txt);
  293. compoundData->m_childShapePtr = (btCompoundShapeChildData*) cast.m_ptr;
  294. node = node->NextSiblingElement("m_childShapePtr");
  295. }
  296. //SET_POINTER_VALUE(xmlColShape, *compoundData,m_childShapePtr,btCompoundShapeChildData*);
  297. }
  298. SET_FLOAT_VALUE(pParent, compoundData,m_collisionMargin);
  299. m_collisionShapeData.push_back((btCollisionShapeData*)compoundData);
  300. m_pointerLookup.insert(cast.m_ptr,compoundData);
  301. }
  302. void btBulletXmlWorldImporter::deSerializeStaticPlaneShapeData(XMLNode* pParent)
  303. {
  304. MyLocalCaster cast;
  305. get_int_attribute_by_name(pParent->ToElement(),"pointer",&cast.m_int);
  306. btStaticPlaneShapeData* planeData = (btStaticPlaneShapeData*) btAlignedAlloc(sizeof(btStaticPlaneShapeData),16);
  307. XMLNode* xmlShapeData = pParent->FirstChildElement("m_collisionShapeData");
  308. btAssert(xmlShapeData );
  309. deSerializeCollisionShapeData(xmlShapeData,&planeData->m_collisionShapeData);
  310. SET_VECTOR4_VALUE(pParent, planeData,m_localScaling);
  311. SET_VECTOR4_VALUE(pParent, planeData,m_planeNormal);
  312. SET_FLOAT_VALUE(pParent, planeData,m_planeConstant);
  313. m_collisionShapeData.push_back((btCollisionShapeData*)planeData);
  314. m_pointerLookup.insert(cast.m_ptr,planeData);
  315. }
  316. void btBulletXmlWorldImporter::deSerializeDynamicsWorldData(XMLNode* pParent)
  317. {
  318. btContactSolverInfo solverInfo;
  319. //btVector3 gravity(0,0,0);
  320. //setDynamicsWorldInfo(gravity,solverInfo);
  321. //gravity and world info
  322. }
  323. void btBulletXmlWorldImporter::deSerializeConvexInternalShapeData(XMLNode* pParent)
  324. {
  325. MyLocalCaster cast;
  326. get_int_attribute_by_name(pParent->ToElement(),"pointer",&cast.m_int);
  327. btConvexInternalShapeData* convexShape = (btConvexInternalShapeData*) btAlignedAlloc(sizeof(btConvexInternalShapeData),16);
  328. memset(convexShape,0,sizeof(btConvexInternalShapeData));
  329. XMLNode* xmlShapeData = pParent->FirstChildElement("m_collisionShapeData");
  330. btAssert(xmlShapeData );
  331. deSerializeCollisionShapeData(xmlShapeData,&convexShape->m_collisionShapeData);
  332. SET_FLOAT_VALUE(pParent,convexShape,m_collisionMargin)
  333. SET_VECTOR4_VALUE(pParent,convexShape,m_localScaling)
  334. SET_VECTOR4_VALUE(pParent,convexShape,m_implicitShapeDimensions)
  335. m_collisionShapeData.push_back((btCollisionShapeData*)convexShape);
  336. m_pointerLookup.insert(cast.m_ptr,convexShape);
  337. }
  338. /*
  339. enum btTypedConstraintType
  340. {
  341. POINT2POINT_CONSTRAINT_TYPE=3,
  342. HINGE_CONSTRAINT_TYPE,
  343. CONETWIST_CONSTRAINT_TYPE,
  344. // D6_CONSTRAINT_TYPE,
  345. SLIDER_CONSTRAINT_TYPE,
  346. CONTACT_CONSTRAINT_TYPE,
  347. D6_SPRING_CONSTRAINT_TYPE,
  348. GEAR_CONSTRAINT_TYPE,
  349. MAX_CONSTRAINT_TYPE
  350. };
  351. */
  352. void btBulletXmlWorldImporter::deSerializeGeneric6DofConstraintData(XMLNode* pParent)
  353. {
  354. MyLocalCaster cast;
  355. get_int_attribute_by_name(pParent->ToElement(),"pointer",&cast.m_int);
  356. btGeneric6DofConstraintData2* dof6Data = (btGeneric6DofConstraintData2*)btAlignedAlloc(sizeof(btGeneric6DofConstraintData2),16);
  357. XMLNode* n = pParent->FirstChildElement("m_typeConstraintData");
  358. if (n)
  359. {
  360. SET_POINTER_VALUE(n,dof6Data->m_typeConstraintData,m_rbA,btRigidBodyData*);
  361. SET_POINTER_VALUE(n,dof6Data->m_typeConstraintData,m_rbB,btRigidBodyData*);
  362. dof6Data->m_typeConstraintData.m_name = 0;//tbd
  363. SET_INT_VALUE(n,&dof6Data->m_typeConstraintData,m_objectType);
  364. SET_INT_VALUE(n,&dof6Data->m_typeConstraintData,m_userConstraintType);
  365. SET_INT_VALUE(n,&dof6Data->m_typeConstraintData,m_userConstraintId);
  366. SET_INT_VALUE(n,&dof6Data->m_typeConstraintData,m_needsFeedback);
  367. SET_FLOAT_VALUE(n,&dof6Data->m_typeConstraintData,m_appliedImpulse);
  368. SET_FLOAT_VALUE(n,&dof6Data->m_typeConstraintData,m_dbgDrawSize);
  369. SET_INT_VALUE(n,&dof6Data->m_typeConstraintData,m_disableCollisionsBetweenLinkedBodies);
  370. SET_INT_VALUE(n,&dof6Data->m_typeConstraintData,m_overrideNumSolverIterations);
  371. SET_FLOAT_VALUE(n,&dof6Data->m_typeConstraintData,m_breakingImpulseThreshold);
  372. SET_INT_VALUE(n,&dof6Data->m_typeConstraintData,m_isEnabled);
  373. }
  374. SET_TRANSFORM_VALUE( pParent, dof6Data, m_rbAFrame);
  375. SET_TRANSFORM_VALUE( pParent, dof6Data, m_rbBFrame);
  376. SET_VECTOR4_VALUE(pParent, dof6Data, m_linearUpperLimit);
  377. SET_VECTOR4_VALUE(pParent, dof6Data, m_linearLowerLimit);
  378. SET_VECTOR4_VALUE(pParent, dof6Data, m_angularUpperLimit);
  379. SET_VECTOR4_VALUE(pParent, dof6Data, m_angularLowerLimit);
  380. SET_INT_VALUE(pParent, dof6Data,m_useLinearReferenceFrameA);
  381. SET_INT_VALUE(pParent, dof6Data,m_useOffsetForConstraintFrame);
  382. m_constraintData.push_back((btTypedConstraintData2*)dof6Data);
  383. m_pointerLookup.insert(cast.m_ptr,dof6Data);
  384. }
  385. void btBulletXmlWorldImporter::deSerializeRigidBodyFloatData(XMLNode* pParent)
  386. {
  387. MyLocalCaster cast;
  388. if (!get_int_attribute_by_name(pParent->ToElement(),"pointer",&cast.m_int))
  389. {
  390. m_fileOk = false;
  391. return;
  392. }
  393. btRigidBodyData* rbData = (btRigidBodyData*)btAlignedAlloc(sizeof(btRigidBodyData),16);
  394. XMLNode* n = pParent->FirstChildElement("m_collisionObjectData");
  395. if (n)
  396. {
  397. SET_POINTER_VALUE(n,rbData->m_collisionObjectData,m_collisionShape, void*);
  398. SET_TRANSFORM_VALUE(n,&rbData->m_collisionObjectData,m_worldTransform);
  399. SET_TRANSFORM_VALUE(n,&rbData->m_collisionObjectData,m_interpolationWorldTransform);
  400. SET_VECTOR4_VALUE(n,&rbData->m_collisionObjectData,m_interpolationLinearVelocity)
  401. SET_VECTOR4_VALUE(n,&rbData->m_collisionObjectData,m_interpolationAngularVelocity)
  402. SET_VECTOR4_VALUE(n,&rbData->m_collisionObjectData,m_anisotropicFriction)
  403. SET_FLOAT_VALUE(n,&rbData->m_collisionObjectData,m_contactProcessingThreshold);
  404. SET_FLOAT_VALUE(n,&rbData->m_collisionObjectData,m_deactivationTime);
  405. SET_FLOAT_VALUE(n,&rbData->m_collisionObjectData,m_friction);
  406. SET_FLOAT_VALUE(n,&rbData->m_collisionObjectData,m_restitution);
  407. SET_FLOAT_VALUE(n,&rbData->m_collisionObjectData,m_hitFraction);
  408. SET_FLOAT_VALUE(n,&rbData->m_collisionObjectData,m_ccdSweptSphereRadius);
  409. SET_FLOAT_VALUE(n,&rbData->m_collisionObjectData,m_ccdMotionThreshold);
  410. SET_INT_VALUE(n,&rbData->m_collisionObjectData,m_hasAnisotropicFriction);
  411. SET_INT_VALUE(n,&rbData->m_collisionObjectData,m_collisionFlags);
  412. SET_INT_VALUE(n,&rbData->m_collisionObjectData,m_islandTag1);
  413. SET_INT_VALUE(n,&rbData->m_collisionObjectData,m_companionId);
  414. SET_INT_VALUE(n,&rbData->m_collisionObjectData,m_activationState1);
  415. SET_INT_VALUE(n,&rbData->m_collisionObjectData,m_internalType);
  416. SET_INT_VALUE(n,&rbData->m_collisionObjectData,m_checkCollideWith);
  417. }
  418. // SET_VECTOR4_VALUE(pParent,rbData,m_linearVelocity);
  419. SET_MATRIX33_VALUE(pParent,rbData,m_invInertiaTensorWorld);
  420. SET_VECTOR4_VALUE(pParent,rbData,m_linearVelocity)
  421. SET_VECTOR4_VALUE(pParent,rbData,m_angularVelocity)
  422. SET_VECTOR4_VALUE(pParent,rbData,m_angularFactor)
  423. SET_VECTOR4_VALUE(pParent,rbData,m_linearFactor)
  424. SET_VECTOR4_VALUE(pParent,rbData,m_gravity)
  425. SET_VECTOR4_VALUE(pParent,rbData,m_gravity_acceleration )
  426. SET_VECTOR4_VALUE(pParent,rbData,m_invInertiaLocal)
  427. SET_VECTOR4_VALUE(pParent,rbData,m_totalTorque)
  428. SET_VECTOR4_VALUE(pParent,rbData,m_totalForce)
  429. SET_FLOAT_VALUE(pParent,rbData,m_inverseMass);
  430. SET_FLOAT_VALUE(pParent,rbData,m_linearDamping);
  431. SET_FLOAT_VALUE(pParent,rbData,m_angularDamping);
  432. SET_FLOAT_VALUE(pParent,rbData,m_additionalDampingFactor);
  433. SET_FLOAT_VALUE(pParent,rbData,m_additionalLinearDampingThresholdSqr);
  434. SET_FLOAT_VALUE(pParent,rbData,m_additionalAngularDampingThresholdSqr);
  435. SET_FLOAT_VALUE(pParent,rbData,m_additionalAngularDampingFactor);
  436. SET_FLOAT_VALUE(pParent,rbData,m_angularSleepingThreshold);
  437. SET_FLOAT_VALUE(pParent,rbData,m_linearSleepingThreshold);
  438. SET_INT_VALUE(pParent,rbData,m_additionalDamping);
  439. m_rigidBodyData.push_back(rbData);
  440. m_pointerLookup.insert(cast.m_ptr,rbData);
  441. // rbData->m_collisionObjectData.m_collisionShape = (void*) (int)atof(txt);
  442. }
  443. /*
  444. TETRAHEDRAL_SHAPE_PROXYTYPE,
  445. CONVEX_TRIANGLEMESH_SHAPE_PROXYTYPE,
  446. ,
  447. CONVEX_POINT_CLOUD_SHAPE_PROXYTYPE,
  448. CUSTOM_POLYHEDRAL_SHAPE_TYPE,
  449. //implicit convex shapes
  450. IMPLICIT_CONVEX_SHAPES_START_HERE,
  451. SPHERE_SHAPE_PROXYTYPE,
  452. MULTI_SPHERE_SHAPE_PROXYTYPE,
  453. CAPSULE_SHAPE_PROXYTYPE,
  454. CONE_SHAPE_PROXYTYPE,
  455. CONVEX_SHAPE_PROXYTYPE,
  456. CYLINDER_SHAPE_PROXYTYPE,
  457. UNIFORM_SCALING_SHAPE_PROXYTYPE,
  458. MINKOWSKI_SUM_SHAPE_PROXYTYPE,
  459. MINKOWSKI_DIFFERENCE_SHAPE_PROXYTYPE,
  460. BOX_2D_SHAPE_PROXYTYPE,
  461. CONVEX_2D_SHAPE_PROXYTYPE,
  462. CUSTOM_CONVEX_SHAPE_TYPE,
  463. //concave shapes
  464. CONCAVE_SHAPES_START_HERE,
  465. //keep all the convex shapetype below here, for the check IsConvexShape in broadphase proxy!
  466. TRIANGLE_MESH_SHAPE_PROXYTYPE,
  467. SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE,
  468. ///used for demo integration FAST/Swift collision library and Bullet
  469. FAST_CONCAVE_MESH_PROXYTYPE,
  470. //terrain
  471. TERRAIN_SHAPE_PROXYTYPE,
  472. ///Used for GIMPACT Trimesh integration
  473. GIMPACT_SHAPE_PROXYTYPE,
  474. ///Multimaterial mesh
  475. MULTIMATERIAL_TRIANGLE_MESH_PROXYTYPE,
  476. ,
  477. ,
  478. CUSTOM_CONCAVE_SHAPE_TYPE,
  479. CONCAVE_SHAPES_END_HERE,
  480. ,
  481. SOFTBODY_SHAPE_PROXYTYPE,
  482. HFFLUID_SHAPE_PROXYTYPE,
  483. HFFLUID_BUOYANT_CONVEX_SHAPE_PROXYTYPE,
  484. INVALID_SHAPE_PROXYTYPE,
  485. MAX_BROADPHASE_COLLISION_TYPES
  486. */
  487. void btBulletXmlWorldImporter::fixupConstraintData(btTypedConstraintData2* tcd)
  488. {
  489. if (tcd->m_rbA)
  490. {
  491. btRigidBodyData** ptrptr = (btRigidBodyData**)m_pointerLookup.find(tcd->m_rbA);
  492. btAssert(ptrptr);
  493. tcd->m_rbA = ptrptr? *ptrptr : 0;
  494. }
  495. if (tcd->m_rbB)
  496. {
  497. btRigidBodyData** ptrptr = (btRigidBodyData**)m_pointerLookup.find(tcd->m_rbB);
  498. btAssert(ptrptr);
  499. tcd->m_rbB = ptrptr? *ptrptr : 0;
  500. }
  501. }
  502. void btBulletXmlWorldImporter::fixupCollisionDataPointers(btCollisionShapeData* shapeData)
  503. {
  504. switch (shapeData->m_shapeType)
  505. {
  506. case COMPOUND_SHAPE_PROXYTYPE:
  507. {
  508. btCompoundShapeData* compound = (btCompoundShapeData*) shapeData;
  509. void** cdptr = m_pointerLookup.find((void*)compound->m_childShapePtr);
  510. btCompoundShapeChildData** c = (btCompoundShapeChildData**)cdptr;
  511. btAssert(c);
  512. if (c)
  513. {
  514. compound->m_childShapePtr = *c;
  515. } else
  516. {
  517. compound->m_childShapePtr = 0;
  518. }
  519. break;
  520. }
  521. case CONVEX_HULL_SHAPE_PROXYTYPE:
  522. {
  523. btConvexHullShapeData* convexData = (btConvexHullShapeData*)shapeData;
  524. btVector3FloatData** ptrptr = (btVector3FloatData**)m_pointerLookup.find((void*)convexData->m_unscaledPointsFloatPtr);
  525. btAssert(ptrptr);
  526. if (ptrptr)
  527. {
  528. convexData->m_unscaledPointsFloatPtr = *ptrptr;
  529. } else
  530. {
  531. convexData->m_unscaledPointsFloatPtr = 0;
  532. }
  533. break;
  534. }
  535. case BOX_SHAPE_PROXYTYPE:
  536. case TRIANGLE_SHAPE_PROXYTYPE:
  537. case STATIC_PLANE_PROXYTYPE:
  538. case EMPTY_SHAPE_PROXYTYPE:
  539. break;
  540. default:
  541. {
  542. btAssert(0);
  543. }
  544. }
  545. }
  546. void btBulletXmlWorldImporter::auto_serialize_root_level_children(XMLNode* pParent)
  547. {
  548. int numChildren = 0;
  549. btAssert(pParent);
  550. if (pParent)
  551. {
  552. XMLNode*pChild;
  553. for ( pChild = pParent->FirstChildElement(); pChild != 0; pChild = pChild->NextSibling(), numChildren++)
  554. {
  555. // printf("child Name=%s\n", pChild->Value());
  556. if (!strcmp(pChild->Value(),"btVector3FloatData"))
  557. {
  558. MyLocalCaster cast;
  559. get_int_attribute_by_name(pChild->ToElement(),"pointer",&cast.m_int);
  560. btAlignedObjectArray<btVector3FloatData> v;
  561. deSerializeVector3FloatData(pChild,v);
  562. int numVectors = v.size();
  563. btVector3FloatData* vectors= (btVector3FloatData*) btAlignedAlloc(sizeof(btVector3FloatData)*numVectors,16);
  564. for (int i=0;i<numVectors;i++)
  565. vectors[i] = v[i];
  566. m_floatVertexArrays.push_back(vectors);
  567. m_pointerLookup.insert(cast.m_ptr,vectors);
  568. continue;
  569. }
  570. if (!strcmp(pChild->Value(),"btGeneric6DofConstraintData"))
  571. {
  572. deSerializeGeneric6DofConstraintData(pChild);
  573. continue;
  574. }
  575. if (!strcmp(pChild->Value(),"btStaticPlaneShapeData"))
  576. {
  577. deSerializeStaticPlaneShapeData(pChild);
  578. continue;
  579. }
  580. if (!strcmp(pChild->Value(),"btCompoundShapeData"))
  581. {
  582. deSerializeCompoundShapeData(pChild);
  583. continue;
  584. }
  585. if (!strcmp(pChild->Value(),"btCompoundShapeChildData"))
  586. {
  587. deSerializeCompoundShapeChildData(pChild);
  588. continue;
  589. }
  590. if (!strcmp(pChild->Value(),"btConvexHullShapeData"))
  591. {
  592. deSerializeConvexHullShapeData(pChild);
  593. continue;
  594. }
  595. if (!strcmp(pChild->Value(),"btDynamicsWorldFloatData"))
  596. {
  597. deSerializeDynamicsWorldData(pChild);
  598. continue;
  599. }
  600. if (!strcmp(pChild->Value(),"btConvexInternalShapeData"))
  601. {
  602. deSerializeConvexInternalShapeData(pChild);
  603. continue;
  604. }
  605. if (!strcmp(pChild->Value(),"btRigidBodyFloatData"))
  606. {
  607. deSerializeRigidBodyFloatData(pChild);
  608. continue;
  609. }
  610. //printf("Error: btBulletXmlWorldImporter doesn't support %s yet\n", pChild->Value());
  611. // btAssert(0);
  612. }
  613. }
  614. ///=================================================================
  615. ///fixup pointers in various places, in the right order
  616. //fixup compoundshape child data
  617. for (int i=0;i<m_compoundShapeChildDataArrays.size();i++)
  618. {
  619. btAlignedObjectArray<btCompoundShapeChildData>* childDataArray = m_compoundShapeChildDataArrays[i];
  620. for (int c=0;c<childDataArray->size();c++)
  621. {
  622. btCompoundShapeChildData* childData = &childDataArray->at(c);
  623. btCollisionShapeData** ptrptr = (btCollisionShapeData**)m_pointerLookup[childData->m_childShape];
  624. btAssert(ptrptr);
  625. if (ptrptr)
  626. {
  627. childData->m_childShape = *ptrptr;
  628. }
  629. }
  630. }
  631. for (int i=0;i<this->m_collisionShapeData.size();i++)
  632. {
  633. btCollisionShapeData* shapeData = m_collisionShapeData[i];
  634. fixupCollisionDataPointers(shapeData);
  635. }
  636. ///now fixup pointers
  637. for (int i=0;i<m_rigidBodyData.size();i++)
  638. {
  639. btRigidBodyData* rbData = m_rigidBodyData[i];
  640. void** ptrptr = m_pointerLookup.find(rbData->m_collisionObjectData.m_collisionShape);
  641. //btAssert(ptrptr);
  642. rbData->m_collisionObjectData.m_broadphaseHandle = 0;
  643. rbData->m_collisionObjectData.m_rootCollisionShape = 0;
  644. rbData->m_collisionObjectData.m_name = 0;//tbd
  645. if (ptrptr)
  646. {
  647. rbData->m_collisionObjectData.m_collisionShape = *ptrptr;
  648. }
  649. }
  650. for (int i=0;i<m_constraintData.size();i++)
  651. {
  652. btTypedConstraintData2* tcd = m_constraintData[i];
  653. fixupConstraintData(tcd);
  654. }
  655. ///=================================================================
  656. ///convert data into Bullet data in the right order
  657. ///convert collision shapes
  658. for (int i=0;i<this->m_collisionShapeData.size();i++)
  659. {
  660. btCollisionShapeData* shapeData = m_collisionShapeData[i];
  661. btCollisionShape* shape = convertCollisionShape(shapeData);
  662. if (shape)
  663. {
  664. m_shapeMap.insert(shapeData,shape);
  665. }
  666. if (shape&& shapeData->m_name)
  667. {
  668. char* newname = duplicateName(shapeData->m_name);
  669. m_objectNameMap.insert(shape,newname);
  670. m_nameShapeMap.insert(newname,shape);
  671. }
  672. }
  673. for (int i=0;i<m_rigidBodyData.size();i++)
  674. {
  675. #ifdef BT_USE_DOUBLE_PRECISION
  676. convertRigidBodyDouble(m_rigidBodyData[i]);
  677. #else
  678. convertRigidBodyFloat(m_rigidBodyData[i]);
  679. #endif
  680. }
  681. for (int i=0;i<m_constraintData.size();i++)
  682. {
  683. btTypedConstraintData2* tcd = m_constraintData[i];
  684. // bool isDoublePrecision = false;
  685. btRigidBody* rbA = 0;
  686. btRigidBody* rbB = 0;
  687. {
  688. btCollisionObject** ptrptr = m_bodyMap.find(tcd->m_rbA);
  689. if (ptrptr)
  690. {
  691. rbA = btRigidBody::upcast(*ptrptr);
  692. }
  693. }
  694. {
  695. btCollisionObject** ptrptr = m_bodyMap.find(tcd->m_rbB);
  696. if (ptrptr)
  697. {
  698. rbB = btRigidBody::upcast(*ptrptr);
  699. }
  700. }
  701. if (rbA || rbB)
  702. {
  703. btAssert(0);//todo
  704. //convertConstraint(tcd,rbA,rbB,isDoublePrecision, m_fileVersion);
  705. }
  706. }
  707. }
  708. void btBulletXmlWorldImporter::auto_serialize(XMLNode* pParent)
  709. {
  710. // XMLElement* root = pParent->FirstChildElement("bullet_physics");
  711. if (pParent)
  712. {
  713. XMLNode*pChild;
  714. for ( pChild = pParent->FirstChildElement(); pChild != 0; pChild = pChild->NextSibling())
  715. {
  716. //if (pChild->Type()==XMLNode::TINYXML_ELEMENT)
  717. {
  718. // printf("root Name=%s\n", pChild->Value());
  719. auto_serialize_root_level_children(pChild);
  720. }
  721. }
  722. } else
  723. {
  724. printf("ERROR: no bullet_physics element\n");
  725. }
  726. }
  727. bool btBulletXmlWorldImporter::loadFile(const char* fileName)
  728. {
  729. XMLDocument doc;
  730. XMLError loadOkay = doc.LoadFile(fileName);
  731. if (loadOkay==XML_SUCCESS)
  732. {
  733. if (get_int_attribute_by_name(doc.FirstChildElement()->ToElement(),"version", &m_fileVersion))
  734. {
  735. if (m_fileVersion==281)
  736. {
  737. m_fileOk = true;
  738. int itemcount;
  739. get_int_attribute_by_name(doc.FirstChildElement()->ToElement(),"itemcount", &itemcount);
  740. auto_serialize(&doc);
  741. return m_fileOk;
  742. }
  743. }
  744. }
  745. return false;
  746. }