btSoftBody.h 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
  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. ///btSoftBody implementation by Nathanael Presson
  14. #ifndef _BT_SOFT_BODY_H
  15. #define _BT_SOFT_BODY_H
  16. #include "LinearMath/btAlignedObjectArray.h"
  17. #include "LinearMath/btTransform.h"
  18. #include "LinearMath/btIDebugDraw.h"
  19. #include "BulletDynamics/Dynamics/btRigidBody.h"
  20. #include "BulletCollision/CollisionShapes/btConcaveShape.h"
  21. #include "BulletCollision/CollisionDispatch/btCollisionCreateFunc.h"
  22. #include "btSparseSDF.h"
  23. #include "BulletCollision/BroadphaseCollision/btDbvt.h"
  24. //#ifdef BT_USE_DOUBLE_PRECISION
  25. //#define btRigidBodyData btRigidBodyDoubleData
  26. //#define btRigidBodyDataName "btRigidBodyDoubleData"
  27. //#else
  28. #define btSoftBodyData btSoftBodyFloatData
  29. #define btSoftBodyDataName "btSoftBodyFloatData"
  30. //#endif //BT_USE_DOUBLE_PRECISION
  31. class btBroadphaseInterface;
  32. class btDispatcher;
  33. class btSoftBodySolver;
  34. /* btSoftBodyWorldInfo */
  35. struct btSoftBodyWorldInfo
  36. {
  37. btScalar air_density;
  38. btScalar water_density;
  39. btScalar water_offset;
  40. btScalar m_maxDisplacement;
  41. btVector3 water_normal;
  42. btBroadphaseInterface* m_broadphase;
  43. btDispatcher* m_dispatcher;
  44. btVector3 m_gravity;
  45. btSparseSdf<3> m_sparsesdf;
  46. btSoftBodyWorldInfo()
  47. :air_density((btScalar)1.2),
  48. water_density(0),
  49. water_offset(0),
  50. m_maxDisplacement(1000.f),//avoid soft body from 'exploding' so use some upper threshold of maximum motion that a node can travel per frame
  51. water_normal(0,0,0),
  52. m_broadphase(0),
  53. m_dispatcher(0),
  54. m_gravity(0,-10,0)
  55. {
  56. }
  57. };
  58. ///The btSoftBody is an class to simulate cloth and volumetric soft bodies.
  59. ///There is two-way interaction between btSoftBody and btRigidBody/btCollisionObject.
  60. class btSoftBody : public btCollisionObject
  61. {
  62. public:
  63. btAlignedObjectArray<const class btCollisionObject*> m_collisionDisabledObjects;
  64. // The solver object that handles this soft body
  65. btSoftBodySolver *m_softBodySolver;
  66. //
  67. // Enumerations
  68. //
  69. ///eAeroModel
  70. struct eAeroModel { enum _ {
  71. V_Point, ///Vertex normals are oriented toward velocity
  72. V_TwoSided, ///Vertex normals are flipped to match velocity
  73. V_TwoSidedLiftDrag, ///Vertex normals are flipped to match velocity and lift and drag forces are applied
  74. V_OneSided, ///Vertex normals are taken as it is
  75. F_TwoSided, ///Face normals are flipped to match velocity
  76. F_TwoSidedLiftDrag, ///Face normals are flipped to match velocity and lift and drag forces are applied
  77. F_OneSided, ///Face normals are taken as it is
  78. END
  79. };};
  80. ///eVSolver : velocities solvers
  81. struct eVSolver { enum _ {
  82. Linear, ///Linear solver
  83. END
  84. };};
  85. ///ePSolver : positions solvers
  86. struct ePSolver { enum _ {
  87. Linear, ///Linear solver
  88. Anchors, ///Anchor solver
  89. RContacts, ///Rigid contacts solver
  90. SContacts, ///Soft contacts solver
  91. END
  92. };};
  93. ///eSolverPresets
  94. struct eSolverPresets { enum _ {
  95. Positions,
  96. Velocities,
  97. Default = Positions,
  98. END
  99. };};
  100. ///eFeature
  101. struct eFeature { enum _ {
  102. None,
  103. Node,
  104. Link,
  105. Face,
  106. Tetra,
  107. END
  108. };};
  109. typedef btAlignedObjectArray<eVSolver::_> tVSolverArray;
  110. typedef btAlignedObjectArray<ePSolver::_> tPSolverArray;
  111. //
  112. // Flags
  113. //
  114. ///fCollision
  115. struct fCollision { enum _ {
  116. RVSmask = 0x000f, ///Rigid versus soft mask
  117. SDF_RS = 0x0001, ///SDF based rigid vs soft
  118. CL_RS = 0x0002, ///Cluster vs convex rigid vs soft
  119. SVSmask = 0x0030, ///Rigid versus soft mask
  120. VF_SS = 0x0010, ///Vertex vs face soft vs soft handling
  121. CL_SS = 0x0020, ///Cluster vs cluster soft vs soft handling
  122. CL_SELF = 0x0040, ///Cluster soft body self collision
  123. /* presets */
  124. Default = SDF_RS,
  125. END
  126. };};
  127. ///fMaterial
  128. struct fMaterial { enum _ {
  129. DebugDraw = 0x0001, /// Enable debug draw
  130. /* presets */
  131. Default = DebugDraw,
  132. END
  133. };};
  134. //
  135. // API Types
  136. //
  137. /* sRayCast */
  138. struct sRayCast
  139. {
  140. btSoftBody* body; /// soft body
  141. eFeature::_ feature; /// feature type
  142. int index; /// feature index
  143. btScalar fraction; /// time of impact fraction (rayorg+(rayto-rayfrom)*fraction)
  144. };
  145. /* ImplicitFn */
  146. struct ImplicitFn
  147. {
  148. virtual btScalar Eval(const btVector3& x)=0;
  149. };
  150. //
  151. // Internal types
  152. //
  153. typedef btAlignedObjectArray<btScalar> tScalarArray;
  154. typedef btAlignedObjectArray<btVector3> tVector3Array;
  155. /* sCti is Softbody contact info */
  156. struct sCti
  157. {
  158. const btCollisionObject* m_colObj; /* Rigid body */
  159. btVector3 m_normal; /* Outward normal */
  160. btScalar m_offset; /* Offset from origin */
  161. };
  162. /* sMedium */
  163. struct sMedium
  164. {
  165. btVector3 m_velocity; /* Velocity */
  166. btScalar m_pressure; /* Pressure */
  167. btScalar m_density; /* Density */
  168. };
  169. /* Base type */
  170. struct Element
  171. {
  172. void* m_tag; // User data
  173. Element() : m_tag(0) {}
  174. };
  175. /* Material */
  176. struct Material : Element
  177. {
  178. btScalar m_kLST; // Linear stiffness coefficient [0,1]
  179. btScalar m_kAST; // Area/Angular stiffness coefficient [0,1]
  180. btScalar m_kVST; // Volume stiffness coefficient [0,1]
  181. int m_flags; // Flags
  182. };
  183. /* Feature */
  184. struct Feature : Element
  185. {
  186. Material* m_material; // Material
  187. };
  188. /* Node */
  189. struct Node : Feature
  190. {
  191. btVector3 m_x; // Position
  192. btVector3 m_q; // Previous step position
  193. btVector3 m_v; // Velocity
  194. btVector3 m_f; // Force accumulator
  195. btVector3 m_n; // Normal
  196. btScalar m_im; // 1/mass
  197. btScalar m_area; // Area
  198. btDbvtNode* m_leaf; // Leaf data
  199. int m_battach:1; // Attached
  200. };
  201. /* Link */
  202. struct Link : Feature
  203. {
  204. Node* m_n[2]; // Node pointers
  205. btScalar m_rl; // Rest length
  206. int m_bbending:1; // Bending link
  207. btScalar m_c0; // (ima+imb)*kLST
  208. btScalar m_c1; // rl^2
  209. btScalar m_c2; // |gradient|^2/c0
  210. btVector3 m_c3; // gradient
  211. };
  212. /* Face */
  213. struct Face : Feature
  214. {
  215. Node* m_n[3]; // Node pointers
  216. btVector3 m_normal; // Normal
  217. btScalar m_ra; // Rest area
  218. btDbvtNode* m_leaf; // Leaf data
  219. };
  220. /* Tetra */
  221. struct Tetra : Feature
  222. {
  223. Node* m_n[4]; // Node pointers
  224. btScalar m_rv; // Rest volume
  225. btDbvtNode* m_leaf; // Leaf data
  226. btVector3 m_c0[4]; // gradients
  227. btScalar m_c1; // (4*kVST)/(im0+im1+im2+im3)
  228. btScalar m_c2; // m_c1/sum(|g0..3|^2)
  229. };
  230. /* RContact */
  231. struct RContact
  232. {
  233. sCti m_cti; // Contact infos
  234. Node* m_node; // Owner node
  235. btMatrix3x3 m_c0; // Impulse matrix
  236. btVector3 m_c1; // Relative anchor
  237. btScalar m_c2; // ima*dt
  238. btScalar m_c3; // Friction
  239. btScalar m_c4; // Hardness
  240. };
  241. /* SContact */
  242. struct SContact
  243. {
  244. Node* m_node; // Node
  245. Face* m_face; // Face
  246. btVector3 m_weights; // Weigths
  247. btVector3 m_normal; // Normal
  248. btScalar m_margin; // Margin
  249. btScalar m_friction; // Friction
  250. btScalar m_cfm[2]; // Constraint force mixing
  251. };
  252. /* Anchor */
  253. struct Anchor
  254. {
  255. Node* m_node; // Node pointer
  256. btVector3 m_local; // Anchor position in body space
  257. btRigidBody* m_body; // Body
  258. btScalar m_influence;
  259. btMatrix3x3 m_c0; // Impulse matrix
  260. btVector3 m_c1; // Relative anchor
  261. btScalar m_c2; // ima*dt
  262. };
  263. /* Note */
  264. struct Note : Element
  265. {
  266. const char* m_text; // Text
  267. btVector3 m_offset; // Offset
  268. int m_rank; // Rank
  269. Node* m_nodes[4]; // Nodes
  270. btScalar m_coords[4]; // Coordinates
  271. };
  272. /* Pose */
  273. struct Pose
  274. {
  275. bool m_bvolume; // Is valid
  276. bool m_bframe; // Is frame
  277. btScalar m_volume; // Rest volume
  278. tVector3Array m_pos; // Reference positions
  279. tScalarArray m_wgh; // Weights
  280. btVector3 m_com; // COM
  281. btMatrix3x3 m_rot; // Rotation
  282. btMatrix3x3 m_scl; // Scale
  283. btMatrix3x3 m_aqq; // Base scaling
  284. };
  285. /* Cluster */
  286. struct Cluster
  287. {
  288. tScalarArray m_masses;
  289. btAlignedObjectArray<Node*> m_nodes;
  290. tVector3Array m_framerefs;
  291. btTransform m_framexform;
  292. btScalar m_idmass;
  293. btScalar m_imass;
  294. btMatrix3x3 m_locii;
  295. btMatrix3x3 m_invwi;
  296. btVector3 m_com;
  297. btVector3 m_vimpulses[2];
  298. btVector3 m_dimpulses[2];
  299. int m_nvimpulses;
  300. int m_ndimpulses;
  301. btVector3 m_lv;
  302. btVector3 m_av;
  303. btDbvtNode* m_leaf;
  304. btScalar m_ndamping; /* Node damping */
  305. btScalar m_ldamping; /* Linear damping */
  306. btScalar m_adamping; /* Angular damping */
  307. btScalar m_matching;
  308. btScalar m_maxSelfCollisionImpulse;
  309. btScalar m_selfCollisionImpulseFactor;
  310. bool m_containsAnchor;
  311. bool m_collide;
  312. int m_clusterIndex;
  313. Cluster() : m_leaf(0),m_ndamping(0),m_ldamping(0),m_adamping(0),m_matching(0)
  314. ,m_maxSelfCollisionImpulse(100.f),
  315. m_selfCollisionImpulseFactor(0.01f),
  316. m_containsAnchor(false)
  317. {}
  318. };
  319. /* Impulse */
  320. struct Impulse
  321. {
  322. btVector3 m_velocity;
  323. btVector3 m_drift;
  324. int m_asVelocity:1;
  325. int m_asDrift:1;
  326. Impulse() : m_velocity(0,0,0),m_drift(0,0,0),m_asVelocity(0),m_asDrift(0) {}
  327. Impulse operator -() const
  328. {
  329. Impulse i=*this;
  330. i.m_velocity=-i.m_velocity;
  331. i.m_drift=-i.m_drift;
  332. return(i);
  333. }
  334. Impulse operator*(btScalar x) const
  335. {
  336. Impulse i=*this;
  337. i.m_velocity*=x;
  338. i.m_drift*=x;
  339. return(i);
  340. }
  341. };
  342. /* Body */
  343. struct Body
  344. {
  345. Cluster* m_soft;
  346. btRigidBody* m_rigid;
  347. const btCollisionObject* m_collisionObject;
  348. Body() : m_soft(0),m_rigid(0),m_collisionObject(0) {}
  349. Body(Cluster* p) : m_soft(p),m_rigid(0),m_collisionObject(0) {}
  350. Body(const btCollisionObject* colObj) : m_soft(0),m_collisionObject(colObj)
  351. {
  352. m_rigid = (btRigidBody*)btRigidBody::upcast(m_collisionObject);
  353. }
  354. void activate() const
  355. {
  356. if(m_rigid)
  357. m_rigid->activate();
  358. if (m_collisionObject)
  359. m_collisionObject->activate();
  360. }
  361. const btMatrix3x3& invWorldInertia() const
  362. {
  363. static const btMatrix3x3 iwi(0,0,0,0,0,0,0,0,0);
  364. if(m_rigid) return(m_rigid->getInvInertiaTensorWorld());
  365. if(m_soft) return(m_soft->m_invwi);
  366. return(iwi);
  367. }
  368. btScalar invMass() const
  369. {
  370. if(m_rigid) return(m_rigid->getInvMass());
  371. if(m_soft) return(m_soft->m_imass);
  372. return(0);
  373. }
  374. const btTransform& xform() const
  375. {
  376. static const btTransform identity=btTransform::getIdentity();
  377. if(m_collisionObject) return(m_collisionObject->getWorldTransform());
  378. if(m_soft) return(m_soft->m_framexform);
  379. return(identity);
  380. }
  381. btVector3 linearVelocity() const
  382. {
  383. if(m_rigid) return(m_rigid->getLinearVelocity());
  384. if(m_soft) return(m_soft->m_lv);
  385. return(btVector3(0,0,0));
  386. }
  387. btVector3 angularVelocity(const btVector3& rpos) const
  388. {
  389. if(m_rigid) return(btCross(m_rigid->getAngularVelocity(),rpos));
  390. if(m_soft) return(btCross(m_soft->m_av,rpos));
  391. return(btVector3(0,0,0));
  392. }
  393. btVector3 angularVelocity() const
  394. {
  395. if(m_rigid) return(m_rigid->getAngularVelocity());
  396. if(m_soft) return(m_soft->m_av);
  397. return(btVector3(0,0,0));
  398. }
  399. btVector3 velocity(const btVector3& rpos) const
  400. {
  401. return(linearVelocity()+angularVelocity(rpos));
  402. }
  403. void applyVImpulse(const btVector3& impulse,const btVector3& rpos) const
  404. {
  405. if(m_rigid) m_rigid->applyImpulse(impulse,rpos);
  406. if(m_soft) btSoftBody::clusterVImpulse(m_soft,rpos,impulse);
  407. }
  408. void applyDImpulse(const btVector3& impulse,const btVector3& rpos) const
  409. {
  410. if(m_rigid) m_rigid->applyImpulse(impulse,rpos);
  411. if(m_soft) btSoftBody::clusterDImpulse(m_soft,rpos,impulse);
  412. }
  413. void applyImpulse(const Impulse& impulse,const btVector3& rpos) const
  414. {
  415. if(impulse.m_asVelocity)
  416. {
  417. // printf("impulse.m_velocity = %f,%f,%f\n",impulse.m_velocity.getX(),impulse.m_velocity.getY(),impulse.m_velocity.getZ());
  418. applyVImpulse(impulse.m_velocity,rpos);
  419. }
  420. if(impulse.m_asDrift)
  421. {
  422. // printf("impulse.m_drift = %f,%f,%f\n",impulse.m_drift.getX(),impulse.m_drift.getY(),impulse.m_drift.getZ());
  423. applyDImpulse(impulse.m_drift,rpos);
  424. }
  425. }
  426. void applyVAImpulse(const btVector3& impulse) const
  427. {
  428. if(m_rigid) m_rigid->applyTorqueImpulse(impulse);
  429. if(m_soft) btSoftBody::clusterVAImpulse(m_soft,impulse);
  430. }
  431. void applyDAImpulse(const btVector3& impulse) const
  432. {
  433. if(m_rigid) m_rigid->applyTorqueImpulse(impulse);
  434. if(m_soft) btSoftBody::clusterDAImpulse(m_soft,impulse);
  435. }
  436. void applyAImpulse(const Impulse& impulse) const
  437. {
  438. if(impulse.m_asVelocity) applyVAImpulse(impulse.m_velocity);
  439. if(impulse.m_asDrift) applyDAImpulse(impulse.m_drift);
  440. }
  441. void applyDCImpulse(const btVector3& impulse) const
  442. {
  443. if(m_rigid) m_rigid->applyCentralImpulse(impulse);
  444. if(m_soft) btSoftBody::clusterDCImpulse(m_soft,impulse);
  445. }
  446. };
  447. /* Joint */
  448. struct Joint
  449. {
  450. struct eType { enum _ {
  451. Linear=0,
  452. Angular,
  453. Contact
  454. };};
  455. struct Specs
  456. {
  457. Specs() : erp(1),cfm(1),split(1) {}
  458. btScalar erp;
  459. btScalar cfm;
  460. btScalar split;
  461. };
  462. Body m_bodies[2];
  463. btVector3 m_refs[2];
  464. btScalar m_cfm;
  465. btScalar m_erp;
  466. btScalar m_split;
  467. btVector3 m_drift;
  468. btVector3 m_sdrift;
  469. btMatrix3x3 m_massmatrix;
  470. bool m_delete;
  471. virtual ~Joint() {}
  472. Joint() : m_delete(false) {}
  473. virtual void Prepare(btScalar dt,int iterations);
  474. virtual void Solve(btScalar dt,btScalar sor)=0;
  475. virtual void Terminate(btScalar dt)=0;
  476. virtual eType::_ Type() const=0;
  477. };
  478. /* LJoint */
  479. struct LJoint : Joint
  480. {
  481. struct Specs : Joint::Specs
  482. {
  483. btVector3 position;
  484. };
  485. btVector3 m_rpos[2];
  486. void Prepare(btScalar dt,int iterations);
  487. void Solve(btScalar dt,btScalar sor);
  488. void Terminate(btScalar dt);
  489. eType::_ Type() const { return(eType::Linear); }
  490. };
  491. /* AJoint */
  492. struct AJoint : Joint
  493. {
  494. struct IControl
  495. {
  496. virtual void Prepare(AJoint*) {}
  497. virtual btScalar Speed(AJoint*,btScalar current) { return(current); }
  498. static IControl* Default() { static IControl def;return(&def); }
  499. };
  500. struct Specs : Joint::Specs
  501. {
  502. Specs() : icontrol(IControl::Default()) {}
  503. btVector3 axis;
  504. IControl* icontrol;
  505. };
  506. btVector3 m_axis[2];
  507. IControl* m_icontrol;
  508. void Prepare(btScalar dt,int iterations);
  509. void Solve(btScalar dt,btScalar sor);
  510. void Terminate(btScalar dt);
  511. eType::_ Type() const { return(eType::Angular); }
  512. };
  513. /* CJoint */
  514. struct CJoint : Joint
  515. {
  516. int m_life;
  517. int m_maxlife;
  518. btVector3 m_rpos[2];
  519. btVector3 m_normal;
  520. btScalar m_friction;
  521. void Prepare(btScalar dt,int iterations);
  522. void Solve(btScalar dt,btScalar sor);
  523. void Terminate(btScalar dt);
  524. eType::_ Type() const { return(eType::Contact); }
  525. };
  526. /* Config */
  527. struct Config
  528. {
  529. eAeroModel::_ aeromodel; // Aerodynamic model (default: V_Point)
  530. btScalar kVCF; // Velocities correction factor (Baumgarte)
  531. btScalar kDP; // Damping coefficient [0,1]
  532. btScalar kDG; // Drag coefficient [0,+inf]
  533. btScalar kLF; // Lift coefficient [0,+inf]
  534. btScalar kPR; // Pressure coefficient [-inf,+inf]
  535. btScalar kVC; // Volume conversation coefficient [0,+inf]
  536. btScalar kDF; // Dynamic friction coefficient [0,1]
  537. btScalar kMT; // Pose matching coefficient [0,1]
  538. btScalar kCHR; // Rigid contacts hardness [0,1]
  539. btScalar kKHR; // Kinetic contacts hardness [0,1]
  540. btScalar kSHR; // Soft contacts hardness [0,1]
  541. btScalar kAHR; // Anchors hardness [0,1]
  542. btScalar kSRHR_CL; // Soft vs rigid hardness [0,1] (cluster only)
  543. btScalar kSKHR_CL; // Soft vs kinetic hardness [0,1] (cluster only)
  544. btScalar kSSHR_CL; // Soft vs soft hardness [0,1] (cluster only)
  545. btScalar kSR_SPLT_CL; // Soft vs rigid impulse split [0,1] (cluster only)
  546. btScalar kSK_SPLT_CL; // Soft vs rigid impulse split [0,1] (cluster only)
  547. btScalar kSS_SPLT_CL; // Soft vs rigid impulse split [0,1] (cluster only)
  548. btScalar maxvolume; // Maximum volume ratio for pose
  549. btScalar timescale; // Time scale
  550. int viterations; // Velocities solver iterations
  551. int piterations; // Positions solver iterations
  552. int diterations; // Drift solver iterations
  553. int citerations; // Cluster solver iterations
  554. int collisions; // Collisions flags
  555. tVSolverArray m_vsequence; // Velocity solvers sequence
  556. tPSolverArray m_psequence; // Position solvers sequence
  557. tPSolverArray m_dsequence; // Drift solvers sequence
  558. };
  559. /* SolverState */
  560. struct SolverState
  561. {
  562. btScalar sdt; // dt*timescale
  563. btScalar isdt; // 1/sdt
  564. btScalar velmrg; // velocity margin
  565. btScalar radmrg; // radial margin
  566. btScalar updmrg; // Update margin
  567. };
  568. /// RayFromToCaster takes a ray from, ray to (instead of direction!)
  569. struct RayFromToCaster : btDbvt::ICollide
  570. {
  571. btVector3 m_rayFrom;
  572. btVector3 m_rayTo;
  573. btVector3 m_rayNormalizedDirection;
  574. btScalar m_mint;
  575. Face* m_face;
  576. int m_tests;
  577. RayFromToCaster(const btVector3& rayFrom,const btVector3& rayTo,btScalar mxt);
  578. void Process(const btDbvtNode* leaf);
  579. static inline btScalar rayFromToTriangle(const btVector3& rayFrom,
  580. const btVector3& rayTo,
  581. const btVector3& rayNormalizedDirection,
  582. const btVector3& a,
  583. const btVector3& b,
  584. const btVector3& c,
  585. btScalar maxt=SIMD_INFINITY);
  586. };
  587. //
  588. // Typedefs
  589. //
  590. typedef void (*psolver_t)(btSoftBody*,btScalar,btScalar);
  591. typedef void (*vsolver_t)(btSoftBody*,btScalar);
  592. typedef btAlignedObjectArray<Cluster*> tClusterArray;
  593. typedef btAlignedObjectArray<Note> tNoteArray;
  594. typedef btAlignedObjectArray<Node> tNodeArray;
  595. typedef btAlignedObjectArray<btDbvtNode*> tLeafArray;
  596. typedef btAlignedObjectArray<Link> tLinkArray;
  597. typedef btAlignedObjectArray<Face> tFaceArray;
  598. typedef btAlignedObjectArray<Tetra> tTetraArray;
  599. typedef btAlignedObjectArray<Anchor> tAnchorArray;
  600. typedef btAlignedObjectArray<RContact> tRContactArray;
  601. typedef btAlignedObjectArray<SContact> tSContactArray;
  602. typedef btAlignedObjectArray<Material*> tMaterialArray;
  603. typedef btAlignedObjectArray<Joint*> tJointArray;
  604. typedef btAlignedObjectArray<btSoftBody*> tSoftBodyArray;
  605. //
  606. // Fields
  607. //
  608. Config m_cfg; // Configuration
  609. SolverState m_sst; // Solver state
  610. Pose m_pose; // Pose
  611. void* m_tag; // User data
  612. btSoftBodyWorldInfo* m_worldInfo; // World info
  613. tNoteArray m_notes; // Notes
  614. tNodeArray m_nodes; // Nodes
  615. tLinkArray m_links; // Links
  616. tFaceArray m_faces; // Faces
  617. tTetraArray m_tetras; // Tetras
  618. tAnchorArray m_anchors; // Anchors
  619. tRContactArray m_rcontacts; // Rigid contacts
  620. tSContactArray m_scontacts; // Soft contacts
  621. tJointArray m_joints; // Joints
  622. tMaterialArray m_materials; // Materials
  623. btScalar m_timeacc; // Time accumulator
  624. btVector3 m_bounds[2]; // Spatial bounds
  625. bool m_bUpdateRtCst; // Update runtime constants
  626. btDbvt m_ndbvt; // Nodes tree
  627. btDbvt m_fdbvt; // Faces tree
  628. btDbvt m_cdbvt; // Clusters tree
  629. tClusterArray m_clusters; // Clusters
  630. btAlignedObjectArray<bool>m_clusterConnectivity;//cluster connectivity, for self-collision
  631. btTransform m_initialWorldTransform;
  632. btVector3 m_windVelocity;
  633. btScalar m_restLengthScale;
  634. //
  635. // Api
  636. //
  637. /* ctor */
  638. btSoftBody( btSoftBodyWorldInfo* worldInfo,int node_count, const btVector3* x, const btScalar* m);
  639. /* ctor */
  640. btSoftBody( btSoftBodyWorldInfo* worldInfo);
  641. void initDefaults();
  642. /* dtor */
  643. virtual ~btSoftBody();
  644. /* Check for existing link */
  645. btAlignedObjectArray<int> m_userIndexMapping;
  646. btSoftBodyWorldInfo* getWorldInfo()
  647. {
  648. return m_worldInfo;
  649. }
  650. ///@todo: avoid internal softbody shape hack and move collision code to collision library
  651. virtual void setCollisionShape(btCollisionShape* collisionShape)
  652. {
  653. }
  654. bool checkLink( int node0,
  655. int node1) const;
  656. bool checkLink( const Node* node0,
  657. const Node* node1) const;
  658. /* Check for existring face */
  659. bool checkFace( int node0,
  660. int node1,
  661. int node2) const;
  662. /* Append material */
  663. Material* appendMaterial();
  664. /* Append note */
  665. void appendNote( const char* text,
  666. const btVector3& o,
  667. const btVector4& c=btVector4(1,0,0,0),
  668. Node* n0=0,
  669. Node* n1=0,
  670. Node* n2=0,
  671. Node* n3=0);
  672. void appendNote( const char* text,
  673. const btVector3& o,
  674. Node* feature);
  675. void appendNote( const char* text,
  676. const btVector3& o,
  677. Link* feature);
  678. void appendNote( const char* text,
  679. const btVector3& o,
  680. Face* feature);
  681. /* Append node */
  682. void appendNode( const btVector3& x,btScalar m);
  683. /* Append link */
  684. void appendLink(int model=-1,Material* mat=0);
  685. void appendLink( int node0,
  686. int node1,
  687. Material* mat=0,
  688. bool bcheckexist=false);
  689. void appendLink( Node* node0,
  690. Node* node1,
  691. Material* mat=0,
  692. bool bcheckexist=false);
  693. /* Append face */
  694. void appendFace(int model=-1,Material* mat=0);
  695. void appendFace( int node0,
  696. int node1,
  697. int node2,
  698. Material* mat=0);
  699. void appendTetra(int model,Material* mat);
  700. //
  701. void appendTetra(int node0,
  702. int node1,
  703. int node2,
  704. int node3,
  705. Material* mat=0);
  706. /* Append anchor */
  707. void appendAnchor( int node,
  708. btRigidBody* body, bool disableCollisionBetweenLinkedBodies=false,btScalar influence = 1);
  709. void appendAnchor(int node,btRigidBody* body, const btVector3& localPivot,bool disableCollisionBetweenLinkedBodies=false,btScalar influence = 1);
  710. /* Append linear joint */
  711. void appendLinearJoint(const LJoint::Specs& specs,Cluster* body0,Body body1);
  712. void appendLinearJoint(const LJoint::Specs& specs,Body body=Body());
  713. void appendLinearJoint(const LJoint::Specs& specs,btSoftBody* body);
  714. /* Append linear joint */
  715. void appendAngularJoint(const AJoint::Specs& specs,Cluster* body0,Body body1);
  716. void appendAngularJoint(const AJoint::Specs& specs,Body body=Body());
  717. void appendAngularJoint(const AJoint::Specs& specs,btSoftBody* body);
  718. /* Add force (or gravity) to the entire body */
  719. void addForce( const btVector3& force);
  720. /* Add force (or gravity) to a node of the body */
  721. void addForce( const btVector3& force,
  722. int node);
  723. /* Add aero force to a node of the body */
  724. void addAeroForceToNode(const btVector3& windVelocity,int nodeIndex);
  725. /* Add aero force to a face of the body */
  726. void addAeroForceToFace(const btVector3& windVelocity,int faceIndex);
  727. /* Add velocity to the entire body */
  728. void addVelocity( const btVector3& velocity);
  729. /* Set velocity for the entire body */
  730. void setVelocity( const btVector3& velocity);
  731. /* Add velocity to a node of the body */
  732. void addVelocity( const btVector3& velocity,
  733. int node);
  734. /* Set mass */
  735. void setMass( int node,
  736. btScalar mass);
  737. /* Get mass */
  738. btScalar getMass( int node) const;
  739. /* Get total mass */
  740. btScalar getTotalMass() const;
  741. /* Set total mass (weighted by previous masses) */
  742. void setTotalMass( btScalar mass,
  743. bool fromfaces=false);
  744. /* Set total density */
  745. void setTotalDensity(btScalar density);
  746. /* Set volume mass (using tetrahedrons) */
  747. void setVolumeMass( btScalar mass);
  748. /* Set volume density (using tetrahedrons) */
  749. void setVolumeDensity( btScalar density);
  750. /* Transform */
  751. void transform( const btTransform& trs);
  752. /* Translate */
  753. void translate( const btVector3& trs);
  754. /* Rotate */
  755. void rotate( const btQuaternion& rot);
  756. /* Scale */
  757. void scale( const btVector3& scl);
  758. /* Get link resting lengths scale */
  759. btScalar getRestLengthScale();
  760. /* Scale resting length of all springs */
  761. void setRestLengthScale(btScalar restLength);
  762. /* Set current state as pose */
  763. void setPose( bool bvolume,
  764. bool bframe);
  765. /* Set current link lengths as resting lengths */
  766. void resetLinkRestLengths();
  767. /* Return the volume */
  768. btScalar getVolume() const;
  769. /* Cluster count */
  770. int clusterCount() const;
  771. /* Cluster center of mass */
  772. static btVector3 clusterCom(const Cluster* cluster);
  773. btVector3 clusterCom(int cluster) const;
  774. /* Cluster velocity at rpos */
  775. static btVector3 clusterVelocity(const Cluster* cluster,const btVector3& rpos);
  776. /* Cluster impulse */
  777. static void clusterVImpulse(Cluster* cluster,const btVector3& rpos,const btVector3& impulse);
  778. static void clusterDImpulse(Cluster* cluster,const btVector3& rpos,const btVector3& impulse);
  779. static void clusterImpulse(Cluster* cluster,const btVector3& rpos,const Impulse& impulse);
  780. static void clusterVAImpulse(Cluster* cluster,const btVector3& impulse);
  781. static void clusterDAImpulse(Cluster* cluster,const btVector3& impulse);
  782. static void clusterAImpulse(Cluster* cluster,const Impulse& impulse);
  783. static void clusterDCImpulse(Cluster* cluster,const btVector3& impulse);
  784. /* Generate bending constraints based on distance in the adjency graph */
  785. int generateBendingConstraints( int distance,
  786. Material* mat=0);
  787. /* Randomize constraints to reduce solver bias */
  788. void randomizeConstraints();
  789. /* Release clusters */
  790. void releaseCluster(int index);
  791. void releaseClusters();
  792. /* Generate clusters (K-mean) */
  793. ///generateClusters with k=0 will create a convex cluster for each tetrahedron or triangle
  794. ///otherwise an approximation will be used (better performance)
  795. int generateClusters(int k,int maxiterations=8192);
  796. /* Refine */
  797. void refine(ImplicitFn* ifn,btScalar accurary,bool cut);
  798. /* CutLink */
  799. bool cutLink(int node0,int node1,btScalar position);
  800. bool cutLink(const Node* node0,const Node* node1,btScalar position);
  801. ///Ray casting using rayFrom and rayTo in worldspace, (not direction!)
  802. bool rayTest(const btVector3& rayFrom,
  803. const btVector3& rayTo,
  804. sRayCast& results);
  805. /* Solver presets */
  806. void setSolver(eSolverPresets::_ preset);
  807. /* predictMotion */
  808. void predictMotion(btScalar dt);
  809. /* solveConstraints */
  810. void solveConstraints();
  811. /* staticSolve */
  812. void staticSolve(int iterations);
  813. /* solveCommonConstraints */
  814. static void solveCommonConstraints(btSoftBody** bodies,int count,int iterations);
  815. /* solveClusters */
  816. static void solveClusters(const btAlignedObjectArray<btSoftBody*>& bodies);
  817. /* integrateMotion */
  818. void integrateMotion();
  819. /* defaultCollisionHandlers */
  820. void defaultCollisionHandler(const btCollisionObjectWrapper* pcoWrap);
  821. void defaultCollisionHandler(btSoftBody* psb);
  822. //
  823. // Functionality to deal with new accelerated solvers.
  824. //
  825. /**
  826. * Set a wind velocity for interaction with the air.
  827. */
  828. void setWindVelocity( const btVector3 &velocity );
  829. /**
  830. * Return the wind velocity for interaction with the air.
  831. */
  832. const btVector3& getWindVelocity();
  833. //
  834. // Set the solver that handles this soft body
  835. // Should not be allowed to get out of sync with reality
  836. // Currently called internally on addition to the world
  837. void setSoftBodySolver( btSoftBodySolver *softBodySolver )
  838. {
  839. m_softBodySolver = softBodySolver;
  840. }
  841. //
  842. // Return the solver that handles this soft body
  843. //
  844. btSoftBodySolver *getSoftBodySolver()
  845. {
  846. return m_softBodySolver;
  847. }
  848. //
  849. // Return the solver that handles this soft body
  850. //
  851. btSoftBodySolver *getSoftBodySolver() const
  852. {
  853. return m_softBodySolver;
  854. }
  855. //
  856. // Cast
  857. //
  858. static const btSoftBody* upcast(const btCollisionObject* colObj)
  859. {
  860. if (colObj->getInternalType()==CO_SOFT_BODY)
  861. return (const btSoftBody*)colObj;
  862. return 0;
  863. }
  864. static btSoftBody* upcast(btCollisionObject* colObj)
  865. {
  866. if (colObj->getInternalType()==CO_SOFT_BODY)
  867. return (btSoftBody*)colObj;
  868. return 0;
  869. }
  870. //
  871. // ::btCollisionObject
  872. //
  873. virtual void getAabb(btVector3& aabbMin,btVector3& aabbMax) const
  874. {
  875. aabbMin = m_bounds[0];
  876. aabbMax = m_bounds[1];
  877. }
  878. //
  879. // Private
  880. //
  881. void pointersToIndices();
  882. void indicesToPointers(const int* map=0);
  883. int rayTest(const btVector3& rayFrom,const btVector3& rayTo,
  884. btScalar& mint,eFeature::_& feature,int& index,bool bcountonly) const;
  885. void initializeFaceTree();
  886. btVector3 evaluateCom() const;
  887. bool checkContact(const btCollisionObjectWrapper* colObjWrap,const btVector3& x,btScalar margin,btSoftBody::sCti& cti) const;
  888. void updateNormals();
  889. void updateBounds();
  890. void updatePose();
  891. void updateConstants();
  892. void updateLinkConstants();
  893. void updateArea(bool averageArea = true);
  894. void initializeClusters();
  895. void updateClusters();
  896. void cleanupClusters();
  897. void prepareClusters(int iterations);
  898. void solveClusters(btScalar sor);
  899. void applyClusters(bool drift);
  900. void dampClusters();
  901. void applyForces();
  902. static void PSolve_Anchors(btSoftBody* psb,btScalar kst,btScalar ti);
  903. static void PSolve_RContacts(btSoftBody* psb,btScalar kst,btScalar ti);
  904. static void PSolve_SContacts(btSoftBody* psb,btScalar,btScalar ti);
  905. static void PSolve_Links(btSoftBody* psb,btScalar kst,btScalar ti);
  906. static void VSolve_Links(btSoftBody* psb,btScalar kst);
  907. static psolver_t getSolver(ePSolver::_ solver);
  908. static vsolver_t getSolver(eVSolver::_ solver);
  909. virtual int calculateSerializeBufferSize() const;
  910. ///fills the dataBuffer and returns the struct name (and 0 on failure)
  911. virtual const char* serialize(void* dataBuffer, class btSerializer* serializer) const;
  912. //virtual void serializeSingleObject(class btSerializer* serializer) const;
  913. };
  914. #endif //_BT_SOFT_BODY_H