btMultiBody.cpp 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  1. /*
  2. * PURPOSE:
  3. * Class representing an articulated rigid body. Stores the body's
  4. * current state, allows forces and torques to be set, handles
  5. * timestepping and implements Featherstone's algorithm.
  6. *
  7. * COPYRIGHT:
  8. * Copyright (C) Stephen Thompson, <[email protected]>, 2011-2013
  9. * Portions written By Erwin Coumans: connection to LCP solver, various multibody constraints, replacing Eigen math library by Bullet LinearMath and a dedicated 6x6 matrix inverse (solveImatrix)
  10. * Portions written By Jakub Stepien: support for multi-DOF constraints, introduction of spatial algebra and several other improvements
  11. This software is provided 'as-is', without any express or implied warranty.
  12. In no event will the authors be held liable for any damages arising from the use of this software.
  13. Permission is granted to anyone to use this software for any purpose,
  14. including commercial applications, and to alter it and redistribute it freely,
  15. subject to the following restrictions:
  16. 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.
  17. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  18. 3. This notice may not be removed or altered from any source distribution.
  19. */
  20. #include "btMultiBody.h"
  21. #include "btMultiBodyLink.h"
  22. #include "btMultiBodyLinkCollider.h"
  23. #include "btMultiBodyJointFeedback.h"
  24. #include "LinearMath/btTransformUtil.h"
  25. #include "LinearMath/btSerializer.h"
  26. //#include "Bullet3Common/b3Logging.h"
  27. // #define INCLUDE_GYRO_TERM
  28. namespace
  29. {
  30. const btScalar SLEEP_EPSILON = btScalar(0.05); // this is a squared velocity (m^2 s^-2)
  31. const btScalar SLEEP_TIMEOUT = btScalar(2); // in seconds
  32. } // namespace
  33. void btMultiBody::spatialTransform(const btMatrix3x3 &rotation_matrix, // rotates vectors in 'from' frame to vectors in 'to' frame
  34. const btVector3 &displacement, // vector from origin of 'from' frame to origin of 'to' frame, in 'to' coordinates
  35. const btVector3 &top_in, // top part of input vector
  36. const btVector3 &bottom_in, // bottom part of input vector
  37. btVector3 &top_out, // top part of output vector
  38. btVector3 &bottom_out) // bottom part of output vector
  39. {
  40. top_out = rotation_matrix * top_in;
  41. bottom_out = -displacement.cross(top_out) + rotation_matrix * bottom_in;
  42. }
  43. namespace
  44. {
  45. #if 0
  46. void InverseSpatialTransform(const btMatrix3x3 &rotation_matrix,
  47. const btVector3 &displacement,
  48. const btVector3 &top_in,
  49. const btVector3 &bottom_in,
  50. btVector3 &top_out,
  51. btVector3 &bottom_out)
  52. {
  53. top_out = rotation_matrix.transpose() * top_in;
  54. bottom_out = rotation_matrix.transpose() * (bottom_in + displacement.cross(top_in));
  55. }
  56. btScalar SpatialDotProduct(const btVector3 &a_top,
  57. const btVector3 &a_bottom,
  58. const btVector3 &b_top,
  59. const btVector3 &b_bottom)
  60. {
  61. return a_bottom.dot(b_top) + a_top.dot(b_bottom);
  62. }
  63. void SpatialCrossProduct(const btVector3 &a_top,
  64. const btVector3 &a_bottom,
  65. const btVector3 &b_top,
  66. const btVector3 &b_bottom,
  67. btVector3 &top_out,
  68. btVector3 &bottom_out)
  69. {
  70. top_out = a_top.cross(b_top);
  71. bottom_out = a_bottom.cross(b_top) + a_top.cross(b_bottom);
  72. }
  73. #endif
  74. } // namespace
  75. //
  76. // Implementation of class btMultiBody
  77. //
  78. btMultiBody::btMultiBody(int n_links,
  79. btScalar mass,
  80. const btVector3 &inertia,
  81. bool fixedBase,
  82. bool canSleep,
  83. bool /*deprecatedUseMultiDof*/)
  84. : m_baseCollider(0),
  85. m_baseName(0),
  86. m_basePos(0, 0, 0),
  87. m_baseQuat(0, 0, 0, 1),
  88. m_baseMass(mass),
  89. m_baseInertia(inertia),
  90. m_fixedBase(fixedBase),
  91. m_awake(true),
  92. m_canSleep(canSleep),
  93. m_sleepTimer(0),
  94. m_userObjectPointer(0),
  95. m_userIndex2(-1),
  96. m_userIndex(-1),
  97. m_companionId(-1),
  98. m_linearDamping(0.04f),
  99. m_angularDamping(0.04f),
  100. m_useGyroTerm(true),
  101. m_maxAppliedImpulse(1000.f),
  102. m_maxCoordinateVelocity(100.f),
  103. m_hasSelfCollision(true),
  104. __posUpdated(false),
  105. m_dofCount(0),
  106. m_posVarCnt(0),
  107. m_useRK4(false),
  108. m_useGlobalVelocities(false),
  109. m_internalNeedsJointFeedback(false)
  110. {
  111. m_cachedInertiaTopLeft.setValue(0, 0, 0, 0, 0, 0, 0, 0, 0);
  112. m_cachedInertiaTopRight.setValue(0, 0, 0, 0, 0, 0, 0, 0, 0);
  113. m_cachedInertiaLowerLeft.setValue(0, 0, 0, 0, 0, 0, 0, 0, 0);
  114. m_cachedInertiaLowerRight.setValue(0, 0, 0, 0, 0, 0, 0, 0, 0);
  115. m_cachedInertiaValid = false;
  116. m_links.resize(n_links);
  117. m_matrixBuf.resize(n_links + 1);
  118. m_baseForce.setValue(0, 0, 0);
  119. m_baseTorque.setValue(0, 0, 0);
  120. clearConstraintForces();
  121. clearForcesAndTorques();
  122. }
  123. btMultiBody::~btMultiBody()
  124. {
  125. }
  126. void btMultiBody::setupFixed(int i,
  127. btScalar mass,
  128. const btVector3 &inertia,
  129. int parent,
  130. const btQuaternion &rotParentToThis,
  131. const btVector3 &parentComToThisPivotOffset,
  132. const btVector3 &thisPivotToThisComOffset, bool /*deprecatedDisableParentCollision*/)
  133. {
  134. m_links[i].m_mass = mass;
  135. m_links[i].m_inertiaLocal = inertia;
  136. m_links[i].m_parent = parent;
  137. m_links[i].setAxisTop(0, 0., 0., 0.);
  138. m_links[i].setAxisBottom(0, btVector3(0, 0, 0));
  139. m_links[i].m_zeroRotParentToThis = rotParentToThis;
  140. m_links[i].m_dVector = thisPivotToThisComOffset;
  141. m_links[i].m_eVector = parentComToThisPivotOffset;
  142. m_links[i].m_jointType = btMultibodyLink::eFixed;
  143. m_links[i].m_dofCount = 0;
  144. m_links[i].m_posVarCount = 0;
  145. m_links[i].m_flags |= BT_MULTIBODYLINKFLAGS_DISABLE_PARENT_COLLISION;
  146. m_links[i].updateCacheMultiDof();
  147. updateLinksDofOffsets();
  148. }
  149. void btMultiBody::setupPrismatic(int i,
  150. btScalar mass,
  151. const btVector3 &inertia,
  152. int parent,
  153. const btQuaternion &rotParentToThis,
  154. const btVector3 &jointAxis,
  155. const btVector3 &parentComToThisPivotOffset,
  156. const btVector3 &thisPivotToThisComOffset,
  157. bool disableParentCollision)
  158. {
  159. m_dofCount += 1;
  160. m_posVarCnt += 1;
  161. m_links[i].m_mass = mass;
  162. m_links[i].m_inertiaLocal = inertia;
  163. m_links[i].m_parent = parent;
  164. m_links[i].m_zeroRotParentToThis = rotParentToThis;
  165. m_links[i].setAxisTop(0, 0., 0., 0.);
  166. m_links[i].setAxisBottom(0, jointAxis);
  167. m_links[i].m_eVector = parentComToThisPivotOffset;
  168. m_links[i].m_dVector = thisPivotToThisComOffset;
  169. m_links[i].m_cachedRotParentToThis = rotParentToThis;
  170. m_links[i].m_jointType = btMultibodyLink::ePrismatic;
  171. m_links[i].m_dofCount = 1;
  172. m_links[i].m_posVarCount = 1;
  173. m_links[i].m_jointPos[0] = 0.f;
  174. m_links[i].m_jointTorque[0] = 0.f;
  175. if (disableParentCollision)
  176. m_links[i].m_flags |= BT_MULTIBODYLINKFLAGS_DISABLE_PARENT_COLLISION;
  177. //
  178. m_links[i].updateCacheMultiDof();
  179. updateLinksDofOffsets();
  180. }
  181. void btMultiBody::setupRevolute(int i,
  182. btScalar mass,
  183. const btVector3 &inertia,
  184. int parent,
  185. const btQuaternion &rotParentToThis,
  186. const btVector3 &jointAxis,
  187. const btVector3 &parentComToThisPivotOffset,
  188. const btVector3 &thisPivotToThisComOffset,
  189. bool disableParentCollision)
  190. {
  191. m_dofCount += 1;
  192. m_posVarCnt += 1;
  193. m_links[i].m_mass = mass;
  194. m_links[i].m_inertiaLocal = inertia;
  195. m_links[i].m_parent = parent;
  196. m_links[i].m_zeroRotParentToThis = rotParentToThis;
  197. m_links[i].setAxisTop(0, jointAxis);
  198. m_links[i].setAxisBottom(0, jointAxis.cross(thisPivotToThisComOffset));
  199. m_links[i].m_dVector = thisPivotToThisComOffset;
  200. m_links[i].m_eVector = parentComToThisPivotOffset;
  201. m_links[i].m_jointType = btMultibodyLink::eRevolute;
  202. m_links[i].m_dofCount = 1;
  203. m_links[i].m_posVarCount = 1;
  204. m_links[i].m_jointPos[0] = 0.f;
  205. m_links[i].m_jointTorque[0] = 0.f;
  206. if (disableParentCollision)
  207. m_links[i].m_flags |= BT_MULTIBODYLINKFLAGS_DISABLE_PARENT_COLLISION;
  208. //
  209. m_links[i].updateCacheMultiDof();
  210. //
  211. updateLinksDofOffsets();
  212. }
  213. void btMultiBody::setupSpherical(int i,
  214. btScalar mass,
  215. const btVector3 &inertia,
  216. int parent,
  217. const btQuaternion &rotParentToThis,
  218. const btVector3 &parentComToThisPivotOffset,
  219. const btVector3 &thisPivotToThisComOffset,
  220. bool disableParentCollision)
  221. {
  222. m_dofCount += 3;
  223. m_posVarCnt += 4;
  224. m_links[i].m_mass = mass;
  225. m_links[i].m_inertiaLocal = inertia;
  226. m_links[i].m_parent = parent;
  227. m_links[i].m_zeroRotParentToThis = rotParentToThis;
  228. m_links[i].m_dVector = thisPivotToThisComOffset;
  229. m_links[i].m_eVector = parentComToThisPivotOffset;
  230. m_links[i].m_jointType = btMultibodyLink::eSpherical;
  231. m_links[i].m_dofCount = 3;
  232. m_links[i].m_posVarCount = 4;
  233. m_links[i].setAxisTop(0, 1.f, 0.f, 0.f);
  234. m_links[i].setAxisTop(1, 0.f, 1.f, 0.f);
  235. m_links[i].setAxisTop(2, 0.f, 0.f, 1.f);
  236. m_links[i].setAxisBottom(0, m_links[i].getAxisTop(0).cross(thisPivotToThisComOffset));
  237. m_links[i].setAxisBottom(1, m_links[i].getAxisTop(1).cross(thisPivotToThisComOffset));
  238. m_links[i].setAxisBottom(2, m_links[i].getAxisTop(2).cross(thisPivotToThisComOffset));
  239. m_links[i].m_jointPos[0] = m_links[i].m_jointPos[1] = m_links[i].m_jointPos[2] = 0.f;
  240. m_links[i].m_jointPos[3] = 1.f;
  241. m_links[i].m_jointTorque[0] = m_links[i].m_jointTorque[1] = m_links[i].m_jointTorque[2] = 0.f;
  242. if (disableParentCollision)
  243. m_links[i].m_flags |= BT_MULTIBODYLINKFLAGS_DISABLE_PARENT_COLLISION;
  244. //
  245. m_links[i].updateCacheMultiDof();
  246. //
  247. updateLinksDofOffsets();
  248. }
  249. void btMultiBody::setupPlanar(int i,
  250. btScalar mass,
  251. const btVector3 &inertia,
  252. int parent,
  253. const btQuaternion &rotParentToThis,
  254. const btVector3 &rotationAxis,
  255. const btVector3 &parentComToThisComOffset,
  256. bool disableParentCollision)
  257. {
  258. m_dofCount += 3;
  259. m_posVarCnt += 3;
  260. m_links[i].m_mass = mass;
  261. m_links[i].m_inertiaLocal = inertia;
  262. m_links[i].m_parent = parent;
  263. m_links[i].m_zeroRotParentToThis = rotParentToThis;
  264. m_links[i].m_dVector.setZero();
  265. m_links[i].m_eVector = parentComToThisComOffset;
  266. //
  267. btVector3 vecNonParallelToRotAxis(1, 0, 0);
  268. if (rotationAxis.normalized().dot(vecNonParallelToRotAxis) > 0.999)
  269. vecNonParallelToRotAxis.setValue(0, 1, 0);
  270. //
  271. m_links[i].m_jointType = btMultibodyLink::ePlanar;
  272. m_links[i].m_dofCount = 3;
  273. m_links[i].m_posVarCount = 3;
  274. btVector3 n = rotationAxis.normalized();
  275. m_links[i].setAxisTop(0, n[0], n[1], n[2]);
  276. m_links[i].setAxisTop(1, 0, 0, 0);
  277. m_links[i].setAxisTop(2, 0, 0, 0);
  278. m_links[i].setAxisBottom(0, 0, 0, 0);
  279. btVector3 cr = m_links[i].getAxisTop(0).cross(vecNonParallelToRotAxis);
  280. m_links[i].setAxisBottom(1, cr[0], cr[1], cr[2]);
  281. cr = m_links[i].getAxisBottom(1).cross(m_links[i].getAxisTop(0));
  282. m_links[i].setAxisBottom(2, cr[0], cr[1], cr[2]);
  283. m_links[i].m_jointPos[0] = m_links[i].m_jointPos[1] = m_links[i].m_jointPos[2] = 0.f;
  284. m_links[i].m_jointTorque[0] = m_links[i].m_jointTorque[1] = m_links[i].m_jointTorque[2] = 0.f;
  285. if (disableParentCollision)
  286. m_links[i].m_flags |= BT_MULTIBODYLINKFLAGS_DISABLE_PARENT_COLLISION;
  287. //
  288. m_links[i].updateCacheMultiDof();
  289. //
  290. updateLinksDofOffsets();
  291. m_links[i].setAxisBottom(1, m_links[i].getAxisBottom(1).normalized());
  292. m_links[i].setAxisBottom(2, m_links[i].getAxisBottom(2).normalized());
  293. }
  294. void btMultiBody::finalizeMultiDof()
  295. {
  296. m_deltaV.resize(0);
  297. m_deltaV.resize(6 + m_dofCount);
  298. m_realBuf.resize(6 + m_dofCount + m_dofCount * m_dofCount + 6 + m_dofCount); //m_dofCount for joint-space vels + m_dofCount^2 for "D" matrices + delta-pos vector (6 base "vels" + joint "vels")
  299. m_vectorBuf.resize(2 * m_dofCount); //two 3-vectors (i.e. one six-vector) for each system dof ("h" matrices)
  300. for (int i = 0; i < m_vectorBuf.size(); i++)
  301. {
  302. m_vectorBuf[i].setValue(0, 0, 0);
  303. }
  304. updateLinksDofOffsets();
  305. }
  306. int btMultiBody::getParent(int i) const
  307. {
  308. return m_links[i].m_parent;
  309. }
  310. btScalar btMultiBody::getLinkMass(int i) const
  311. {
  312. return m_links[i].m_mass;
  313. }
  314. const btVector3 &btMultiBody::getLinkInertia(int i) const
  315. {
  316. return m_links[i].m_inertiaLocal;
  317. }
  318. btScalar btMultiBody::getJointPos(int i) const
  319. {
  320. return m_links[i].m_jointPos[0];
  321. }
  322. btScalar btMultiBody::getJointVel(int i) const
  323. {
  324. return m_realBuf[6 + m_links[i].m_dofOffset];
  325. }
  326. btScalar *btMultiBody::getJointPosMultiDof(int i)
  327. {
  328. return &m_links[i].m_jointPos[0];
  329. }
  330. btScalar *btMultiBody::getJointVelMultiDof(int i)
  331. {
  332. return &m_realBuf[6 + m_links[i].m_dofOffset];
  333. }
  334. const btScalar *btMultiBody::getJointPosMultiDof(int i) const
  335. {
  336. return &m_links[i].m_jointPos[0];
  337. }
  338. const btScalar *btMultiBody::getJointVelMultiDof(int i) const
  339. {
  340. return &m_realBuf[6 + m_links[i].m_dofOffset];
  341. }
  342. void btMultiBody::setJointPos(int i, btScalar q)
  343. {
  344. m_links[i].m_jointPos[0] = q;
  345. m_links[i].updateCacheMultiDof();
  346. }
  347. void btMultiBody::setJointPosMultiDof(int i, const double *q)
  348. {
  349. for (int pos = 0; pos < m_links[i].m_posVarCount; ++pos)
  350. m_links[i].m_jointPos[pos] = (btScalar)q[pos];
  351. m_links[i].updateCacheMultiDof();
  352. }
  353. void btMultiBody::setJointPosMultiDof(int i, const float *q)
  354. {
  355. for (int pos = 0; pos < m_links[i].m_posVarCount; ++pos)
  356. m_links[i].m_jointPos[pos] = (btScalar)q[pos];
  357. m_links[i].updateCacheMultiDof();
  358. }
  359. void btMultiBody::setJointVel(int i, btScalar qdot)
  360. {
  361. m_realBuf[6 + m_links[i].m_dofOffset] = qdot;
  362. }
  363. void btMultiBody::setJointVelMultiDof(int i, const double *qdot)
  364. {
  365. for (int dof = 0; dof < m_links[i].m_dofCount; ++dof)
  366. m_realBuf[6 + m_links[i].m_dofOffset + dof] = (btScalar)qdot[dof];
  367. }
  368. void btMultiBody::setJointVelMultiDof(int i, const float* qdot)
  369. {
  370. for (int dof = 0; dof < m_links[i].m_dofCount; ++dof)
  371. m_realBuf[6 + m_links[i].m_dofOffset + dof] = (btScalar)qdot[dof];
  372. }
  373. const btVector3 &btMultiBody::getRVector(int i) const
  374. {
  375. return m_links[i].m_cachedRVector;
  376. }
  377. const btQuaternion &btMultiBody::getParentToLocalRot(int i) const
  378. {
  379. return m_links[i].m_cachedRotParentToThis;
  380. }
  381. btVector3 btMultiBody::localPosToWorld(int i, const btVector3 &local_pos) const
  382. {
  383. btAssert(i >= -1);
  384. btAssert(i < m_links.size());
  385. if ((i < -1) || (i >= m_links.size()))
  386. {
  387. return btVector3(SIMD_INFINITY, SIMD_INFINITY, SIMD_INFINITY);
  388. }
  389. btVector3 result = local_pos;
  390. while (i != -1)
  391. {
  392. // 'result' is in frame i. transform it to frame parent(i)
  393. result += getRVector(i);
  394. result = quatRotate(getParentToLocalRot(i).inverse(), result);
  395. i = getParent(i);
  396. }
  397. // 'result' is now in the base frame. transform it to world frame
  398. result = quatRotate(getWorldToBaseRot().inverse(), result);
  399. result += getBasePos();
  400. return result;
  401. }
  402. btVector3 btMultiBody::worldPosToLocal(int i, const btVector3 &world_pos) const
  403. {
  404. btAssert(i >= -1);
  405. btAssert(i < m_links.size());
  406. if ((i < -1) || (i >= m_links.size()))
  407. {
  408. return btVector3(SIMD_INFINITY, SIMD_INFINITY, SIMD_INFINITY);
  409. }
  410. if (i == -1)
  411. {
  412. // world to base
  413. return quatRotate(getWorldToBaseRot(), (world_pos - getBasePos()));
  414. }
  415. else
  416. {
  417. // find position in parent frame, then transform to current frame
  418. return quatRotate(getParentToLocalRot(i), worldPosToLocal(getParent(i), world_pos)) - getRVector(i);
  419. }
  420. }
  421. btVector3 btMultiBody::localDirToWorld(int i, const btVector3 &local_dir) const
  422. {
  423. btAssert(i >= -1);
  424. btAssert(i < m_links.size());
  425. if ((i < -1) || (i >= m_links.size()))
  426. {
  427. return btVector3(SIMD_INFINITY, SIMD_INFINITY, SIMD_INFINITY);
  428. }
  429. btVector3 result = local_dir;
  430. while (i != -1)
  431. {
  432. result = quatRotate(getParentToLocalRot(i).inverse(), result);
  433. i = getParent(i);
  434. }
  435. result = quatRotate(getWorldToBaseRot().inverse(), result);
  436. return result;
  437. }
  438. btVector3 btMultiBody::worldDirToLocal(int i, const btVector3 &world_dir) const
  439. {
  440. btAssert(i >= -1);
  441. btAssert(i < m_links.size());
  442. if ((i < -1) || (i >= m_links.size()))
  443. {
  444. return btVector3(SIMD_INFINITY, SIMD_INFINITY, SIMD_INFINITY);
  445. }
  446. if (i == -1)
  447. {
  448. return quatRotate(getWorldToBaseRot(), world_dir);
  449. }
  450. else
  451. {
  452. return quatRotate(getParentToLocalRot(i), worldDirToLocal(getParent(i), world_dir));
  453. }
  454. }
  455. btMatrix3x3 btMultiBody::localFrameToWorld(int i, const btMatrix3x3 &local_frame) const
  456. {
  457. btMatrix3x3 result = local_frame;
  458. btVector3 frameInWorld0 = localDirToWorld(i, local_frame.getColumn(0));
  459. btVector3 frameInWorld1 = localDirToWorld(i, local_frame.getColumn(1));
  460. btVector3 frameInWorld2 = localDirToWorld(i, local_frame.getColumn(2));
  461. result.setValue(frameInWorld0[0], frameInWorld1[0], frameInWorld2[0], frameInWorld0[1], frameInWorld1[1], frameInWorld2[1], frameInWorld0[2], frameInWorld1[2], frameInWorld2[2]);
  462. return result;
  463. }
  464. void btMultiBody::compTreeLinkVelocities(btVector3 *omega, btVector3 *vel) const
  465. {
  466. int num_links = getNumLinks();
  467. // Calculates the velocities of each link (and the base) in its local frame
  468. const btQuaternion& base_rot = getWorldToBaseRot();
  469. omega[0] = quatRotate(base_rot, getBaseOmega());
  470. vel[0] = quatRotate(base_rot, getBaseVel());
  471. for (int i = 0; i < num_links; ++i)
  472. {
  473. const btMultibodyLink& link = getLink(i);
  474. const int parent = link.m_parent;
  475. // transform parent vel into this frame, store in omega[i+1], vel[i+1]
  476. spatialTransform(btMatrix3x3(link.m_cachedRotParentToThis), link.m_cachedRVector,
  477. omega[parent + 1], vel[parent + 1],
  478. omega[i + 1], vel[i + 1]);
  479. // now add qidot * shat_i
  480. const btScalar* jointVel = getJointVelMultiDof(i);
  481. for (int dof = 0; dof < link.m_dofCount; ++dof)
  482. {
  483. omega[i + 1] += jointVel[dof] * link.getAxisTop(dof);
  484. vel[i + 1] += jointVel[dof] * link.getAxisBottom(dof);
  485. }
  486. }
  487. }
  488. btScalar btMultiBody::getKineticEnergy() const
  489. {
  490. int num_links = getNumLinks();
  491. // TODO: would be better not to allocate memory here
  492. btAlignedObjectArray<btVector3> omega;
  493. omega.resize(num_links + 1);
  494. btAlignedObjectArray<btVector3> vel;
  495. vel.resize(num_links + 1);
  496. compTreeLinkVelocities(&omega[0], &vel[0]);
  497. // we will do the factor of 0.5 at the end
  498. btScalar result = m_baseMass * vel[0].dot(vel[0]);
  499. result += omega[0].dot(m_baseInertia * omega[0]);
  500. for (int i = 0; i < num_links; ++i)
  501. {
  502. result += m_links[i].m_mass * vel[i + 1].dot(vel[i + 1]);
  503. result += omega[i + 1].dot(m_links[i].m_inertiaLocal * omega[i + 1]);
  504. }
  505. return 0.5f * result;
  506. }
  507. btVector3 btMultiBody::getAngularMomentum() const
  508. {
  509. int num_links = getNumLinks();
  510. // TODO: would be better not to allocate memory here
  511. btAlignedObjectArray<btVector3> omega;
  512. omega.resize(num_links + 1);
  513. btAlignedObjectArray<btVector3> vel;
  514. vel.resize(num_links + 1);
  515. btAlignedObjectArray<btQuaternion> rot_from_world;
  516. rot_from_world.resize(num_links + 1);
  517. compTreeLinkVelocities(&omega[0], &vel[0]);
  518. rot_from_world[0] = m_baseQuat;
  519. btVector3 result = quatRotate(rot_from_world[0].inverse(), (m_baseInertia * omega[0]));
  520. for (int i = 0; i < num_links; ++i)
  521. {
  522. rot_from_world[i + 1] = m_links[i].m_cachedRotParentToThis * rot_from_world[m_links[i].m_parent + 1];
  523. result += (quatRotate(rot_from_world[i + 1].inverse(), (m_links[i].m_inertiaLocal * omega[i + 1])));
  524. }
  525. return result;
  526. }
  527. void btMultiBody::clearConstraintForces()
  528. {
  529. m_baseConstraintForce.setValue(0, 0, 0);
  530. m_baseConstraintTorque.setValue(0, 0, 0);
  531. for (int i = 0; i < getNumLinks(); ++i)
  532. {
  533. m_links[i].m_appliedConstraintForce.setValue(0, 0, 0);
  534. m_links[i].m_appliedConstraintTorque.setValue(0, 0, 0);
  535. }
  536. }
  537. void btMultiBody::clearForcesAndTorques()
  538. {
  539. m_baseForce.setValue(0, 0, 0);
  540. m_baseTorque.setValue(0, 0, 0);
  541. for (int i = 0; i < getNumLinks(); ++i)
  542. {
  543. m_links[i].m_appliedForce.setValue(0, 0, 0);
  544. m_links[i].m_appliedTorque.setValue(0, 0, 0);
  545. m_links[i].m_jointTorque[0] = m_links[i].m_jointTorque[1] = m_links[i].m_jointTorque[2] = m_links[i].m_jointTorque[3] = m_links[i].m_jointTorque[4] = m_links[i].m_jointTorque[5] = 0.f;
  546. }
  547. }
  548. void btMultiBody::clearVelocities()
  549. {
  550. for (int i = 0; i < 6 + getNumDofs(); ++i)
  551. {
  552. m_realBuf[i] = 0.f;
  553. }
  554. }
  555. void btMultiBody::addLinkForce(int i, const btVector3 &f)
  556. {
  557. m_links[i].m_appliedForce += f;
  558. }
  559. void btMultiBody::addLinkTorque(int i, const btVector3 &t)
  560. {
  561. m_links[i].m_appliedTorque += t;
  562. }
  563. void btMultiBody::addLinkConstraintForce(int i, const btVector3 &f)
  564. {
  565. m_links[i].m_appliedConstraintForce += f;
  566. }
  567. void btMultiBody::addLinkConstraintTorque(int i, const btVector3 &t)
  568. {
  569. m_links[i].m_appliedConstraintTorque += t;
  570. }
  571. void btMultiBody::addJointTorque(int i, btScalar Q)
  572. {
  573. m_links[i].m_jointTorque[0] += Q;
  574. }
  575. void btMultiBody::addJointTorqueMultiDof(int i, int dof, btScalar Q)
  576. {
  577. m_links[i].m_jointTorque[dof] += Q;
  578. }
  579. void btMultiBody::addJointTorqueMultiDof(int i, const btScalar *Q)
  580. {
  581. for (int dof = 0; dof < m_links[i].m_dofCount; ++dof)
  582. m_links[i].m_jointTorque[dof] = Q[dof];
  583. }
  584. const btVector3 &btMultiBody::getLinkForce(int i) const
  585. {
  586. return m_links[i].m_appliedForce;
  587. }
  588. const btVector3 &btMultiBody::getLinkTorque(int i) const
  589. {
  590. return m_links[i].m_appliedTorque;
  591. }
  592. btScalar btMultiBody::getJointTorque(int i) const
  593. {
  594. return m_links[i].m_jointTorque[0];
  595. }
  596. btScalar *btMultiBody::getJointTorqueMultiDof(int i)
  597. {
  598. return &m_links[i].m_jointTorque[0];
  599. }
  600. inline btMatrix3x3 outerProduct(const btVector3 &v0, const btVector3 &v1) //renamed it from vecMulVecTranspose (http://en.wikipedia.org/wiki/Outer_product); maybe it should be moved to btVector3 like dot and cross?
  601. {
  602. btVector3 row0 = btVector3(
  603. v0.x() * v1.x(),
  604. v0.x() * v1.y(),
  605. v0.x() * v1.z());
  606. btVector3 row1 = btVector3(
  607. v0.y() * v1.x(),
  608. v0.y() * v1.y(),
  609. v0.y() * v1.z());
  610. btVector3 row2 = btVector3(
  611. v0.z() * v1.x(),
  612. v0.z() * v1.y(),
  613. v0.z() * v1.z());
  614. btMatrix3x3 m(row0[0], row0[1], row0[2],
  615. row1[0], row1[1], row1[2],
  616. row2[0], row2[1], row2[2]);
  617. return m;
  618. }
  619. #define vecMulVecTranspose(v0, v1Transposed) outerProduct(v0, v1Transposed)
  620. //
  621. void btMultiBody::computeAccelerationsArticulatedBodyAlgorithmMultiDof(btScalar dt,
  622. btAlignedObjectArray<btScalar> &scratch_r,
  623. btAlignedObjectArray<btVector3> &scratch_v,
  624. btAlignedObjectArray<btMatrix3x3> &scratch_m,
  625. bool isConstraintPass,
  626. bool jointFeedbackInWorldSpace,
  627. bool jointFeedbackInJointFrame)
  628. {
  629. // Implement Featherstone's algorithm to calculate joint accelerations (q_double_dot)
  630. // and the base linear & angular accelerations.
  631. // We apply damping forces in this routine as well as any external forces specified by the
  632. // caller (via addBaseForce etc).
  633. // output should point to an array of 6 + num_links reals.
  634. // Format is: 3 angular accelerations (in world frame), 3 linear accelerations (in world frame),
  635. // num_links joint acceleration values.
  636. // We added support for multi degree of freedom (multi dof) joints.
  637. // In addition we also can compute the joint reaction forces. This is performed in a second pass,
  638. // so that we can include the effect of the constraint solver forces (computed in the PGS LCP solver)
  639. m_internalNeedsJointFeedback = false;
  640. int num_links = getNumLinks();
  641. const btScalar DAMPING_K1_LINEAR = m_linearDamping;
  642. const btScalar DAMPING_K2_LINEAR = m_linearDamping;
  643. const btScalar DAMPING_K1_ANGULAR = m_angularDamping;
  644. const btScalar DAMPING_K2_ANGULAR = m_angularDamping;
  645. const btVector3 base_vel = getBaseVel();
  646. const btVector3 base_omega = getBaseOmega();
  647. // Temporary matrices/vectors -- use scratch space from caller
  648. // so that we don't have to keep reallocating every frame
  649. scratch_r.resize(2 * m_dofCount + 7); //multidof? ("Y"s use it and it is used to store qdd) => 2 x m_dofCount
  650. scratch_v.resize(8 * num_links + 6);
  651. scratch_m.resize(4 * num_links + 4);
  652. //btScalar * r_ptr = &scratch_r[0];
  653. btScalar *output = &scratch_r[m_dofCount]; // "output" holds the q_double_dot results
  654. btVector3 *v_ptr = &scratch_v[0];
  655. // vhat_i (top = angular, bottom = linear part)
  656. btSpatialMotionVector *spatVel = (btSpatialMotionVector *)v_ptr;
  657. v_ptr += num_links * 2 + 2;
  658. //
  659. // zhat_i^A
  660. btSpatialForceVector *zeroAccSpatFrc = (btSpatialForceVector *)v_ptr;
  661. v_ptr += num_links * 2 + 2;
  662. //
  663. // chat_i (note NOT defined for the base)
  664. btSpatialMotionVector *spatCoriolisAcc = (btSpatialMotionVector *)v_ptr;
  665. v_ptr += num_links * 2;
  666. //
  667. // Ihat_i^A.
  668. btSymmetricSpatialDyad *spatInertia = (btSymmetricSpatialDyad *)&scratch_m[num_links + 1];
  669. // Cached 3x3 rotation matrices from parent frame to this frame.
  670. btMatrix3x3 *rot_from_parent = &m_matrixBuf[0];
  671. btMatrix3x3 *rot_from_world = &scratch_m[0];
  672. // hhat_i, ahat_i
  673. // hhat is NOT stored for the base (but ahat is)
  674. btSpatialForceVector *h = (btSpatialForceVector *)(m_dofCount > 0 ? &m_vectorBuf[0] : 0);
  675. btSpatialMotionVector *spatAcc = (btSpatialMotionVector *)v_ptr;
  676. v_ptr += num_links * 2 + 2;
  677. //
  678. // Y_i, invD_i
  679. btScalar *invD = m_dofCount > 0 ? &m_realBuf[6 + m_dofCount] : 0;
  680. btScalar *Y = &scratch_r[0];
  681. //
  682. //aux variables
  683. btSpatialMotionVector spatJointVel; //spatial velocity due to the joint motion (i.e. without predecessors' influence)
  684. btScalar D[36]; //"D" matrix; it's dofxdof for each body so asingle 6x6 D matrix will do
  685. btScalar invD_times_Y[6]; //D^{-1} * Y [dofxdof x dofx1 = dofx1] <=> D^{-1} * u; better moved to buffers since it is recalced in calcAccelerationDeltasMultiDof; num_dof of btScalar would cover all bodies
  686. btSpatialMotionVector result; //holds results of the SolveImatrix op; it is a spatial motion vector (accel)
  687. btScalar Y_minus_hT_a[6]; //Y - h^{T} * a; it's dofx1 for each body so a single 6x1 temp is enough
  688. btSpatialForceVector spatForceVecTemps[6]; //6 temporary spatial force vectors
  689. btSpatialTransformationMatrix fromParent; //spatial transform from parent to child
  690. btSymmetricSpatialDyad dyadTemp; //inertia matrix temp
  691. btSpatialTransformationMatrix fromWorld;
  692. fromWorld.m_trnVec.setZero();
  693. /////////////////
  694. // ptr to the joint accel part of the output
  695. btScalar *joint_accel = output + 6;
  696. // Start of the algorithm proper.
  697. // First 'upward' loop.
  698. // Combines CompTreeLinkVelocities and InitTreeLinks from Mirtich.
  699. rot_from_parent[0] = btMatrix3x3(m_baseQuat); //m_baseQuat assumed to be alias!?
  700. //create the vector of spatial velocity of the base by transforming global-coor linear and angular velocities into base-local coordinates
  701. spatVel[0].setVector(rot_from_parent[0] * base_omega, rot_from_parent[0] * base_vel);
  702. if (m_fixedBase)
  703. {
  704. zeroAccSpatFrc[0].setZero();
  705. }
  706. else
  707. {
  708. const btVector3 &baseForce = isConstraintPass ? m_baseConstraintForce : m_baseForce;
  709. const btVector3 &baseTorque = isConstraintPass ? m_baseConstraintTorque : m_baseTorque;
  710. //external forces
  711. zeroAccSpatFrc[0].setVector(-(rot_from_parent[0] * baseTorque), -(rot_from_parent[0] * baseForce));
  712. //adding damping terms (only)
  713. const btScalar linDampMult = 1., angDampMult = 1.;
  714. zeroAccSpatFrc[0].addVector(angDampMult * m_baseInertia * spatVel[0].getAngular() * (DAMPING_K1_ANGULAR + DAMPING_K2_ANGULAR * spatVel[0].getAngular().safeNorm()),
  715. linDampMult * m_baseMass * spatVel[0].getLinear() * (DAMPING_K1_LINEAR + DAMPING_K2_LINEAR * spatVel[0].getLinear().safeNorm()));
  716. //
  717. //p += vhat x Ihat vhat - done in a simpler way
  718. if (m_useGyroTerm)
  719. zeroAccSpatFrc[0].addAngular(spatVel[0].getAngular().cross(m_baseInertia * spatVel[0].getAngular()));
  720. //
  721. zeroAccSpatFrc[0].addLinear(m_baseMass * spatVel[0].getAngular().cross(spatVel[0].getLinear()));
  722. }
  723. //init the spatial AB inertia (it has the simple form thanks to choosing local body frames origins at their COMs)
  724. spatInertia[0].setMatrix(btMatrix3x3(0, 0, 0, 0, 0, 0, 0, 0, 0),
  725. //
  726. btMatrix3x3(m_baseMass, 0, 0,
  727. 0, m_baseMass, 0,
  728. 0, 0, m_baseMass),
  729. //
  730. btMatrix3x3(m_baseInertia[0], 0, 0,
  731. 0, m_baseInertia[1], 0,
  732. 0, 0, m_baseInertia[2]));
  733. rot_from_world[0] = rot_from_parent[0];
  734. //
  735. for (int i = 0; i < num_links; ++i)
  736. {
  737. const int parent = m_links[i].m_parent;
  738. rot_from_parent[i + 1] = btMatrix3x3(m_links[i].m_cachedRotParentToThis);
  739. rot_from_world[i + 1] = rot_from_parent[i + 1] * rot_from_world[parent + 1];
  740. fromParent.m_rotMat = rot_from_parent[i + 1];
  741. fromParent.m_trnVec = m_links[i].m_cachedRVector;
  742. fromWorld.m_rotMat = rot_from_world[i + 1];
  743. fromParent.transform(spatVel[parent + 1], spatVel[i + 1]);
  744. // now set vhat_i to its true value by doing
  745. // vhat_i += qidot * shat_i
  746. if (!m_useGlobalVelocities)
  747. {
  748. spatJointVel.setZero();
  749. for (int dof = 0; dof < m_links[i].m_dofCount; ++dof)
  750. spatJointVel += m_links[i].m_axes[dof] * getJointVelMultiDof(i)[dof];
  751. // remember vhat_i is really vhat_p(i) (but in current frame) at this point => we need to add velocity across the inboard joint
  752. spatVel[i + 1] += spatJointVel;
  753. //
  754. // vhat_i is vhat_p(i) transformed to local coors + the velocity across the i-th inboard joint
  755. //spatVel[i+1] = fromParent * spatVel[parent+1] + spatJointVel;
  756. }
  757. else
  758. {
  759. fromWorld.transformRotationOnly(m_links[i].m_absFrameTotVelocity, spatVel[i + 1]);
  760. fromWorld.transformRotationOnly(m_links[i].m_absFrameLocVelocity, spatJointVel);
  761. }
  762. // we can now calculate chat_i
  763. spatVel[i + 1].cross(spatJointVel, spatCoriolisAcc[i]);
  764. // calculate zhat_i^A
  765. //
  766. //external forces
  767. btVector3 linkAppliedForce = isConstraintPass ? m_links[i].m_appliedConstraintForce : m_links[i].m_appliedForce;
  768. btVector3 linkAppliedTorque = isConstraintPass ? m_links[i].m_appliedConstraintTorque : m_links[i].m_appliedTorque;
  769. zeroAccSpatFrc[i + 1].setVector(-(rot_from_world[i + 1] * linkAppliedTorque), -(rot_from_world[i + 1] * linkAppliedForce));
  770. #if 0
  771. {
  772. b3Printf("stepVelocitiesMultiDof zeroAccSpatFrc[%d] linear:%f,%f,%f, angular:%f,%f,%f",
  773. i+1,
  774. zeroAccSpatFrc[i+1].m_topVec[0],
  775. zeroAccSpatFrc[i+1].m_topVec[1],
  776. zeroAccSpatFrc[i+1].m_topVec[2],
  777. zeroAccSpatFrc[i+1].m_bottomVec[0],
  778. zeroAccSpatFrc[i+1].m_bottomVec[1],
  779. zeroAccSpatFrc[i+1].m_bottomVec[2]);
  780. }
  781. #endif
  782. //
  783. //adding damping terms (only)
  784. btScalar linDampMult = 1., angDampMult = 1.;
  785. zeroAccSpatFrc[i + 1].addVector(angDampMult * m_links[i].m_inertiaLocal * spatVel[i + 1].getAngular() * (DAMPING_K1_ANGULAR + DAMPING_K2_ANGULAR * spatVel[i + 1].getAngular().safeNorm()),
  786. linDampMult * m_links[i].m_mass * spatVel[i + 1].getLinear() * (DAMPING_K1_LINEAR + DAMPING_K2_LINEAR * spatVel[i + 1].getLinear().safeNorm()));
  787. // calculate Ihat_i^A
  788. //init the spatial AB inertia (it has the simple form thanks to choosing local body frames origins at their COMs)
  789. spatInertia[i + 1].setMatrix(btMatrix3x3(0, 0, 0, 0, 0, 0, 0, 0, 0),
  790. //
  791. btMatrix3x3(m_links[i].m_mass, 0, 0,
  792. 0, m_links[i].m_mass, 0,
  793. 0, 0, m_links[i].m_mass),
  794. //
  795. btMatrix3x3(m_links[i].m_inertiaLocal[0], 0, 0,
  796. 0, m_links[i].m_inertiaLocal[1], 0,
  797. 0, 0, m_links[i].m_inertiaLocal[2]));
  798. //
  799. //p += vhat x Ihat vhat - done in a simpler way
  800. if (m_useGyroTerm)
  801. zeroAccSpatFrc[i + 1].addAngular(spatVel[i + 1].getAngular().cross(m_links[i].m_inertiaLocal * spatVel[i + 1].getAngular()));
  802. //
  803. zeroAccSpatFrc[i + 1].addLinear(m_links[i].m_mass * spatVel[i + 1].getAngular().cross(spatVel[i + 1].getLinear()));
  804. //btVector3 temp = m_links[i].m_mass * spatVel[i+1].getAngular().cross(spatVel[i+1].getLinear());
  805. ////clamp parent's omega
  806. //btScalar parOmegaMod = temp.length();
  807. //btScalar parOmegaModMax = 1000;
  808. //if(parOmegaMod > parOmegaModMax)
  809. // temp *= parOmegaModMax / parOmegaMod;
  810. //zeroAccSpatFrc[i+1].addLinear(temp);
  811. //printf("|zeroAccSpatFrc[%d]| = %.4f\n", i+1, temp.length());
  812. //temp = spatCoriolisAcc[i].getLinear();
  813. //printf("|spatCoriolisAcc[%d]| = %.4f\n", i+1, temp.length());
  814. //printf("w[%d] = [%.4f %.4f %.4f]\n", i, vel_top_angular[i+1].x(), vel_top_angular[i+1].y(), vel_top_angular[i+1].z());
  815. //printf("v[%d] = [%.4f %.4f %.4f]\n", i, vel_bottom_linear[i+1].x(), vel_bottom_linear[i+1].y(), vel_bottom_linear[i+1].z());
  816. //printf("c[%d] = [%.4f %.4f %.4f]\n", i, coriolis_bottom_linear[i].x(), coriolis_bottom_linear[i].y(), coriolis_bottom_linear[i].z());
  817. }
  818. // 'Downward' loop.
  819. // (part of TreeForwardDynamics in Mirtich.)
  820. for (int i = num_links - 1; i >= 0; --i)
  821. {
  822. const int parent = m_links[i].m_parent;
  823. fromParent.m_rotMat = rot_from_parent[i + 1];
  824. fromParent.m_trnVec = m_links[i].m_cachedRVector;
  825. for (int dof = 0; dof < m_links[i].m_dofCount; ++dof)
  826. {
  827. btSpatialForceVector &hDof = h[m_links[i].m_dofOffset + dof];
  828. //
  829. hDof = spatInertia[i + 1] * m_links[i].m_axes[dof];
  830. //
  831. Y[m_links[i].m_dofOffset + dof] = m_links[i].m_jointTorque[dof] - m_links[i].m_axes[dof].dot(zeroAccSpatFrc[i + 1]) - spatCoriolisAcc[i].dot(hDof);
  832. }
  833. for (int dof = 0; dof < m_links[i].m_dofCount; ++dof)
  834. {
  835. btScalar *D_row = &D[dof * m_links[i].m_dofCount];
  836. for (int dof2 = 0; dof2 < m_links[i].m_dofCount; ++dof2)
  837. {
  838. const btSpatialForceVector &hDof2 = h[m_links[i].m_dofOffset + dof2];
  839. D_row[dof2] = m_links[i].m_axes[dof].dot(hDof2);
  840. }
  841. }
  842. btScalar *invDi = &invD[m_links[i].m_dofOffset * m_links[i].m_dofOffset];
  843. switch (m_links[i].m_jointType)
  844. {
  845. case btMultibodyLink::ePrismatic:
  846. case btMultibodyLink::eRevolute:
  847. {
  848. if (D[0] >= SIMD_EPSILON)
  849. {
  850. invDi[0] = 1.0f / D[0];
  851. }
  852. else
  853. {
  854. invDi[0] = 0;
  855. }
  856. break;
  857. }
  858. case btMultibodyLink::eSpherical:
  859. case btMultibodyLink::ePlanar:
  860. {
  861. const btMatrix3x3 D3x3(D[0], D[1], D[2], D[3], D[4], D[5], D[6], D[7], D[8]);
  862. const btMatrix3x3 invD3x3(D3x3.inverse());
  863. //unroll the loop?
  864. for (int row = 0; row < 3; ++row)
  865. {
  866. for (int col = 0; col < 3; ++col)
  867. {
  868. invDi[row * 3 + col] = invD3x3[row][col];
  869. }
  870. }
  871. break;
  872. }
  873. default:
  874. {
  875. }
  876. }
  877. //determine h*D^{-1}
  878. for (int dof = 0; dof < m_links[i].m_dofCount; ++dof)
  879. {
  880. spatForceVecTemps[dof].setZero();
  881. for (int dof2 = 0; dof2 < m_links[i].m_dofCount; ++dof2)
  882. {
  883. const btSpatialForceVector &hDof2 = h[m_links[i].m_dofOffset + dof2];
  884. //
  885. spatForceVecTemps[dof] += hDof2 * invDi[dof2 * m_links[i].m_dofCount + dof];
  886. }
  887. }
  888. dyadTemp = spatInertia[i + 1];
  889. //determine (h*D^{-1}) * h^{T}
  890. for (int dof = 0; dof < m_links[i].m_dofCount; ++dof)
  891. {
  892. const btSpatialForceVector &hDof = h[m_links[i].m_dofOffset + dof];
  893. //
  894. dyadTemp -= symmetricSpatialOuterProduct(hDof, spatForceVecTemps[dof]);
  895. }
  896. fromParent.transformInverse(dyadTemp, spatInertia[parent + 1], btSpatialTransformationMatrix::Add);
  897. for (int dof = 0; dof < m_links[i].m_dofCount; ++dof)
  898. {
  899. invD_times_Y[dof] = 0.f;
  900. for (int dof2 = 0; dof2 < m_links[i].m_dofCount; ++dof2)
  901. {
  902. invD_times_Y[dof] += invDi[dof * m_links[i].m_dofCount + dof2] * Y[m_links[i].m_dofOffset + dof2];
  903. }
  904. }
  905. spatForceVecTemps[0] = zeroAccSpatFrc[i + 1] + spatInertia[i + 1] * spatCoriolisAcc[i];
  906. for (int dof = 0; dof < m_links[i].m_dofCount; ++dof)
  907. {
  908. const btSpatialForceVector &hDof = h[m_links[i].m_dofOffset + dof];
  909. //
  910. spatForceVecTemps[0] += hDof * invD_times_Y[dof];
  911. }
  912. fromParent.transformInverse(spatForceVecTemps[0], spatForceVecTemps[1]);
  913. zeroAccSpatFrc[parent + 1] += spatForceVecTemps[1];
  914. }
  915. // Second 'upward' loop
  916. // (part of TreeForwardDynamics in Mirtich)
  917. if (m_fixedBase)
  918. {
  919. spatAcc[0].setZero();
  920. }
  921. else
  922. {
  923. if (num_links > 0)
  924. {
  925. m_cachedInertiaValid = true;
  926. m_cachedInertiaTopLeft = spatInertia[0].m_topLeftMat;
  927. m_cachedInertiaTopRight = spatInertia[0].m_topRightMat;
  928. m_cachedInertiaLowerLeft = spatInertia[0].m_bottomLeftMat;
  929. m_cachedInertiaLowerRight = spatInertia[0].m_topLeftMat.transpose();
  930. }
  931. solveImatrix(zeroAccSpatFrc[0], result);
  932. spatAcc[0] = -result;
  933. }
  934. // now do the loop over the m_links
  935. for (int i = 0; i < num_links; ++i)
  936. {
  937. // qdd = D^{-1} * (Y - h^{T}*apar) = (S^{T}*I*S)^{-1} * (tau - S^{T}*I*cor - S^{T}*zeroAccFrc - S^{T}*I*apar)
  938. // a = apar + cor + Sqdd
  939. //or
  940. // qdd = D^{-1} * (Y - h^{T}*(apar+cor))
  941. // a = apar + Sqdd
  942. const int parent = m_links[i].m_parent;
  943. fromParent.m_rotMat = rot_from_parent[i + 1];
  944. fromParent.m_trnVec = m_links[i].m_cachedRVector;
  945. fromParent.transform(spatAcc[parent + 1], spatAcc[i + 1]);
  946. for (int dof = 0; dof < m_links[i].m_dofCount; ++dof)
  947. {
  948. const btSpatialForceVector &hDof = h[m_links[i].m_dofOffset + dof];
  949. //
  950. Y_minus_hT_a[dof] = Y[m_links[i].m_dofOffset + dof] - spatAcc[i + 1].dot(hDof);
  951. }
  952. btScalar *invDi = &invD[m_links[i].m_dofOffset * m_links[i].m_dofOffset];
  953. //D^{-1} * (Y - h^{T}*apar)
  954. mulMatrix(invDi, Y_minus_hT_a, m_links[i].m_dofCount, m_links[i].m_dofCount, m_links[i].m_dofCount, 1, &joint_accel[m_links[i].m_dofOffset]);
  955. spatAcc[i + 1] += spatCoriolisAcc[i];
  956. for (int dof = 0; dof < m_links[i].m_dofCount; ++dof)
  957. spatAcc[i + 1] += m_links[i].m_axes[dof] * joint_accel[m_links[i].m_dofOffset + dof];
  958. if (m_links[i].m_jointFeedback)
  959. {
  960. m_internalNeedsJointFeedback = true;
  961. btVector3 angularBotVec = (spatInertia[i + 1] * spatAcc[i + 1] + zeroAccSpatFrc[i + 1]).m_bottomVec;
  962. btVector3 linearTopVec = (spatInertia[i + 1] * spatAcc[i + 1] + zeroAccSpatFrc[i + 1]).m_topVec;
  963. if (jointFeedbackInJointFrame)
  964. {
  965. //shift the reaction forces to the joint frame
  966. //linear (force) component is the same
  967. //shift the angular (torque, moment) component using the relative position, m_links[i].m_dVector
  968. angularBotVec = angularBotVec - linearTopVec.cross(m_links[i].m_dVector);
  969. }
  970. if (jointFeedbackInWorldSpace)
  971. {
  972. if (isConstraintPass)
  973. {
  974. m_links[i].m_jointFeedback->m_reactionForces.m_bottomVec += m_links[i].m_cachedWorldTransform.getBasis() * angularBotVec;
  975. m_links[i].m_jointFeedback->m_reactionForces.m_topVec += m_links[i].m_cachedWorldTransform.getBasis() * linearTopVec;
  976. }
  977. else
  978. {
  979. m_links[i].m_jointFeedback->m_reactionForces.m_bottomVec = m_links[i].m_cachedWorldTransform.getBasis() * angularBotVec;
  980. m_links[i].m_jointFeedback->m_reactionForces.m_topVec = m_links[i].m_cachedWorldTransform.getBasis() * linearTopVec;
  981. }
  982. }
  983. else
  984. {
  985. if (isConstraintPass)
  986. {
  987. m_links[i].m_jointFeedback->m_reactionForces.m_bottomVec += angularBotVec;
  988. m_links[i].m_jointFeedback->m_reactionForces.m_topVec += linearTopVec;
  989. }
  990. else
  991. {
  992. m_links[i].m_jointFeedback->m_reactionForces.m_bottomVec = angularBotVec;
  993. m_links[i].m_jointFeedback->m_reactionForces.m_topVec = linearTopVec;
  994. }
  995. }
  996. }
  997. }
  998. // transform base accelerations back to the world frame.
  999. const btVector3 omegadot_out = rot_from_parent[0].transpose() * spatAcc[0].getAngular();
  1000. output[0] = omegadot_out[0];
  1001. output[1] = omegadot_out[1];
  1002. output[2] = omegadot_out[2];
  1003. const btVector3 vdot_out = rot_from_parent[0].transpose() * (spatAcc[0].getLinear() + spatVel[0].getAngular().cross(spatVel[0].getLinear()));
  1004. output[3] = vdot_out[0];
  1005. output[4] = vdot_out[1];
  1006. output[5] = vdot_out[2];
  1007. /////////////////
  1008. //printf("q = [");
  1009. //printf("%.6f, %.6f, %.6f, %.6f, %.6f, %.6f, %.6f ", m_baseQuat.x(), m_baseQuat.y(), m_baseQuat.z(), m_baseQuat.w(), m_basePos.x(), m_basePos.y(), m_basePos.z());
  1010. //for(int link = 0; link < getNumLinks(); ++link)
  1011. // for(int dof = 0; dof < m_links[link].m_dofCount; ++dof)
  1012. // printf("%.6f ", m_links[link].m_jointPos[dof]);
  1013. //printf("]\n");
  1014. ////
  1015. //printf("qd = [");
  1016. //for(int dof = 0; dof < getNumDofs() + 6; ++dof)
  1017. // printf("%.6f ", m_realBuf[dof]);
  1018. //printf("]\n");
  1019. //printf("qdd = [");
  1020. //for(int dof = 0; dof < getNumDofs() + 6; ++dof)
  1021. // printf("%.6f ", output[dof]);
  1022. //printf("]\n");
  1023. /////////////////
  1024. // Final step: add the accelerations (times dt) to the velocities.
  1025. if (!isConstraintPass)
  1026. {
  1027. if (dt > 0.)
  1028. applyDeltaVeeMultiDof(output, dt);
  1029. }
  1030. /////
  1031. //btScalar angularThres = 1;
  1032. //btScalar maxAngVel = 0.;
  1033. //bool scaleDown = 1.;
  1034. //for(int link = 0; link < m_links.size(); ++link)
  1035. //{
  1036. // if(spatVel[link+1].getAngular().length() > maxAngVel)
  1037. // {
  1038. // maxAngVel = spatVel[link+1].getAngular().length();
  1039. // scaleDown = angularThres / spatVel[link+1].getAngular().length();
  1040. // break;
  1041. // }
  1042. //}
  1043. //if(scaleDown != 1.)
  1044. //{
  1045. // for(int link = 0; link < m_links.size(); ++link)
  1046. // {
  1047. // if(m_links[link].m_jointType == btMultibodyLink::eRevolute || m_links[link].m_jointType == btMultibodyLink::eSpherical)
  1048. // {
  1049. // for(int dof = 0; dof < m_links[link].m_dofCount; ++dof)
  1050. // getJointVelMultiDof(link)[dof] *= scaleDown;
  1051. // }
  1052. // }
  1053. //}
  1054. /////
  1055. /////////////////////
  1056. if (m_useGlobalVelocities)
  1057. {
  1058. for (int i = 0; i < num_links; ++i)
  1059. {
  1060. const int parent = m_links[i].m_parent;
  1061. //rot_from_parent[i+1] = btMatrix3x3(m_links[i].m_cachedRotParentToThis); /// <- done
  1062. //rot_from_world[i+1] = rot_from_parent[i+1] * rot_from_world[parent+1]; /// <- done
  1063. fromParent.m_rotMat = rot_from_parent[i + 1];
  1064. fromParent.m_trnVec = m_links[i].m_cachedRVector;
  1065. fromWorld.m_rotMat = rot_from_world[i + 1];
  1066. // vhat_i = i_xhat_p(i) * vhat_p(i)
  1067. fromParent.transform(spatVel[parent + 1], spatVel[i + 1]);
  1068. //nice alternative below (using operator *) but it generates temps
  1069. /////////////////////////////////////////////////////////////
  1070. // now set vhat_i to its true value by doing
  1071. // vhat_i += qidot * shat_i
  1072. spatJointVel.setZero();
  1073. for (int dof = 0; dof < m_links[i].m_dofCount; ++dof)
  1074. spatJointVel += m_links[i].m_axes[dof] * getJointVelMultiDof(i)[dof];
  1075. // remember vhat_i is really vhat_p(i) (but in current frame) at this point => we need to add velocity across the inboard joint
  1076. spatVel[i + 1] += spatJointVel;
  1077. fromWorld.transformInverseRotationOnly(spatVel[i + 1], m_links[i].m_absFrameTotVelocity);
  1078. fromWorld.transformInverseRotationOnly(spatJointVel, m_links[i].m_absFrameLocVelocity);
  1079. }
  1080. }
  1081. }
  1082. void btMultiBody::solveImatrix(const btVector3 &rhs_top, const btVector3 &rhs_bot, btScalar result[6]) const
  1083. {
  1084. int num_links = getNumLinks();
  1085. ///solve I * x = rhs, so the result = invI * rhs
  1086. if (num_links == 0)
  1087. {
  1088. // in the case of 0 m_links (i.e. a plain rigid body, not a multibody) rhs * invI is easier
  1089. if ((m_baseInertia[0] >= SIMD_EPSILON) && (m_baseInertia[1] >= SIMD_EPSILON) && (m_baseInertia[2] >= SIMD_EPSILON))
  1090. {
  1091. result[0] = rhs_bot[0] / m_baseInertia[0];
  1092. result[1] = rhs_bot[1] / m_baseInertia[1];
  1093. result[2] = rhs_bot[2] / m_baseInertia[2];
  1094. }
  1095. else
  1096. {
  1097. result[0] = 0;
  1098. result[1] = 0;
  1099. result[2] = 0;
  1100. }
  1101. if (m_baseMass >= SIMD_EPSILON)
  1102. {
  1103. result[3] = rhs_top[0] / m_baseMass;
  1104. result[4] = rhs_top[1] / m_baseMass;
  1105. result[5] = rhs_top[2] / m_baseMass;
  1106. }
  1107. else
  1108. {
  1109. result[3] = 0;
  1110. result[4] = 0;
  1111. result[5] = 0;
  1112. }
  1113. }
  1114. else
  1115. {
  1116. if (!m_cachedInertiaValid)
  1117. {
  1118. for (int i = 0; i < 6; i++)
  1119. {
  1120. result[i] = 0.f;
  1121. }
  1122. return;
  1123. }
  1124. /// Special routine for calculating the inverse of a spatial inertia matrix
  1125. ///the 6x6 matrix is stored as 4 blocks of 3x3 matrices
  1126. btMatrix3x3 Binv = m_cachedInertiaTopRight.inverse() * -1.f;
  1127. btMatrix3x3 tmp = m_cachedInertiaLowerRight * Binv;
  1128. btMatrix3x3 invIupper_right = (tmp * m_cachedInertiaTopLeft + m_cachedInertiaLowerLeft).inverse();
  1129. tmp = invIupper_right * m_cachedInertiaLowerRight;
  1130. btMatrix3x3 invI_upper_left = (tmp * Binv);
  1131. btMatrix3x3 invI_lower_right = (invI_upper_left).transpose();
  1132. tmp = m_cachedInertiaTopLeft * invI_upper_left;
  1133. tmp[0][0] -= 1.0;
  1134. tmp[1][1] -= 1.0;
  1135. tmp[2][2] -= 1.0;
  1136. btMatrix3x3 invI_lower_left = (Binv * tmp);
  1137. //multiply result = invI * rhs
  1138. {
  1139. btVector3 vtop = invI_upper_left * rhs_top;
  1140. btVector3 tmp;
  1141. tmp = invIupper_right * rhs_bot;
  1142. vtop += tmp;
  1143. btVector3 vbot = invI_lower_left * rhs_top;
  1144. tmp = invI_lower_right * rhs_bot;
  1145. vbot += tmp;
  1146. result[0] = vtop[0];
  1147. result[1] = vtop[1];
  1148. result[2] = vtop[2];
  1149. result[3] = vbot[0];
  1150. result[4] = vbot[1];
  1151. result[5] = vbot[2];
  1152. }
  1153. }
  1154. }
  1155. void btMultiBody::solveImatrix(const btSpatialForceVector &rhs, btSpatialMotionVector &result) const
  1156. {
  1157. int num_links = getNumLinks();
  1158. ///solve I * x = rhs, so the result = invI * rhs
  1159. if (num_links == 0)
  1160. {
  1161. // in the case of 0 m_links (i.e. a plain rigid body, not a multibody) rhs * invI is easier
  1162. if ((m_baseInertia[0] >= SIMD_EPSILON) && (m_baseInertia[1] >= SIMD_EPSILON) && (m_baseInertia[2] >= SIMD_EPSILON))
  1163. {
  1164. result.setAngular(rhs.getAngular() / m_baseInertia);
  1165. }
  1166. else
  1167. {
  1168. result.setAngular(btVector3(0, 0, 0));
  1169. }
  1170. if (m_baseMass >= SIMD_EPSILON)
  1171. {
  1172. result.setLinear(rhs.getLinear() / m_baseMass);
  1173. }
  1174. else
  1175. {
  1176. result.setLinear(btVector3(0, 0, 0));
  1177. }
  1178. }
  1179. else
  1180. {
  1181. /// Special routine for calculating the inverse of a spatial inertia matrix
  1182. ///the 6x6 matrix is stored as 4 blocks of 3x3 matrices
  1183. if (!m_cachedInertiaValid)
  1184. {
  1185. result.setLinear(btVector3(0, 0, 0));
  1186. result.setAngular(btVector3(0, 0, 0));
  1187. result.setVector(btVector3(0, 0, 0), btVector3(0, 0, 0));
  1188. return;
  1189. }
  1190. btMatrix3x3 Binv = m_cachedInertiaTopRight.inverse() * -1.f;
  1191. btMatrix3x3 tmp = m_cachedInertiaLowerRight * Binv;
  1192. btMatrix3x3 invIupper_right = (tmp * m_cachedInertiaTopLeft + m_cachedInertiaLowerLeft).inverse();
  1193. tmp = invIupper_right * m_cachedInertiaLowerRight;
  1194. btMatrix3x3 invI_upper_left = (tmp * Binv);
  1195. btMatrix3x3 invI_lower_right = (invI_upper_left).transpose();
  1196. tmp = m_cachedInertiaTopLeft * invI_upper_left;
  1197. tmp[0][0] -= 1.0;
  1198. tmp[1][1] -= 1.0;
  1199. tmp[2][2] -= 1.0;
  1200. btMatrix3x3 invI_lower_left = (Binv * tmp);
  1201. //multiply result = invI * rhs
  1202. {
  1203. btVector3 vtop = invI_upper_left * rhs.getLinear();
  1204. btVector3 tmp;
  1205. tmp = invIupper_right * rhs.getAngular();
  1206. vtop += tmp;
  1207. btVector3 vbot = invI_lower_left * rhs.getLinear();
  1208. tmp = invI_lower_right * rhs.getAngular();
  1209. vbot += tmp;
  1210. result.setVector(vtop, vbot);
  1211. }
  1212. }
  1213. }
  1214. void btMultiBody::mulMatrix(btScalar *pA, btScalar *pB, int rowsA, int colsA, int rowsB, int colsB, btScalar *pC) const
  1215. {
  1216. for (int row = 0; row < rowsA; row++)
  1217. {
  1218. for (int col = 0; col < colsB; col++)
  1219. {
  1220. pC[row * colsB + col] = 0.f;
  1221. for (int inner = 0; inner < rowsB; inner++)
  1222. {
  1223. pC[row * colsB + col] += pA[row * colsA + inner] * pB[col + inner * colsB];
  1224. }
  1225. }
  1226. }
  1227. }
  1228. void btMultiBody::calcAccelerationDeltasMultiDof(const btScalar *force, btScalar *output,
  1229. btAlignedObjectArray<btScalar> &scratch_r, btAlignedObjectArray<btVector3> &scratch_v) const
  1230. {
  1231. // Temporary matrices/vectors -- use scratch space from caller
  1232. // so that we don't have to keep reallocating every frame
  1233. int num_links = getNumLinks();
  1234. scratch_r.resize(m_dofCount);
  1235. scratch_v.resize(4 * num_links + 4);
  1236. btScalar *r_ptr = m_dofCount ? &scratch_r[0] : 0;
  1237. btVector3 *v_ptr = &scratch_v[0];
  1238. // zhat_i^A (scratch space)
  1239. btSpatialForceVector *zeroAccSpatFrc = (btSpatialForceVector *)v_ptr;
  1240. v_ptr += num_links * 2 + 2;
  1241. // rot_from_parent (cached from calcAccelerations)
  1242. const btMatrix3x3 *rot_from_parent = &m_matrixBuf[0];
  1243. // hhat (cached), accel (scratch)
  1244. // hhat is NOT stored for the base (but ahat is)
  1245. const btSpatialForceVector *h = (btSpatialForceVector *)(m_dofCount > 0 ? &m_vectorBuf[0] : 0);
  1246. btSpatialMotionVector *spatAcc = (btSpatialMotionVector *)v_ptr;
  1247. v_ptr += num_links * 2 + 2;
  1248. // Y_i (scratch), invD_i (cached)
  1249. const btScalar *invD = m_dofCount > 0 ? &m_realBuf[6 + m_dofCount] : 0;
  1250. btScalar *Y = r_ptr;
  1251. ////////////////
  1252. //aux variables
  1253. btScalar invD_times_Y[6]; //D^{-1} * Y [dofxdof x dofx1 = dofx1] <=> D^{-1} * u; better moved to buffers since it is recalced in calcAccelerationDeltasMultiDof; num_dof of btScalar would cover all bodies
  1254. btSpatialMotionVector result; //holds results of the SolveImatrix op; it is a spatial motion vector (accel)
  1255. btScalar Y_minus_hT_a[6]; //Y - h^{T} * a; it's dofx1 for each body so a single 6x1 temp is enough
  1256. btSpatialForceVector spatForceVecTemps[6]; //6 temporary spatial force vectors
  1257. btSpatialTransformationMatrix fromParent;
  1258. /////////////////
  1259. // First 'upward' loop.
  1260. // Combines CompTreeLinkVelocities and InitTreeLinks from Mirtich.
  1261. // Fill in zero_acc
  1262. // -- set to force/torque on the base, zero otherwise
  1263. if (m_fixedBase)
  1264. {
  1265. zeroAccSpatFrc[0].setZero();
  1266. }
  1267. else
  1268. {
  1269. //test forces
  1270. fromParent.m_rotMat = rot_from_parent[0];
  1271. fromParent.transformRotationOnly(btSpatialForceVector(-force[0], -force[1], -force[2], -force[3], -force[4], -force[5]), zeroAccSpatFrc[0]);
  1272. }
  1273. for (int i = 0; i < num_links; ++i)
  1274. {
  1275. zeroAccSpatFrc[i + 1].setZero();
  1276. }
  1277. // 'Downward' loop.
  1278. // (part of TreeForwardDynamics in Mirtich.)
  1279. for (int i = num_links - 1; i >= 0; --i)
  1280. {
  1281. const int parent = m_links[i].m_parent;
  1282. fromParent.m_rotMat = rot_from_parent[i + 1];
  1283. fromParent.m_trnVec = m_links[i].m_cachedRVector;
  1284. for (int dof = 0; dof < m_links[i].m_dofCount; ++dof)
  1285. {
  1286. Y[m_links[i].m_dofOffset + dof] = force[6 + m_links[i].m_dofOffset + dof] - m_links[i].m_axes[dof].dot(zeroAccSpatFrc[i + 1]);
  1287. }
  1288. btVector3 in_top, in_bottom, out_top, out_bottom;
  1289. const btScalar *invDi = &invD[m_links[i].m_dofOffset * m_links[i].m_dofOffset];
  1290. for (int dof = 0; dof < m_links[i].m_dofCount; ++dof)
  1291. {
  1292. invD_times_Y[dof] = 0.f;
  1293. for (int dof2 = 0; dof2 < m_links[i].m_dofCount; ++dof2)
  1294. {
  1295. invD_times_Y[dof] += invDi[dof * m_links[i].m_dofCount + dof2] * Y[m_links[i].m_dofOffset + dof2];
  1296. }
  1297. }
  1298. // Zp += pXi * (Zi + hi*Yi/Di)
  1299. spatForceVecTemps[0] = zeroAccSpatFrc[i + 1];
  1300. for (int dof = 0; dof < m_links[i].m_dofCount; ++dof)
  1301. {
  1302. const btSpatialForceVector &hDof = h[m_links[i].m_dofOffset + dof];
  1303. //
  1304. spatForceVecTemps[0] += hDof * invD_times_Y[dof];
  1305. }
  1306. fromParent.transformInverse(spatForceVecTemps[0], spatForceVecTemps[1]);
  1307. zeroAccSpatFrc[parent + 1] += spatForceVecTemps[1];
  1308. }
  1309. // ptr to the joint accel part of the output
  1310. btScalar *joint_accel = output + 6;
  1311. // Second 'upward' loop
  1312. // (part of TreeForwardDynamics in Mirtich)
  1313. if (m_fixedBase)
  1314. {
  1315. spatAcc[0].setZero();
  1316. }
  1317. else
  1318. {
  1319. solveImatrix(zeroAccSpatFrc[0], result);
  1320. spatAcc[0] = -result;
  1321. }
  1322. // now do the loop over the m_links
  1323. for (int i = 0; i < num_links; ++i)
  1324. {
  1325. const int parent = m_links[i].m_parent;
  1326. fromParent.m_rotMat = rot_from_parent[i + 1];
  1327. fromParent.m_trnVec = m_links[i].m_cachedRVector;
  1328. fromParent.transform(spatAcc[parent + 1], spatAcc[i + 1]);
  1329. for (int dof = 0; dof < m_links[i].m_dofCount; ++dof)
  1330. {
  1331. const btSpatialForceVector &hDof = h[m_links[i].m_dofOffset + dof];
  1332. //
  1333. Y_minus_hT_a[dof] = Y[m_links[i].m_dofOffset + dof] - spatAcc[i + 1].dot(hDof);
  1334. }
  1335. const btScalar *invDi = &invD[m_links[i].m_dofOffset * m_links[i].m_dofOffset];
  1336. mulMatrix(const_cast<btScalar *>(invDi), Y_minus_hT_a, m_links[i].m_dofCount, m_links[i].m_dofCount, m_links[i].m_dofCount, 1, &joint_accel[m_links[i].m_dofOffset]);
  1337. for (int dof = 0; dof < m_links[i].m_dofCount; ++dof)
  1338. spatAcc[i + 1] += m_links[i].m_axes[dof] * joint_accel[m_links[i].m_dofOffset + dof];
  1339. }
  1340. // transform base accelerations back to the world frame.
  1341. btVector3 omegadot_out;
  1342. omegadot_out = rot_from_parent[0].transpose() * spatAcc[0].getAngular();
  1343. output[0] = omegadot_out[0];
  1344. output[1] = omegadot_out[1];
  1345. output[2] = omegadot_out[2];
  1346. btVector3 vdot_out;
  1347. vdot_out = rot_from_parent[0].transpose() * spatAcc[0].getLinear();
  1348. output[3] = vdot_out[0];
  1349. output[4] = vdot_out[1];
  1350. output[5] = vdot_out[2];
  1351. /////////////////
  1352. //printf("delta = [");
  1353. //for(int dof = 0; dof < getNumDofs() + 6; ++dof)
  1354. // printf("%.2f ", output[dof]);
  1355. //printf("]\n");
  1356. /////////////////
  1357. }
  1358. void btMultiBody::stepPositionsMultiDof(btScalar dt, btScalar *pq, btScalar *pqd)
  1359. {
  1360. int num_links = getNumLinks();
  1361. // step position by adding dt * velocity
  1362. //btVector3 v = getBaseVel();
  1363. //m_basePos += dt * v;
  1364. //
  1365. btScalar *pBasePos = (pq ? &pq[4] : m_basePos);
  1366. btScalar *pBaseVel = (pqd ? &pqd[3] : &m_realBuf[3]); //note: the !pqd case assumes m_realBuf holds with base velocity at 3,4,5 (should be wrapped for safety)
  1367. //
  1368. pBasePos[0] += dt * pBaseVel[0];
  1369. pBasePos[1] += dt * pBaseVel[1];
  1370. pBasePos[2] += dt * pBaseVel[2];
  1371. ///////////////////////////////
  1372. //local functor for quaternion integration (to avoid error prone redundancy)
  1373. struct
  1374. {
  1375. //"exponential map" based on btTransformUtil::integrateTransform(..)
  1376. void operator()(const btVector3 &omega, btQuaternion &quat, bool baseBody, btScalar dt)
  1377. {
  1378. //baseBody => quat is alias and omega is global coor
  1379. //!baseBody => quat is alibi and omega is local coor
  1380. btVector3 axis;
  1381. btVector3 angvel;
  1382. if (!baseBody)
  1383. angvel = quatRotate(quat, omega); //if quat is not m_baseQuat, it is alibi => ok
  1384. else
  1385. angvel = omega;
  1386. btScalar fAngle = angvel.length();
  1387. //limit the angular motion
  1388. if (fAngle * dt > ANGULAR_MOTION_THRESHOLD)
  1389. {
  1390. fAngle = btScalar(0.5) * SIMD_HALF_PI / dt;
  1391. }
  1392. if (fAngle < btScalar(0.001))
  1393. {
  1394. // use Taylor's expansions of sync function
  1395. axis = angvel * (btScalar(0.5) * dt - (dt * dt * dt) * (btScalar(0.020833333333)) * fAngle * fAngle);
  1396. }
  1397. else
  1398. {
  1399. // sync(fAngle) = sin(c*fAngle)/t
  1400. axis = angvel * (btSin(btScalar(0.5) * fAngle * dt) / fAngle);
  1401. }
  1402. if (!baseBody)
  1403. quat = btQuaternion(axis.x(), axis.y(), axis.z(), btCos(fAngle * dt * btScalar(0.5))) * quat;
  1404. else
  1405. quat = quat * btQuaternion(-axis.x(), -axis.y(), -axis.z(), btCos(fAngle * dt * btScalar(0.5)));
  1406. //equivalent to: quat = (btQuaternion(axis.x(),axis.y(),axis.z(),btCos( fAngle*dt*btScalar(0.5) )) * quat.inverse()).inverse();
  1407. quat.normalize();
  1408. }
  1409. } pQuatUpdateFun;
  1410. ///////////////////////////////
  1411. //pQuatUpdateFun(getBaseOmega(), m_baseQuat, true, dt);
  1412. //
  1413. btScalar *pBaseQuat = pq ? pq : m_baseQuat;
  1414. btScalar *pBaseOmega = pqd ? pqd : &m_realBuf[0]; //note: the !pqd case assumes m_realBuf starts with base omega (should be wrapped for safety)
  1415. //
  1416. btQuaternion baseQuat;
  1417. baseQuat.setValue(pBaseQuat[0], pBaseQuat[1], pBaseQuat[2], pBaseQuat[3]);
  1418. btVector3 baseOmega;
  1419. baseOmega.setValue(pBaseOmega[0], pBaseOmega[1], pBaseOmega[2]);
  1420. pQuatUpdateFun(baseOmega, baseQuat, true, dt);
  1421. pBaseQuat[0] = baseQuat.x();
  1422. pBaseQuat[1] = baseQuat.y();
  1423. pBaseQuat[2] = baseQuat.z();
  1424. pBaseQuat[3] = baseQuat.w();
  1425. //printf("pBaseOmega = %.4f %.4f %.4f\n", pBaseOmega->x(), pBaseOmega->y(), pBaseOmega->z());
  1426. //printf("pBaseVel = %.4f %.4f %.4f\n", pBaseVel->x(), pBaseVel->y(), pBaseVel->z());
  1427. //printf("baseQuat = %.4f %.4f %.4f %.4f\n", pBaseQuat->x(), pBaseQuat->y(), pBaseQuat->z(), pBaseQuat->w());
  1428. if (pq)
  1429. pq += 7;
  1430. if (pqd)
  1431. pqd += 6;
  1432. // Finally we can update m_jointPos for each of the m_links
  1433. for (int i = 0; i < num_links; ++i)
  1434. {
  1435. btScalar *pJointPos = (pq ? pq : &m_links[i].m_jointPos[0]);
  1436. btScalar *pJointVel = (pqd ? pqd : getJointVelMultiDof(i));
  1437. switch (m_links[i].m_jointType)
  1438. {
  1439. case btMultibodyLink::ePrismatic:
  1440. case btMultibodyLink::eRevolute:
  1441. {
  1442. btScalar jointVel = pJointVel[0];
  1443. pJointPos[0] += dt * jointVel;
  1444. break;
  1445. }
  1446. case btMultibodyLink::eSpherical:
  1447. {
  1448. btVector3 jointVel;
  1449. jointVel.setValue(pJointVel[0], pJointVel[1], pJointVel[2]);
  1450. btQuaternion jointOri;
  1451. jointOri.setValue(pJointPos[0], pJointPos[1], pJointPos[2], pJointPos[3]);
  1452. pQuatUpdateFun(jointVel, jointOri, false, dt);
  1453. pJointPos[0] = jointOri.x();
  1454. pJointPos[1] = jointOri.y();
  1455. pJointPos[2] = jointOri.z();
  1456. pJointPos[3] = jointOri.w();
  1457. break;
  1458. }
  1459. case btMultibodyLink::ePlanar:
  1460. {
  1461. pJointPos[0] += dt * getJointVelMultiDof(i)[0];
  1462. btVector3 q0_coors_qd1qd2 = getJointVelMultiDof(i)[1] * m_links[i].getAxisBottom(1) + getJointVelMultiDof(i)[2] * m_links[i].getAxisBottom(2);
  1463. btVector3 no_q0_coors_qd1qd2 = quatRotate(btQuaternion(m_links[i].getAxisTop(0), pJointPos[0]), q0_coors_qd1qd2);
  1464. pJointPos[1] += m_links[i].getAxisBottom(1).dot(no_q0_coors_qd1qd2) * dt;
  1465. pJointPos[2] += m_links[i].getAxisBottom(2).dot(no_q0_coors_qd1qd2) * dt;
  1466. break;
  1467. }
  1468. default:
  1469. {
  1470. }
  1471. }
  1472. m_links[i].updateCacheMultiDof(pq);
  1473. if (pq)
  1474. pq += m_links[i].m_posVarCount;
  1475. if (pqd)
  1476. pqd += m_links[i].m_dofCount;
  1477. }
  1478. }
  1479. void btMultiBody::fillConstraintJacobianMultiDof(int link,
  1480. const btVector3 &contact_point,
  1481. const btVector3 &normal_ang,
  1482. const btVector3 &normal_lin,
  1483. btScalar *jac,
  1484. btAlignedObjectArray<btScalar> &scratch_r1,
  1485. btAlignedObjectArray<btVector3> &scratch_v,
  1486. btAlignedObjectArray<btMatrix3x3> &scratch_m) const
  1487. {
  1488. // temporary space
  1489. int num_links = getNumLinks();
  1490. int m_dofCount = getNumDofs();
  1491. scratch_v.resize(3 * num_links + 3); //(num_links + base) offsets + (num_links + base) normals_lin + (num_links + base) normals_ang
  1492. scratch_m.resize(num_links + 1);
  1493. btVector3 *v_ptr = &scratch_v[0];
  1494. btVector3 *p_minus_com_local = v_ptr;
  1495. v_ptr += num_links + 1;
  1496. btVector3 *n_local_lin = v_ptr;
  1497. v_ptr += num_links + 1;
  1498. btVector3 *n_local_ang = v_ptr;
  1499. v_ptr += num_links + 1;
  1500. btAssert(v_ptr - &scratch_v[0] == scratch_v.size());
  1501. //scratch_r.resize(m_dofCount);
  1502. //btScalar *results = m_dofCount > 0 ? &scratch_r[0] : 0;
  1503. scratch_r1.resize(m_dofCount+num_links);
  1504. btScalar * results = m_dofCount > 0 ? &scratch_r1[0] : 0;
  1505. btScalar* links = num_links? &scratch_r1[m_dofCount] : 0;
  1506. int numLinksChildToRoot=0;
  1507. int l = link;
  1508. while (l != -1)
  1509. {
  1510. links[numLinksChildToRoot++]=l;
  1511. l = m_links[l].m_parent;
  1512. }
  1513. btMatrix3x3 *rot_from_world = &scratch_m[0];
  1514. const btVector3 p_minus_com_world = contact_point - m_basePos;
  1515. const btVector3 &normal_lin_world = normal_lin; //convenience
  1516. const btVector3 &normal_ang_world = normal_ang;
  1517. rot_from_world[0] = btMatrix3x3(m_baseQuat);
  1518. // omega coeffients first.
  1519. btVector3 omega_coeffs_world;
  1520. omega_coeffs_world = p_minus_com_world.cross(normal_lin_world);
  1521. jac[0] = omega_coeffs_world[0] + normal_ang_world[0];
  1522. jac[1] = omega_coeffs_world[1] + normal_ang_world[1];
  1523. jac[2] = omega_coeffs_world[2] + normal_ang_world[2];
  1524. // then v coefficients
  1525. jac[3] = normal_lin_world[0];
  1526. jac[4] = normal_lin_world[1];
  1527. jac[5] = normal_lin_world[2];
  1528. //create link-local versions of p_minus_com and normal
  1529. p_minus_com_local[0] = rot_from_world[0] * p_minus_com_world;
  1530. n_local_lin[0] = rot_from_world[0] * normal_lin_world;
  1531. n_local_ang[0] = rot_from_world[0] * normal_ang_world;
  1532. // Set remaining jac values to zero for now.
  1533. for (int i = 6; i < 6 + m_dofCount; ++i)
  1534. {
  1535. jac[i] = 0;
  1536. }
  1537. // Qdot coefficients, if necessary.
  1538. if (num_links > 0 && link > -1)
  1539. {
  1540. // TODO: (Also, we are making 3 separate calls to this function, for the normal & the 2 friction directions,
  1541. // which is resulting in repeated work being done...)
  1542. // calculate required normals & positions in the local frames.
  1543. for (int a = 0; a < numLinksChildToRoot; a++)
  1544. {
  1545. int i = links[numLinksChildToRoot-1-a];
  1546. // transform to local frame
  1547. const int parent = m_links[i].m_parent;
  1548. const btMatrix3x3 mtx(m_links[i].m_cachedRotParentToThis);
  1549. rot_from_world[i + 1] = mtx * rot_from_world[parent + 1];
  1550. n_local_lin[i + 1] = mtx * n_local_lin[parent + 1];
  1551. n_local_ang[i + 1] = mtx * n_local_ang[parent + 1];
  1552. p_minus_com_local[i + 1] = mtx * p_minus_com_local[parent + 1] - m_links[i].m_cachedRVector;
  1553. // calculate the jacobian entry
  1554. switch (m_links[i].m_jointType)
  1555. {
  1556. case btMultibodyLink::eRevolute:
  1557. {
  1558. results[m_links[i].m_dofOffset] = n_local_lin[i + 1].dot(m_links[i].getAxisTop(0).cross(p_minus_com_local[i + 1]) + m_links[i].getAxisBottom(0));
  1559. results[m_links[i].m_dofOffset] += n_local_ang[i + 1].dot(m_links[i].getAxisTop(0));
  1560. break;
  1561. }
  1562. case btMultibodyLink::ePrismatic:
  1563. {
  1564. results[m_links[i].m_dofOffset] = n_local_lin[i + 1].dot(m_links[i].getAxisBottom(0));
  1565. break;
  1566. }
  1567. case btMultibodyLink::eSpherical:
  1568. {
  1569. results[m_links[i].m_dofOffset + 0] = n_local_lin[i + 1].dot(m_links[i].getAxisTop(0).cross(p_minus_com_local[i + 1]) + m_links[i].getAxisBottom(0));
  1570. results[m_links[i].m_dofOffset + 1] = n_local_lin[i + 1].dot(m_links[i].getAxisTop(1).cross(p_minus_com_local[i + 1]) + m_links[i].getAxisBottom(1));
  1571. results[m_links[i].m_dofOffset + 2] = n_local_lin[i + 1].dot(m_links[i].getAxisTop(2).cross(p_minus_com_local[i + 1]) + m_links[i].getAxisBottom(2));
  1572. results[m_links[i].m_dofOffset + 0] += n_local_ang[i + 1].dot(m_links[i].getAxisTop(0));
  1573. results[m_links[i].m_dofOffset + 1] += n_local_ang[i + 1].dot(m_links[i].getAxisTop(1));
  1574. results[m_links[i].m_dofOffset + 2] += n_local_ang[i + 1].dot(m_links[i].getAxisTop(2));
  1575. break;
  1576. }
  1577. case btMultibodyLink::ePlanar:
  1578. {
  1579. results[m_links[i].m_dofOffset + 0] = n_local_lin[i + 1].dot(m_links[i].getAxisTop(0).cross(p_minus_com_local[i + 1])); // + m_links[i].getAxisBottom(0));
  1580. results[m_links[i].m_dofOffset + 1] = n_local_lin[i + 1].dot(m_links[i].getAxisBottom(1));
  1581. results[m_links[i].m_dofOffset + 2] = n_local_lin[i + 1].dot(m_links[i].getAxisBottom(2));
  1582. break;
  1583. }
  1584. default:
  1585. {
  1586. }
  1587. }
  1588. }
  1589. // Now copy through to output.
  1590. //printf("jac[%d] = ", link);
  1591. while (link != -1)
  1592. {
  1593. for (int dof = 0; dof < m_links[link].m_dofCount; ++dof)
  1594. {
  1595. jac[6 + m_links[link].m_dofOffset + dof] = results[m_links[link].m_dofOffset + dof];
  1596. //printf("%.2f\t", jac[6 + m_links[link].m_dofOffset + dof]);
  1597. }
  1598. link = m_links[link].m_parent;
  1599. }
  1600. //printf("]\n");
  1601. }
  1602. }
  1603. void btMultiBody::wakeUp()
  1604. {
  1605. m_sleepTimer = 0;
  1606. m_awake = true;
  1607. }
  1608. void btMultiBody::goToSleep()
  1609. {
  1610. m_awake = false;
  1611. }
  1612. void btMultiBody::checkMotionAndSleepIfRequired(btScalar timestep)
  1613. {
  1614. extern bool gDisableDeactivation;
  1615. if (!m_canSleep || gDisableDeactivation)
  1616. {
  1617. m_awake = true;
  1618. m_sleepTimer = 0;
  1619. return;
  1620. }
  1621. // motion is computed as omega^2 + v^2 + (sum of squares of joint velocities)
  1622. btScalar motion = 0;
  1623. {
  1624. for (int i = 0; i < 6 + m_dofCount; ++i)
  1625. motion += m_realBuf[i] * m_realBuf[i];
  1626. }
  1627. if (motion < SLEEP_EPSILON)
  1628. {
  1629. m_sleepTimer += timestep;
  1630. if (m_sleepTimer > SLEEP_TIMEOUT)
  1631. {
  1632. goToSleep();
  1633. }
  1634. }
  1635. else
  1636. {
  1637. m_sleepTimer = 0;
  1638. if (!m_awake)
  1639. wakeUp();
  1640. }
  1641. }
  1642. void btMultiBody::forwardKinematics(btAlignedObjectArray<btQuaternion> &world_to_local, btAlignedObjectArray<btVector3> &local_origin)
  1643. {
  1644. int num_links = getNumLinks();
  1645. // Cached 3x3 rotation matrices from parent frame to this frame.
  1646. btMatrix3x3 *rot_from_parent = (btMatrix3x3 *)&m_matrixBuf[0];
  1647. rot_from_parent[0] = btMatrix3x3(m_baseQuat); //m_baseQuat assumed to be alias!?
  1648. for (int i = 0; i < num_links; ++i)
  1649. {
  1650. rot_from_parent[i + 1] = btMatrix3x3(m_links[i].m_cachedRotParentToThis);
  1651. }
  1652. int nLinks = getNumLinks();
  1653. ///base + num m_links
  1654. world_to_local.resize(nLinks + 1);
  1655. local_origin.resize(nLinks + 1);
  1656. world_to_local[0] = getWorldToBaseRot();
  1657. local_origin[0] = getBasePos();
  1658. for (int k = 0; k < getNumLinks(); k++)
  1659. {
  1660. const int parent = getParent(k);
  1661. world_to_local[k + 1] = getParentToLocalRot(k) * world_to_local[parent + 1];
  1662. local_origin[k + 1] = local_origin[parent + 1] + (quatRotate(world_to_local[k + 1].inverse(), getRVector(k)));
  1663. }
  1664. for (int link = 0; link < getNumLinks(); link++)
  1665. {
  1666. int index = link + 1;
  1667. btVector3 posr = local_origin[index];
  1668. btScalar quat[4] = {-world_to_local[index].x(), -world_to_local[index].y(), -world_to_local[index].z(), world_to_local[index].w()};
  1669. btTransform tr;
  1670. tr.setIdentity();
  1671. tr.setOrigin(posr);
  1672. tr.setRotation(btQuaternion(quat[0], quat[1], quat[2], quat[3]));
  1673. getLink(link).m_cachedWorldTransform = tr;
  1674. }
  1675. }
  1676. void btMultiBody::updateCollisionObjectWorldTransforms(btAlignedObjectArray<btQuaternion> &world_to_local, btAlignedObjectArray<btVector3> &local_origin)
  1677. {
  1678. world_to_local.resize(getNumLinks() + 1);
  1679. local_origin.resize(getNumLinks() + 1);
  1680. world_to_local[0] = getWorldToBaseRot();
  1681. local_origin[0] = getBasePos();
  1682. if (getBaseCollider())
  1683. {
  1684. btVector3 posr = local_origin[0];
  1685. // float pos[4]={posr.x(),posr.y(),posr.z(),1};
  1686. btScalar quat[4] = {-world_to_local[0].x(), -world_to_local[0].y(), -world_to_local[0].z(), world_to_local[0].w()};
  1687. btTransform tr;
  1688. tr.setIdentity();
  1689. tr.setOrigin(posr);
  1690. tr.setRotation(btQuaternion(quat[0], quat[1], quat[2], quat[3]));
  1691. getBaseCollider()->setWorldTransform(tr);
  1692. }
  1693. for (int k = 0; k < getNumLinks(); k++)
  1694. {
  1695. const int parent = getParent(k);
  1696. world_to_local[k + 1] = getParentToLocalRot(k) * world_to_local[parent + 1];
  1697. local_origin[k + 1] = local_origin[parent + 1] + (quatRotate(world_to_local[k + 1].inverse(), getRVector(k)));
  1698. }
  1699. for (int m = 0; m < getNumLinks(); m++)
  1700. {
  1701. btMultiBodyLinkCollider *col = getLink(m).m_collider;
  1702. if (col)
  1703. {
  1704. int link = col->m_link;
  1705. btAssert(link == m);
  1706. int index = link + 1;
  1707. btVector3 posr = local_origin[index];
  1708. // float pos[4]={posr.x(),posr.y(),posr.z(),1};
  1709. btScalar quat[4] = {-world_to_local[index].x(), -world_to_local[index].y(), -world_to_local[index].z(), world_to_local[index].w()};
  1710. btTransform tr;
  1711. tr.setIdentity();
  1712. tr.setOrigin(posr);
  1713. tr.setRotation(btQuaternion(quat[0], quat[1], quat[2], quat[3]));
  1714. col->setWorldTransform(tr);
  1715. }
  1716. }
  1717. }
  1718. int btMultiBody::calculateSerializeBufferSize() const
  1719. {
  1720. int sz = sizeof(btMultiBodyData);
  1721. return sz;
  1722. }
  1723. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  1724. const char *btMultiBody::serialize(void *dataBuffer, class btSerializer *serializer) const
  1725. {
  1726. btMultiBodyData *mbd = (btMultiBodyData *)dataBuffer;
  1727. getBasePos().serialize(mbd->m_baseWorldPosition);
  1728. getWorldToBaseRot().inverse().serialize(mbd->m_baseWorldOrientation);
  1729. getBaseVel().serialize(mbd->m_baseLinearVelocity);
  1730. getBaseOmega().serialize(mbd->m_baseAngularVelocity);
  1731. mbd->m_baseMass = this->getBaseMass();
  1732. getBaseInertia().serialize(mbd->m_baseInertia);
  1733. {
  1734. char *name = (char *)serializer->findNameForPointer(m_baseName);
  1735. mbd->m_baseName = (char *)serializer->getUniquePointer(name);
  1736. if (mbd->m_baseName)
  1737. {
  1738. serializer->serializeName(name);
  1739. }
  1740. }
  1741. mbd->m_numLinks = this->getNumLinks();
  1742. if (mbd->m_numLinks)
  1743. {
  1744. int sz = sizeof(btMultiBodyLinkData);
  1745. int numElem = mbd->m_numLinks;
  1746. btChunk *chunk = serializer->allocate(sz, numElem);
  1747. btMultiBodyLinkData *memPtr = (btMultiBodyLinkData *)chunk->m_oldPtr;
  1748. for (int i = 0; i < numElem; i++, memPtr++)
  1749. {
  1750. memPtr->m_jointType = getLink(i).m_jointType;
  1751. memPtr->m_dofCount = getLink(i).m_dofCount;
  1752. memPtr->m_posVarCount = getLink(i).m_posVarCount;
  1753. getLink(i).m_inertiaLocal.serialize(memPtr->m_linkInertia);
  1754. getLink(i).m_absFrameTotVelocity.m_topVec.serialize(memPtr->m_absFrameTotVelocityTop);
  1755. getLink(i).m_absFrameTotVelocity.m_bottomVec.serialize(memPtr->m_absFrameTotVelocityBottom);
  1756. getLink(i).m_absFrameLocVelocity.m_topVec.serialize(memPtr->m_absFrameLocVelocityTop);
  1757. getLink(i).m_absFrameLocVelocity.m_bottomVec.serialize(memPtr->m_absFrameLocVelocityBottom);
  1758. memPtr->m_linkMass = getLink(i).m_mass;
  1759. memPtr->m_parentIndex = getLink(i).m_parent;
  1760. memPtr->m_jointDamping = getLink(i).m_jointDamping;
  1761. memPtr->m_jointFriction = getLink(i).m_jointFriction;
  1762. memPtr->m_jointLowerLimit = getLink(i).m_jointLowerLimit;
  1763. memPtr->m_jointUpperLimit = getLink(i).m_jointUpperLimit;
  1764. memPtr->m_jointMaxForce = getLink(i).m_jointMaxForce;
  1765. memPtr->m_jointMaxVelocity = getLink(i).m_jointMaxVelocity;
  1766. getLink(i).m_eVector.serialize(memPtr->m_parentComToThisPivotOffset);
  1767. getLink(i).m_dVector.serialize(memPtr->m_thisPivotToThisComOffset);
  1768. getLink(i).m_zeroRotParentToThis.serialize(memPtr->m_zeroRotParentToThis);
  1769. btAssert(memPtr->m_dofCount <= 3);
  1770. for (int dof = 0; dof < getLink(i).m_dofCount; dof++)
  1771. {
  1772. getLink(i).getAxisBottom(dof).serialize(memPtr->m_jointAxisBottom[dof]);
  1773. getLink(i).getAxisTop(dof).serialize(memPtr->m_jointAxisTop[dof]);
  1774. memPtr->m_jointTorque[dof] = getLink(i).m_jointTorque[dof];
  1775. memPtr->m_jointVel[dof] = getJointVelMultiDof(i)[dof];
  1776. }
  1777. int numPosVar = getLink(i).m_posVarCount;
  1778. for (int posvar = 0; posvar < numPosVar; posvar++)
  1779. {
  1780. memPtr->m_jointPos[posvar] = getLink(i).m_jointPos[posvar];
  1781. }
  1782. {
  1783. char *name = (char *)serializer->findNameForPointer(m_links[i].m_linkName);
  1784. memPtr->m_linkName = (char *)serializer->getUniquePointer(name);
  1785. if (memPtr->m_linkName)
  1786. {
  1787. serializer->serializeName(name);
  1788. }
  1789. }
  1790. {
  1791. char *name = (char *)serializer->findNameForPointer(m_links[i].m_jointName);
  1792. memPtr->m_jointName = (char *)serializer->getUniquePointer(name);
  1793. if (memPtr->m_jointName)
  1794. {
  1795. serializer->serializeName(name);
  1796. }
  1797. }
  1798. memPtr->m_linkCollider = (btCollisionObjectData *)serializer->getUniquePointer(getLink(i).m_collider);
  1799. }
  1800. serializer->finalizeChunk(chunk, btMultiBodyLinkDataName, BT_ARRAY_CODE, (void *)&m_links[0]);
  1801. }
  1802. mbd->m_links = mbd->m_numLinks ? (btMultiBodyLinkData *)serializer->getUniquePointer((void *)&m_links[0]) : 0;
  1803. // Fill padding with zeros to appease msan.
  1804. #ifdef BT_USE_DOUBLE_PRECISION
  1805. memset(mbd->m_padding, 0, sizeof(mbd->m_padding));
  1806. #endif
  1807. return btMultiBodyDataName;
  1808. }