SoftDemo.cpp 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. ///btSoftBody implementation by Nathanael Presson
  14. #include "btBulletDynamicsCommon.h"
  15. #include "BulletSoftBody/btSoftRigidDynamicsWorld.h"
  16. #include "BulletCollision/CollisionDispatch/btSphereSphereCollisionAlgorithm.h"
  17. #include "BulletCollision/NarrowPhaseCollision/btGjkEpa2.h"
  18. #include "LinearMath/btQuickprof.h"
  19. #include "LinearMath/btIDebugDraw.h"
  20. #include "BunnyMesh.h"
  21. #include "TorusMesh.h"
  22. #include <stdio.h> //printf debugging
  23. #include "LinearMath/btConvexHull.h"
  24. #include "BulletSoftBody/btSoftBodyRigidBodyCollisionConfiguration.h"
  25. #include "BulletSoftBody/btSoftBodyHelpers.h"
  26. #include "SoftDemo.h"
  27. #include "GL_ShapeDrawer.h"
  28. #include "LinearMath/btAlignedObjectArray.h"
  29. #include "BulletSoftBody/btSoftBody.h"
  30. class btBroadphaseInterface;
  31. class btCollisionShape;
  32. class btOverlappingPairCache;
  33. class btCollisionDispatcher;
  34. class btConstraintSolver;
  35. struct btCollisionAlgorithmCreateFunc;
  36. class btDefaultCollisionConfiguration;
  37. ///collisions between two btSoftBody's
  38. class btSoftSoftCollisionAlgorithm;
  39. ///collisions between a btSoftBody and a btRigidBody
  40. class btSoftRididCollisionAlgorithm;
  41. class btSoftRigidDynamicsWorld;
  42. #include "../CommonInterfaces/CommonRigidBodyBase.h"
  43. class SoftDemo : public CommonRigidBodyBase
  44. {
  45. public:
  46. btAlignedObjectArray<btSoftSoftCollisionAlgorithm*> m_SoftSoftCollisionAlgorithms;
  47. btAlignedObjectArray<btSoftRididCollisionAlgorithm*> m_SoftRigidCollisionAlgorithms;
  48. btSoftBodyWorldInfo m_softBodyWorldInfo;
  49. bool m_autocam;
  50. bool m_cutting;
  51. bool m_raycast;
  52. btScalar m_animtime;
  53. btClock m_clock;
  54. int m_lastmousepos[2];
  55. btVector3 m_impact;
  56. btSoftBody::sRayCast m_results;
  57. btSoftBody::Node* m_node;
  58. btVector3 m_goal;
  59. bool m_drag;
  60. //keep the collision shapes, for deletion/cleanup
  61. btAlignedObjectArray<btCollisionShape*> m_collisionShapes;
  62. btBroadphaseInterface* m_broadphase;
  63. btCollisionDispatcher* m_dispatcher;
  64. btConstraintSolver* m_solver;
  65. btCollisionAlgorithmCreateFunc* m_boxBoxCF;
  66. btDefaultCollisionConfiguration* m_collisionConfiguration;
  67. public:
  68. void initPhysics();
  69. void exitPhysics();
  70. virtual void resetCamera()
  71. {
  72. //@todo depends on current_demo?
  73. float dist = 45;
  74. float pitch = -31;
  75. float yaw = 27;
  76. float targetPos[3] = {10 - 1, 0};
  77. m_guiHelper->resetCamera(dist, yaw, pitch, targetPos[0], targetPos[1], targetPos[2]);
  78. }
  79. SoftDemo(struct GUIHelperInterface* helper)
  80. : CommonRigidBodyBase(helper),
  81. m_drag(false)
  82. {
  83. }
  84. virtual ~SoftDemo()
  85. {
  86. btAssert(m_dynamicsWorld == 0);
  87. }
  88. //virtual void clientMoveAndDisplay();
  89. //virtual void displayCallback();
  90. void createStack(btCollisionShape* boxShape, float halfCubeSize, int size, float zPos);
  91. virtual void setDrawClusters(bool drawClusters);
  92. virtual const btSoftRigidDynamicsWorld* getSoftDynamicsWorld() const
  93. {
  94. ///just make it a btSoftRigidDynamicsWorld please
  95. ///or we will add type checking
  96. return (btSoftRigidDynamicsWorld*)m_dynamicsWorld;
  97. }
  98. virtual btSoftRigidDynamicsWorld* getSoftDynamicsWorld()
  99. {
  100. ///just make it a btSoftRigidDynamicsWorld please
  101. ///or we will add type checking
  102. return (btSoftRigidDynamicsWorld*)m_dynamicsWorld;
  103. }
  104. //
  105. //void clientResetScene();
  106. void renderme();
  107. void keyboardCallback(unsigned char key, int x, int y);
  108. void mouseFunc(int button, int state, int x, int y);
  109. void mouseMotionFunc(int x, int y);
  110. GUIHelperInterface* getGUIHelper()
  111. {
  112. return m_guiHelper;
  113. }
  114. virtual void renderScene()
  115. {
  116. CommonRigidBodyBase::renderScene();
  117. btSoftRigidDynamicsWorld* softWorld = getSoftDynamicsWorld();
  118. for (int i = 0; i < softWorld->getSoftBodyArray().size(); i++)
  119. {
  120. btSoftBody* psb = (btSoftBody*)softWorld->getSoftBodyArray()[i];
  121. //if (softWorld->getDebugDrawer() && !(softWorld->getDebugDrawer()->getDebugMode() & (btIDebugDraw::DBG_DrawWireframe)))
  122. {
  123. btSoftBodyHelpers::DrawFrame(psb, softWorld->getDebugDrawer());
  124. btSoftBodyHelpers::Draw(psb, softWorld->getDebugDrawer(), softWorld->getDrawFlags());
  125. }
  126. }
  127. }
  128. };
  129. #define MACRO_SOFT_DEMO(a) \
  130. class SoftDemo##a : public SoftDemo \
  131. { \
  132. public: \
  133. static DemoApplication* Create() \
  134. { \
  135. SoftDemo* demo = new SoftDemo##a; \
  136. extern int current_demo; \
  137. current_demo = a; \
  138. demo->initPhysics(); \
  139. return demo; \
  140. } \
  141. };
  142. //MACRO_SOFT_DEMO(0) //Init_Cloth
  143. #if 0
  144. MACRO_SOFT_DEMO(1) //Init_Pressure
  145. MACRO_SOFT_DEMO(2)//Init_Volume
  146. MACRO_SOFT_DEMO(3)//Init_Ropes
  147. MACRO_SOFT_DEMO(4)//Init_Ropes_Attach
  148. MACRO_SOFT_DEMO(5)//Init_ClothAttach
  149. MACRO_SOFT_DEMO(6)//Init_Sticks
  150. MACRO_SOFT_DEMO(7)//Init_Collide
  151. MACRO_SOFT_DEMO(8)//Init_Collide2
  152. MACRO_SOFT_DEMO(9)//Init_Collide3
  153. MACRO_SOFT_DEMO(10)//Init_Impact
  154. MACRO_SOFT_DEMO(11)//Init_Aero
  155. MACRO_SOFT_DEMO(12)//Init_Friction
  156. MACRO_SOFT_DEMO(13)//Init_Torus
  157. MACRO_SOFT_DEMO(14)//Init_TorusMatch
  158. MACRO_SOFT_DEMO(15)//Init_Bunny
  159. MACRO_SOFT_DEMO(16)//Init_BunnyMatch
  160. MACRO_SOFT_DEMO(17)//Init_Cutting1
  161. MACRO_SOFT_DEMO(18)//Init_ClusterDeform
  162. MACRO_SOFT_DEMO(19)//Init_ClusterCollide1
  163. MACRO_SOFT_DEMO(20)//Init_ClusterCollide2
  164. MACRO_SOFT_DEMO(21)//Init_ClusterSocket
  165. MACRO_SOFT_DEMO(22)//Init_ClusterHinge
  166. MACRO_SOFT_DEMO(23)//Init_ClusterCombine
  167. MACRO_SOFT_DEMO(24)//Init_ClusterCar
  168. MACRO_SOFT_DEMO(25)//Init_ClusterRobot
  169. MACRO_SOFT_DEMO(26)//Init_ClusterStackSoft
  170. MACRO_SOFT_DEMO(27)//Init_ClusterStackMixed
  171. MACRO_SOFT_DEMO(28)//Init_TetraCube
  172. MACRO_SOFT_DEMO(29)//Init_TetraBunny
  173. #endif
  174. extern float eye[3];
  175. extern int glutScreenWidth;
  176. extern int glutScreenHeight;
  177. //static bool sDemoMode = false;
  178. const int maxProxies = 32766;
  179. //const int maxOverlap = 65535;
  180. static btVector3* gGroundVertices = 0;
  181. static int* gGroundIndices = 0;
  182. //static btBvhTriangleMeshShape* trimeshShape =0;
  183. //static btRigidBody* staticBody = 0;
  184. static float waveheight = 5.f;
  185. const float TRIANGLE_SIZE = 8.f;
  186. int current_demo = 20;
  187. #define DEMO_MODE_TIMEOUT 15.f //15 seconds for each demo
  188. #ifdef _DEBUG
  189. //const int gNumObjects = 1;
  190. #else
  191. //const int gNumObjects = 1;//try this in release mode: 3000. never go above 16384, unless you increate maxNumObjects value in DemoApplication.cp
  192. #endif
  193. //const int maxNumObjects = 32760;
  194. #define CUBE_HALF_EXTENTS 1.5
  195. #define EXTRA_HEIGHT -10.f
  196. //
  197. void SoftDemo::createStack(btCollisionShape* boxShape, float halfCubeSize, int size, float zPos)
  198. {
  199. btTransform trans;
  200. trans.setIdentity();
  201. for (int i = 0; i < size; i++)
  202. {
  203. // This constructs a row, from left to right
  204. int rowSize = size - i;
  205. for (int j = 0; j < rowSize; j++)
  206. {
  207. btVector3 pos;
  208. pos.setValue(
  209. -rowSize * halfCubeSize + halfCubeSize + j * 2.0f * halfCubeSize,
  210. halfCubeSize + i * halfCubeSize * 2.0f,
  211. zPos);
  212. trans.setOrigin(pos);
  213. btScalar mass = 1.f;
  214. btRigidBody* body = 0;
  215. body = createRigidBody(mass, trans, boxShape);
  216. }
  217. }
  218. }
  219. ////////////////////////////////////
  220. ///for mouse picking
  221. void pickingPreTickCallback(btDynamicsWorld* world, btScalar timeStep)
  222. {
  223. SoftDemo* softDemo = (SoftDemo*)world->getWorldUserInfo();
  224. if (softDemo->m_drag)
  225. {
  226. const int x = softDemo->m_lastmousepos[0];
  227. const int y = softDemo->m_lastmousepos[1];
  228. float rf[3];
  229. softDemo->getGUIHelper()->getRenderInterface()->getActiveCamera()->getCameraPosition(rf);
  230. float target[3];
  231. softDemo->getGUIHelper()->getRenderInterface()->getActiveCamera()->getCameraTargetPosition(target);
  232. btVector3 cameraTargetPosition(target[0], target[1], target[2]);
  233. const btVector3 cameraPosition(rf[0], rf[1], rf[2]);
  234. const btVector3 rayFrom = cameraPosition;
  235. const btVector3 rayTo = softDemo->getRayTo(x, y);
  236. const btVector3 rayDir = (rayTo - rayFrom).normalized();
  237. const btVector3 N = (cameraTargetPosition - cameraPosition).normalized();
  238. const btScalar O = btDot(softDemo->m_impact, N);
  239. const btScalar den = btDot(N, rayDir);
  240. if ((den * den) > 0)
  241. {
  242. const btScalar num = O - btDot(N, rayFrom);
  243. const btScalar hit = num / den;
  244. if ((hit > 0) && (hit < 1500))
  245. {
  246. softDemo->m_goal = rayFrom + rayDir * hit;
  247. }
  248. }
  249. btVector3 delta = softDemo->m_goal - softDemo->m_node->m_x;
  250. static const btScalar maxdrag = 10;
  251. if (delta.length2() > (maxdrag * maxdrag))
  252. {
  253. delta = delta.normalized() * maxdrag;
  254. }
  255. softDemo->m_node->m_v += delta / timeStep;
  256. }
  257. }
  258. //
  259. // ImplicitShape
  260. //
  261. //
  262. struct ImplicitSphere : btSoftBody::ImplicitFn
  263. {
  264. btVector3 center;
  265. btScalar sqradius;
  266. ImplicitSphere() {}
  267. ImplicitSphere(const btVector3& c, btScalar r) : center(c), sqradius(r * r) {}
  268. btScalar Eval(const btVector3& x)
  269. {
  270. return ((x - center).length2() - sqradius);
  271. }
  272. };
  273. //
  274. // Tetra meshes
  275. //
  276. struct TetraBunny
  277. {
  278. #include "bunny.inl"
  279. };
  280. struct TetraCube
  281. {
  282. #include "cube.inl"
  283. };
  284. //
  285. // Random
  286. //
  287. static inline btScalar UnitRand()
  288. {
  289. return (rand() / (btScalar)RAND_MAX);
  290. }
  291. static inline btScalar SignedUnitRand()
  292. {
  293. return (UnitRand() * 2 - 1);
  294. }
  295. static inline btVector3 Vector3Rand()
  296. {
  297. const btVector3 p = btVector3(SignedUnitRand(), SignedUnitRand(), SignedUnitRand());
  298. return (p.normalized());
  299. }
  300. //
  301. // Rb rain
  302. //
  303. static void Ctor_RbUpStack(SoftDemo* pdemo, int count)
  304. {
  305. float mass = 10;
  306. btCompoundShape* cylinderCompound = new btCompoundShape;
  307. btCollisionShape* cylinderShape = new btCylinderShapeX(btVector3(4, 1, 1));
  308. btCollisionShape* boxShape = new btBoxShape(btVector3(4, 1, 1));
  309. btTransform localTransform;
  310. localTransform.setIdentity();
  311. cylinderCompound->addChildShape(localTransform, boxShape);
  312. btQuaternion orn(SIMD_HALF_PI, 0, 0);
  313. localTransform.setRotation(orn);
  314. // localTransform.setOrigin(btVector3(1,1,1));
  315. cylinderCompound->addChildShape(localTransform, cylinderShape);
  316. btCollisionShape* shape[] = {cylinderCompound,
  317. new btBoxShape(btVector3(1, 1, 1)),
  318. new btSphereShape(1.5)
  319. };
  320. static const int nshapes = sizeof(shape) / sizeof(shape[0]);
  321. for (int i = 0; i < count; ++i)
  322. {
  323. btTransform startTransform;
  324. startTransform.setIdentity();
  325. startTransform.setOrigin(btVector3(0, 2 + 6 * i, 0));
  326. pdemo->createRigidBody(mass, startTransform, shape[i % nshapes]);
  327. //pdemo->createRigidBody(mass,startTransform,shape[0]);
  328. }
  329. }
  330. //
  331. // Big ball
  332. //
  333. static void Ctor_BigBall(SoftDemo* pdemo, btScalar mass = 10)
  334. {
  335. btTransform startTransform;
  336. startTransform.setIdentity();
  337. startTransform.setOrigin(btVector3(0, 13, 0));
  338. pdemo->createRigidBody(mass, startTransform, new btSphereShape(3));
  339. }
  340. //
  341. // Big plate
  342. //
  343. static btRigidBody* Ctor_BigPlate(SoftDemo* pdemo, btScalar mass = 15, btScalar height = 4)
  344. {
  345. btTransform startTransform;
  346. startTransform.setIdentity();
  347. startTransform.setOrigin(btVector3(0, height, 0.5));
  348. btRigidBody* body = pdemo->createRigidBody(mass, startTransform, new btBoxShape(btVector3(5, 1, 5)));
  349. body->setFriction(1);
  350. return (body);
  351. }
  352. //
  353. // Linear stair
  354. //
  355. static void Ctor_LinearStair(SoftDemo* pdemo, const btVector3& org, const btVector3& sizes, btScalar angle, int count)
  356. {
  357. btBoxShape* shape = new btBoxShape(sizes);
  358. for (int i = 0; i < count; ++i)
  359. {
  360. btTransform startTransform;
  361. startTransform.setIdentity();
  362. startTransform.setOrigin(org + btVector3(sizes.x() * i * 2, sizes.y() * i * 2, 0));
  363. btRigidBody* body = pdemo->createRigidBody(0, startTransform, shape);
  364. body->setFriction(1);
  365. }
  366. }
  367. //
  368. // Softbox
  369. //
  370. static btSoftBody* Ctor_SoftBox(SoftDemo* pdemo, const btVector3& p, const btVector3& s)
  371. {
  372. const btVector3 h = s * 0.5;
  373. const btVector3 c[] = {p + h * btVector3(-1, -1, -1),
  374. p + h * btVector3(+1, -1, -1),
  375. p + h * btVector3(-1, +1, -1),
  376. p + h * btVector3(+1, +1, -1),
  377. p + h * btVector3(-1, -1, +1),
  378. p + h * btVector3(+1, -1, +1),
  379. p + h * btVector3(-1, +1, +1),
  380. p + h * btVector3(+1, +1, +1)};
  381. btSoftBody* psb = btSoftBodyHelpers::CreateFromConvexHull(pdemo->m_softBodyWorldInfo, c, 8);
  382. psb->generateBendingConstraints(2);
  383. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  384. return (psb);
  385. }
  386. //
  387. // SoftBoulder
  388. //
  389. static btSoftBody* Ctor_SoftBoulder(SoftDemo* pdemo, const btVector3& p, const btVector3& s, int np, int id)
  390. {
  391. btAlignedObjectArray<btVector3> pts;
  392. if (id) srand(id);
  393. for (int i = 0; i < np; ++i)
  394. {
  395. pts.push_back(Vector3Rand() * s + p);
  396. }
  397. btSoftBody* psb = btSoftBodyHelpers::CreateFromConvexHull(pdemo->m_softBodyWorldInfo, &pts[0], pts.size());
  398. psb->generateBendingConstraints(2);
  399. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  400. return (psb);
  401. }
  402. //#define TRACEDEMO { pdemo->demoname=__FUNCTION__+5;printf("Launching demo: " __FUNCTION__ "\r\n"); }
  403. //
  404. // Basic ropes
  405. //
  406. static void Init_Ropes(SoftDemo* pdemo)
  407. {
  408. //TRACEDEMO
  409. const int n = 15;
  410. for (int i = 0; i < n; ++i)
  411. {
  412. btSoftBody* psb = btSoftBodyHelpers::CreateRope(pdemo->m_softBodyWorldInfo, btVector3(-10, 0, i * 0.25),
  413. btVector3(10, 0, i * 0.25),
  414. 16,
  415. 1 + 2);
  416. psb->m_cfg.piterations = 4;
  417. psb->m_materials[0]->m_kLST = 0.1 + (i / (btScalar)(n - 1)) * 0.9;
  418. psb->setTotalMass(20);
  419. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  420. }
  421. }
  422. //
  423. // Rope attach
  424. //
  425. static void Init_RopeAttach(SoftDemo* pdemo)
  426. {
  427. //TRACEDEMO
  428. pdemo->m_softBodyWorldInfo.m_sparsesdf.RemoveReferences(0);
  429. struct Functors
  430. {
  431. static btSoftBody* CtorRope(SoftDemo* pdemo, const btVector3& p)
  432. {
  433. btSoftBody* psb = btSoftBodyHelpers::CreateRope(pdemo->m_softBodyWorldInfo, p, p + btVector3(10, 0, 0), 8, 1);
  434. psb->setTotalMass(50);
  435. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  436. return (psb);
  437. }
  438. };
  439. btTransform startTransform;
  440. startTransform.setIdentity();
  441. startTransform.setOrigin(btVector3(12, 8, 0));
  442. btRigidBody* body = pdemo->createRigidBody(50, startTransform, new btBoxShape(btVector3(2, 6, 2)));
  443. btSoftBody* psb0 = Functors::CtorRope(pdemo, btVector3(0, 8, -1));
  444. btSoftBody* psb1 = Functors::CtorRope(pdemo, btVector3(0, 8, +1));
  445. psb0->appendAnchor(psb0->m_nodes.size() - 1, body);
  446. psb1->appendAnchor(psb1->m_nodes.size() - 1, body);
  447. }
  448. //
  449. // Cloth attach
  450. //
  451. static void Init_ClothAttach(SoftDemo* pdemo)
  452. {
  453. //TRACEDEMO
  454. const btScalar s = 4;
  455. const btScalar h = 6;
  456. const int r = 9;
  457. btSoftBody* psb = btSoftBodyHelpers::CreatePatch(pdemo->m_softBodyWorldInfo, btVector3(-s, h, -s),
  458. btVector3(+s, h, -s),
  459. btVector3(-s, h, +s),
  460. btVector3(+s, h, +s), r, r, 4 + 8, true);
  461. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  462. btTransform startTransform;
  463. startTransform.setIdentity();
  464. startTransform.setOrigin(btVector3(0, h, -(s + 3.5)));
  465. btRigidBody* body = pdemo->createRigidBody(20, startTransform, new btBoxShape(btVector3(s, 1, 3)));
  466. psb->appendAnchor(0, body);
  467. psb->appendAnchor(r - 1, body);
  468. pdemo->m_cutting = true;
  469. }
  470. //
  471. // Impact
  472. //
  473. static void Init_Impact(SoftDemo* pdemo)
  474. {
  475. //TRACEDEMO
  476. btSoftBody* psb = btSoftBodyHelpers::CreateRope(pdemo->m_softBodyWorldInfo, btVector3(0, 0, 0),
  477. btVector3(0, -1, 0),
  478. 0,
  479. 1);
  480. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  481. psb->m_cfg.kCHR = 0.5;
  482. btTransform startTransform;
  483. startTransform.setIdentity();
  484. startTransform.setOrigin(btVector3(0, 20, 0));
  485. pdemo->createRigidBody(10, startTransform, new btBoxShape(btVector3(2, 2, 2)));
  486. }
  487. static void Init_CapsuleCollision(SoftDemo* pdemo)
  488. {
  489. #ifdef USE_AMD_OPENCL
  490. btAlignedObjectArray<btSoftBody*> emptyArray;
  491. if (g_openCLSIMDSolver)
  492. g_openCLSIMDSolver->optimize(emptyArray);
  493. #endif //USE_AMD_OPENCL
  494. //TRACEDEMO
  495. const btScalar s = 4;
  496. const btScalar h = 6;
  497. const int r = 20;
  498. btTransform startTransform;
  499. startTransform.setIdentity();
  500. startTransform.setOrigin(btVector3(0, h - 2, 0));
  501. btCollisionShape* capsuleShape = new btCapsuleShapeX(1, 5);
  502. capsuleShape->setMargin(0.5);
  503. // capsule->setLocalScaling(btVector3(5,1,1));
  504. // btRigidBody* body=pdemo->createRigidBody(20,startTransform,capsuleShape);
  505. btRigidBody* body = pdemo->createRigidBody(0, startTransform, capsuleShape);
  506. body->setFriction(0.8f);
  507. int fixed = 0; //4+8;
  508. btSoftBody* psb = btSoftBodyHelpers::CreatePatch(pdemo->m_softBodyWorldInfo, btVector3(-s, h, -s),
  509. btVector3(+s, h, -s),
  510. btVector3(-s, h, +s),
  511. btVector3(+s, h, +s), r, r, fixed, true);
  512. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  513. psb->setTotalMass(0.1);
  514. psb->m_cfg.piterations = 10;
  515. psb->m_cfg.citerations = 10;
  516. psb->m_cfg.diterations = 10;
  517. // psb->m_cfg.viterations = 10;
  518. // psb->appendAnchor(0,body);
  519. // psb->appendAnchor(r-1,body);
  520. // pdemo->m_cutting=true;
  521. }
  522. //
  523. // Collide
  524. //
  525. static void Init_Collide(SoftDemo* pdemo)
  526. {
  527. //TRACEDEMO
  528. struct Functor
  529. {
  530. static btSoftBody* Create(SoftDemo* pdemo, const btVector3& x, const btVector3& a)
  531. {
  532. btSoftBody* psb = btSoftBodyHelpers::CreateFromTriMesh(pdemo->m_softBodyWorldInfo, gVertices,
  533. &gIndices[0][0],
  534. NUM_TRIANGLES);
  535. psb->generateBendingConstraints(2);
  536. psb->m_cfg.piterations = 2;
  537. psb->m_cfg.collisions |= btSoftBody::fCollision::VF_SS;
  538. psb->randomizeConstraints();
  539. btMatrix3x3 m;
  540. m.setEulerZYX(a.x(), a.y(), a.z());
  541. psb->transform(btTransform(m, x));
  542. psb->scale(btVector3(2, 2, 2));
  543. psb->setTotalMass(50, true);
  544. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  545. return (psb);
  546. }
  547. };
  548. for (int i = 0; i < 3; ++i)
  549. {
  550. Functor::Create(pdemo, btVector3(3 * i, 2, 0), btVector3(SIMD_PI / 2 * (1 - (i & 1)), SIMD_PI / 2 * (i & 1), 0));
  551. }
  552. pdemo->m_cutting = true;
  553. }
  554. //
  555. // Collide2
  556. //
  557. static void Init_Collide2(SoftDemo* pdemo)
  558. {
  559. //TRACEDEMO
  560. struct Functor
  561. {
  562. static btSoftBody* Create(SoftDemo* pdemo, const btVector3& x, const btVector3& a)
  563. {
  564. btSoftBody* psb = btSoftBodyHelpers::CreateFromTriMesh(pdemo->m_softBodyWorldInfo, gVerticesBunny,
  565. &gIndicesBunny[0][0],
  566. BUNNY_NUM_TRIANGLES);
  567. btSoftBody::Material* pm = psb->appendMaterial();
  568. pm->m_kLST = 0.5;
  569. pm->m_flags -= btSoftBody::fMaterial::DebugDraw;
  570. psb->generateBendingConstraints(2, pm);
  571. psb->m_cfg.piterations = 2;
  572. psb->m_cfg.kDF = 0.5;
  573. psb->m_cfg.collisions |= btSoftBody::fCollision::VF_SS;
  574. psb->randomizeConstraints();
  575. btMatrix3x3 m;
  576. m.setEulerZYX(a.x(), a.y(), a.z());
  577. psb->transform(btTransform(m, x));
  578. psb->scale(btVector3(6, 6, 6));
  579. psb->setTotalMass(100, true);
  580. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  581. return (psb);
  582. }
  583. };
  584. for (int i = 0; i < 3; ++i)
  585. {
  586. Functor::Create(pdemo, btVector3(0, -1 + 5 * i, 0), btVector3(0, SIMD_PI / 2 * (i & 1), 0));
  587. }
  588. pdemo->m_cutting = true;
  589. }
  590. //
  591. // Collide3
  592. //
  593. static void Init_Collide3(SoftDemo* pdemo)
  594. {
  595. //TRACEDEMO
  596. {
  597. const btScalar s = 8;
  598. btSoftBody* psb = btSoftBodyHelpers::CreatePatch(pdemo->m_softBodyWorldInfo, btVector3(-s, 0, -s),
  599. btVector3(+s, 0, -s),
  600. btVector3(-s, 0, +s),
  601. btVector3(+s, 0, +s),
  602. 15, 15, 1 + 2 + 4 + 8, true);
  603. psb->m_materials[0]->m_kLST = 0.4;
  604. psb->m_cfg.collisions |= btSoftBody::fCollision::VF_SS;
  605. psb->setTotalMass(150);
  606. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  607. }
  608. {
  609. const btScalar s = 4;
  610. const btVector3 o = btVector3(5, 10, 0);
  611. btSoftBody* psb = btSoftBodyHelpers::CreatePatch(pdemo->m_softBodyWorldInfo,
  612. btVector3(-s, 0, -s) + o,
  613. btVector3(+s, 0, -s) + o,
  614. btVector3(-s, 0, +s) + o,
  615. btVector3(+s, 0, +s) + o,
  616. 7, 7, 0, true);
  617. btSoftBody::Material* pm = psb->appendMaterial();
  618. pm->m_kLST = 0.1;
  619. pm->m_flags -= btSoftBody::fMaterial::DebugDraw;
  620. psb->generateBendingConstraints(2, pm);
  621. psb->m_materials[0]->m_kLST = 0.5;
  622. psb->m_cfg.collisions |= btSoftBody::fCollision::VF_SS;
  623. psb->setTotalMass(150);
  624. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  625. pdemo->m_cutting = true;
  626. }
  627. }
  628. //
  629. // Aerodynamic forces, 50x1g flyers
  630. //
  631. static void Init_Aero(SoftDemo* pdemo)
  632. {
  633. //TRACEDEMO
  634. const btScalar s = 2;
  635. const btScalar h = 10;
  636. const int segments = 6;
  637. const int count = 50;
  638. for (int i = 0; i < count; ++i)
  639. {
  640. btSoftBody* psb = btSoftBodyHelpers::CreatePatch(pdemo->m_softBodyWorldInfo, btVector3(-s, h, -s),
  641. btVector3(+s, h, -s),
  642. btVector3(-s, h, +s),
  643. btVector3(+s, h, +s),
  644. segments, segments,
  645. 0, true);
  646. btSoftBody::Material* pm = psb->appendMaterial();
  647. pm->m_flags -= btSoftBody::fMaterial::DebugDraw;
  648. psb->generateBendingConstraints(2, pm);
  649. psb->m_cfg.kLF = 0.004;
  650. psb->m_cfg.kDG = 0.0003;
  651. psb->m_cfg.aeromodel = btSoftBody::eAeroModel::V_TwoSided;
  652. btTransform trs;
  653. btQuaternion rot;
  654. btVector3 ra = Vector3Rand() * 0.1;
  655. btVector3 rp = Vector3Rand() * 15 + btVector3(0, 20, 80);
  656. rot.setEuler(SIMD_PI / 8 + ra.x(), -SIMD_PI / 7 + ra.y(), ra.z());
  657. trs.setIdentity();
  658. trs.setOrigin(rp);
  659. trs.setRotation(rot);
  660. psb->transform(trs);
  661. psb->setTotalMass(0.1);
  662. psb->addForce(btVector3(0, 2, 0), 0);
  663. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  664. }
  665. pdemo->m_autocam = true;
  666. }
  667. static void Init_Aero2(SoftDemo* pdemo)
  668. {
  669. //TRACEDEMO
  670. const btScalar s = 5;
  671. //psb->getWorldInfo()->m_gravity.setValue(0,0,0);
  672. const int segments = 10;
  673. const int count = 5;
  674. btVector3 pos(-s * segments, 0, 0);
  675. btScalar gap = 0.5;
  676. for (int i = 0; i < count; ++i)
  677. {
  678. btSoftBody* psb = btSoftBodyHelpers::CreatePatch(pdemo->m_softBodyWorldInfo, btVector3(-s, 0, -s * 3),
  679. btVector3(+s, 0, -s * 3),
  680. btVector3(-s, 0, +s),
  681. btVector3(+s, 0, +s),
  682. segments, segments * 3,
  683. 1 + 2, true);
  684. psb->getCollisionShape()->setMargin(0.5);
  685. btSoftBody::Material* pm = psb->appendMaterial();
  686. pm->m_kLST = 0.0004;
  687. pm->m_flags -= btSoftBody::fMaterial::DebugDraw;
  688. psb->generateBendingConstraints(2, pm);
  689. psb->m_cfg.kLF = 0.05;
  690. psb->m_cfg.kDG = 0.01;
  691. //psb->m_cfg.kLF = 0.004;
  692. //psb->m_cfg.kDG = 0.0003;
  693. psb->m_cfg.piterations = 2;
  694. psb->m_cfg.aeromodel = btSoftBody::eAeroModel::V_TwoSidedLiftDrag;
  695. psb->setWindVelocity(btVector3(4, -12.0, -25.0));
  696. btTransform trs;
  697. btQuaternion rot;
  698. pos += btVector3(s * 2 + gap, 0, 0);
  699. rot.setRotation(btVector3(1, 0, 0), btScalar(SIMD_PI / 2));
  700. trs.setIdentity();
  701. trs.setOrigin(pos);
  702. trs.setRotation(rot);
  703. psb->transform(trs);
  704. psb->setTotalMass(2.0);
  705. //this could help performance in some cases
  706. btSoftBodyHelpers::ReoptimizeLinkOrder(psb);
  707. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  708. }
  709. pdemo->m_autocam = true;
  710. }
  711. //
  712. // Friction
  713. //
  714. static void Init_Friction(SoftDemo* pdemo)
  715. {
  716. //TRACEDEMO
  717. const btScalar bs = 2;
  718. const btScalar ts = bs + bs / 4;
  719. for (int i = 0, ni = 20; i < ni; ++i)
  720. {
  721. const btVector3 p(-ni * ts / 2 + i * ts, -10 + bs, 40);
  722. btSoftBody* psb = Ctor_SoftBox(pdemo, p, btVector3(bs, bs, bs));
  723. psb->m_cfg.kDF = 0.1 * ((i + 1) / (btScalar)ni);
  724. psb->addVelocity(btVector3(0, 0, -10));
  725. }
  726. }
  727. //
  728. // Pressure
  729. //
  730. static void Init_Pressure(SoftDemo* pdemo)
  731. {
  732. //TRACEDEMO
  733. btSoftBody* psb = btSoftBodyHelpers::CreateEllipsoid(pdemo->m_softBodyWorldInfo, btVector3(35, 25, 0),
  734. btVector3(1, 1, 1) * 3,
  735. 512);
  736. psb->m_materials[0]->m_kLST = 0.1;
  737. psb->m_cfg.kDF = 1;
  738. psb->m_cfg.kDP = 0.001; // fun factor...
  739. psb->m_cfg.kPR = 2500;
  740. psb->setTotalMass(30, true);
  741. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  742. Ctor_BigPlate(pdemo);
  743. Ctor_LinearStair(pdemo, btVector3(0, 0, 0), btVector3(2, 1, 5), 0, 10);
  744. pdemo->m_autocam = true;
  745. }
  746. //
  747. // Volume conservation
  748. //
  749. static void Init_Volume(SoftDemo* pdemo)
  750. {
  751. //TRACEDEMO
  752. btSoftBody* psb = btSoftBodyHelpers::CreateEllipsoid(pdemo->m_softBodyWorldInfo, btVector3(35, 25, 0),
  753. btVector3(1, 1, 1) * 3,
  754. 512);
  755. psb->m_materials[0]->m_kLST = 0.45;
  756. psb->m_cfg.kVC = 20;
  757. psb->setTotalMass(50, true);
  758. psb->setPose(true, false);
  759. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  760. Ctor_BigPlate(pdemo);
  761. Ctor_LinearStair(pdemo, btVector3(0, 0, 0), btVector3(2, 1, 5), 0, 10);
  762. pdemo->m_autocam = true;
  763. }
  764. //
  765. // Stick+Bending+Rb's
  766. //
  767. static void Init_Sticks(SoftDemo* pdemo)
  768. {
  769. //TRACEDEMO
  770. const int n = 16;
  771. const int sg = 4;
  772. const btScalar sz = 5;
  773. const btScalar hg = 4;
  774. const btScalar in = 1 / (btScalar)(n - 1);
  775. for (int y = 0; y < n; ++y)
  776. {
  777. for (int x = 0; x < n; ++x)
  778. {
  779. const btVector3 org(-sz + sz * 2 * x * in,
  780. -10,
  781. -sz + sz * 2 * y * in);
  782. btSoftBody* psb = btSoftBodyHelpers::CreateRope(pdemo->m_softBodyWorldInfo, org,
  783. org + btVector3(hg * 0.001, hg, 0),
  784. sg,
  785. 1);
  786. psb->m_cfg.kDP = 0.005;
  787. psb->m_cfg.kCHR = 0.1;
  788. for (int i = 0; i < 3; ++i)
  789. {
  790. psb->generateBendingConstraints(2 + i);
  791. }
  792. psb->setMass(1, 0);
  793. psb->setTotalMass(0.01);
  794. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  795. }
  796. }
  797. Ctor_BigBall(pdemo);
  798. }
  799. //
  800. // Bending
  801. //
  802. static void Init_Bending(SoftDemo* pdemo)
  803. {
  804. //TRACEDEMO
  805. const btScalar s = 4;
  806. const btVector3 x[] = {btVector3(-s, 0, -s),
  807. btVector3(+s, 0, -s),
  808. btVector3(+s, 0, +s),
  809. btVector3(-s, 0, +s)};
  810. const btScalar m[] = {0, 0, 0, 1};
  811. btSoftBody* psb = new btSoftBody(&pdemo->m_softBodyWorldInfo, 4, x, m);
  812. psb->appendLink(0, 1);
  813. psb->appendLink(1, 2);
  814. psb->appendLink(2, 3);
  815. psb->appendLink(3, 0);
  816. psb->appendLink(0, 2);
  817. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  818. }
  819. //
  820. // 100kg cloth locked at corners, 10 falling 10kg rb's.
  821. //
  822. static void Init_Cloth(SoftDemo* pdemo)
  823. {
  824. //TRACEDEMO
  825. const btScalar s = 8;
  826. btSoftBody* psb = btSoftBodyHelpers::CreatePatch(pdemo->m_softBodyWorldInfo, btVector3(-s, 0, -s),
  827. btVector3(+s, 0, -s),
  828. btVector3(-s, 0, +s),
  829. btVector3(+s, 0, +s),
  830. 31, 31,
  831. // 31,31,
  832. 1 + 2 + 4 + 8, true);
  833. psb->getCollisionShape()->setMargin(0.5);
  834. btSoftBody::Material* pm = psb->appendMaterial();
  835. pm->m_kLST = 0.4;
  836. pm->m_flags -= btSoftBody::fMaterial::DebugDraw;
  837. psb->generateBendingConstraints(2, pm);
  838. psb->setTotalMass(150);
  839. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  840. Ctor_RbUpStack(pdemo, 10);
  841. pdemo->m_cutting = true;
  842. }
  843. //
  844. // 100kg Stanford's bunny
  845. //
  846. static void Init_Bunny(SoftDemo* pdemo)
  847. {
  848. //TRACEDEMO
  849. btSoftBody* psb = btSoftBodyHelpers::CreateFromTriMesh(pdemo->m_softBodyWorldInfo, gVerticesBunny,
  850. &gIndicesBunny[0][0],
  851. BUNNY_NUM_TRIANGLES);
  852. btSoftBody::Material* pm = psb->appendMaterial();
  853. pm->m_kLST = 0.5;
  854. pm->m_flags -= btSoftBody::fMaterial::DebugDraw;
  855. psb->generateBendingConstraints(2, pm);
  856. psb->m_cfg.piterations = 2;
  857. psb->m_cfg.kDF = 0.5;
  858. psb->randomizeConstraints();
  859. psb->scale(btVector3(6, 6, 6));
  860. psb->setTotalMass(100, true);
  861. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  862. pdemo->m_cutting = true;
  863. }
  864. //
  865. // 100kg Stanford's bunny with pose matching
  866. //
  867. static void Init_BunnyMatch(SoftDemo* pdemo)
  868. {
  869. //TRACEDEMO
  870. btSoftBody* psb = btSoftBodyHelpers::CreateFromTriMesh(pdemo->m_softBodyWorldInfo, gVerticesBunny,
  871. &gIndicesBunny[0][0],
  872. BUNNY_NUM_TRIANGLES);
  873. psb->m_cfg.kDF = 0.5;
  874. psb->m_cfg.kMT = 0.05;
  875. psb->m_cfg.piterations = 5;
  876. psb->randomizeConstraints();
  877. psb->scale(btVector3(6, 6, 6));
  878. psb->setTotalMass(100, true);
  879. psb->setPose(false, true);
  880. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  881. }
  882. //
  883. // 50Kg Torus
  884. //
  885. static void Init_Torus(SoftDemo* pdemo)
  886. {
  887. //TRACEDEMO
  888. btSoftBody* psb = btSoftBodyHelpers::CreateFromTriMesh(pdemo->m_softBodyWorldInfo, gVertices,
  889. &gIndices[0][0],
  890. NUM_TRIANGLES);
  891. psb->generateBendingConstraints(2);
  892. psb->m_cfg.piterations = 2;
  893. psb->randomizeConstraints();
  894. btMatrix3x3 m;
  895. m.setEulerZYX(SIMD_PI / 2, 0, 0);
  896. psb->transform(btTransform(m, btVector3(0, 4, 0)));
  897. psb->scale(btVector3(2, 2, 2));
  898. psb->setTotalMass(50, true);
  899. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  900. pdemo->m_cutting = true;
  901. }
  902. //
  903. // 50Kg Torus with pose matching
  904. //
  905. static void Init_TorusMatch(SoftDemo* pdemo)
  906. {
  907. //TRACEDEMO
  908. btSoftBody* psb = btSoftBodyHelpers::CreateFromTriMesh(pdemo->m_softBodyWorldInfo, gVertices,
  909. &gIndices[0][0],
  910. NUM_TRIANGLES);
  911. psb->m_materials[0]->m_kLST = 0.1;
  912. psb->m_cfg.kMT = 0.05;
  913. psb->randomizeConstraints();
  914. btMatrix3x3 m;
  915. m.setEulerZYX(SIMD_PI / 2, 0, 0);
  916. psb->transform(btTransform(m, btVector3(0, 4, 0)));
  917. psb->scale(btVector3(2, 2, 2));
  918. psb->setTotalMass(50, true);
  919. psb->setPose(false, true);
  920. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  921. }
  922. //
  923. // Cutting1
  924. //
  925. static void Init_Cutting1(SoftDemo* pdemo)
  926. {
  927. const btScalar s = 6;
  928. const btScalar h = 2;
  929. const int r = 16;
  930. const btVector3 p[] = {btVector3(+s, h, -s),
  931. btVector3(-s, h, -s),
  932. btVector3(+s, h, +s),
  933. btVector3(-s, h, +s)};
  934. btSoftBody* psb = btSoftBodyHelpers::CreatePatch(pdemo->m_softBodyWorldInfo, p[0], p[1], p[2], p[3], r, r, 1 + 2 + 4 + 8, true);
  935. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  936. psb->m_cfg.piterations = 1;
  937. pdemo->m_cutting = true;
  938. }
  939. //
  940. // Clusters
  941. //
  942. //
  943. static void Ctor_Gear(SoftDemo* pdemo, const btVector3& pos, btScalar speed)
  944. {
  945. btTransform startTransform;
  946. startTransform.setIdentity();
  947. startTransform.setOrigin(pos);
  948. btCompoundShape* shape = new btCompoundShape();
  949. #if 1
  950. shape->addChildShape(btTransform(btQuaternion(0, 0, 0)), new btBoxShape(btVector3(5, 1, 6)));
  951. shape->addChildShape(btTransform(btQuaternion(0, 0, SIMD_HALF_PI)), new btBoxShape(btVector3(5, 1, 6)));
  952. #else
  953. shape->addChildShape(btTransform(btQuaternion(0, 0, 0)), new btCylinderShapeZ(btVector3(5, 1, 7)));
  954. shape->addChildShape(btTransform(btQuaternion(0, 0, SIMD_HALF_PI)), new btBoxShape(btVector3(4, 1, 8)));
  955. #endif
  956. btRigidBody* body = pdemo->createRigidBody(10, startTransform, shape);
  957. body->setFriction(1);
  958. btDynamicsWorld* world = pdemo->getDynamicsWorld();
  959. btHingeConstraint* hinge = new btHingeConstraint(*body, btTransform::getIdentity());
  960. if (speed != 0) hinge->enableAngularMotor(true, speed, 3);
  961. world->addConstraint(hinge);
  962. }
  963. //
  964. static btSoftBody* Ctor_ClusterBunny(SoftDemo* pdemo, const btVector3& x, const btVector3& a)
  965. {
  966. btSoftBody* psb = btSoftBodyHelpers::CreateFromTriMesh(pdemo->m_softBodyWorldInfo, gVerticesBunny, &gIndicesBunny[0][0], BUNNY_NUM_TRIANGLES);
  967. btSoftBody::Material* pm = psb->appendMaterial();
  968. pm->m_kLST = 1;
  969. pm->m_flags -= btSoftBody::fMaterial::DebugDraw;
  970. psb->generateBendingConstraints(2, pm);
  971. psb->m_cfg.piterations = 2;
  972. psb->m_cfg.kDF = 1;
  973. psb->m_cfg.collisions = btSoftBody::fCollision::CL_SS +
  974. btSoftBody::fCollision::CL_RS;
  975. psb->randomizeConstraints();
  976. btMatrix3x3 m;
  977. m.setEulerZYX(a.x(), a.y(), a.z());
  978. psb->transform(btTransform(m, x));
  979. psb->scale(btVector3(8, 8, 8));
  980. psb->setTotalMass(150, true);
  981. psb->generateClusters(1);
  982. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  983. return (psb);
  984. }
  985. //
  986. static btSoftBody* Ctor_ClusterTorus(SoftDemo* pdemo, const btVector3& x, const btVector3& a, const btVector3& s = btVector3(2, 2, 2))
  987. {
  988. btSoftBody* psb = btSoftBodyHelpers::CreateFromTriMesh(pdemo->m_softBodyWorldInfo, gVertices, &gIndices[0][0], NUM_TRIANGLES);
  989. btSoftBody::Material* pm = psb->appendMaterial();
  990. pm->m_kLST = 1;
  991. pm->m_flags -= btSoftBody::fMaterial::DebugDraw;
  992. psb->generateBendingConstraints(2, pm);
  993. psb->m_cfg.piterations = 2;
  994. psb->m_cfg.collisions = btSoftBody::fCollision::CL_SS +
  995. btSoftBody::fCollision::CL_RS;
  996. psb->randomizeConstraints();
  997. psb->scale(s);
  998. psb->rotate(btQuaternion(a[0], a[1], a[2]));
  999. psb->translate(x);
  1000. psb->setTotalMass(50, true);
  1001. psb->generateClusters(64);
  1002. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  1003. return (psb);
  1004. }
  1005. //
  1006. static struct MotorControl : btSoftBody::AJoint::IControl
  1007. {
  1008. MotorControl()
  1009. {
  1010. goal = 0;
  1011. maxtorque = 0;
  1012. }
  1013. btScalar Speed(btSoftBody::AJoint*, btScalar current)
  1014. {
  1015. return (current + btMin(maxtorque, btMax(-maxtorque, goal - current)));
  1016. }
  1017. btScalar goal;
  1018. btScalar maxtorque;
  1019. } motorcontrol;
  1020. //
  1021. struct SteerControl : btSoftBody::AJoint::IControl
  1022. {
  1023. SteerControl(btScalar s)
  1024. {
  1025. angle = 0;
  1026. sign = s;
  1027. }
  1028. void Prepare(btSoftBody::AJoint* joint)
  1029. {
  1030. joint->m_refs[0][0] = btCos(angle * sign);
  1031. joint->m_refs[0][2] = btSin(angle * sign);
  1032. }
  1033. btScalar Speed(btSoftBody::AJoint* joint, btScalar current)
  1034. {
  1035. return (motorcontrol.Speed(joint, current));
  1036. }
  1037. btScalar angle;
  1038. btScalar sign;
  1039. };
  1040. static SteerControl steercontrol_f(+1);
  1041. static SteerControl steercontrol_r(-1);
  1042. //
  1043. static void Init_ClusterDeform(SoftDemo* pdemo)
  1044. {
  1045. btSoftBody* psb = Ctor_ClusterTorus(pdemo, btVector3(0, 0, 0), btVector3(SIMD_PI / 2, 0, SIMD_HALF_PI));
  1046. psb->generateClusters(8);
  1047. psb->m_cfg.kDF = 1;
  1048. }
  1049. //
  1050. static void Init_ClusterCollide1(SoftDemo* pdemo)
  1051. {
  1052. const btScalar s = 8;
  1053. btSoftBody* psb = btSoftBodyHelpers::CreatePatch(pdemo->m_softBodyWorldInfo, btVector3(-s, 0, -s),
  1054. btVector3(+s, 0, -s),
  1055. btVector3(-s, 0, +s),
  1056. btVector3(+s, 0, +s),
  1057. 17, 17, //9,9,//31,31,
  1058. 1 + 2 + 4 + 8,
  1059. true);
  1060. btSoftBody::Material* pm = psb->appendMaterial();
  1061. pm->m_kLST = 0.4;
  1062. pm->m_flags -= btSoftBody::fMaterial::DebugDraw;
  1063. psb->m_cfg.kDF = 1;
  1064. psb->m_cfg.kSRHR_CL = 1;
  1065. psb->m_cfg.kSR_SPLT_CL = 0;
  1066. psb->m_cfg.collisions = btSoftBody::fCollision::CL_SS +
  1067. btSoftBody::fCollision::CL_RS;
  1068. psb->generateBendingConstraints(2, pm);
  1069. psb->getCollisionShape()->setMargin(0.05);
  1070. psb->setTotalMass(50);
  1071. ///pass zero in generateClusters to create cluster for each tetrahedron or triangle
  1072. psb->generateClusters(0);
  1073. //psb->generateClusters(64);
  1074. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  1075. Ctor_RbUpStack(pdemo, 10);
  1076. }
  1077. //
  1078. static void Init_ClusterCollide2(SoftDemo* pdemo)
  1079. {
  1080. struct Functor
  1081. {
  1082. static btSoftBody* Create(SoftDemo* pdemo, const btVector3& x, const btVector3& a)
  1083. {
  1084. btSoftBody* psb = btSoftBodyHelpers::CreateFromTriMesh(pdemo->m_softBodyWorldInfo, gVertices,
  1085. &gIndices[0][0],
  1086. NUM_TRIANGLES);
  1087. btSoftBody::Material* pm = psb->appendMaterial();
  1088. pm->m_flags -= btSoftBody::fMaterial::DebugDraw;
  1089. psb->generateBendingConstraints(2, pm);
  1090. psb->m_cfg.piterations = 2;
  1091. psb->m_cfg.kDF = 1;
  1092. psb->m_cfg.kSSHR_CL = 1;
  1093. psb->m_cfg.kSS_SPLT_CL = 0;
  1094. psb->m_cfg.kSKHR_CL = 0.1f;
  1095. psb->m_cfg.kSK_SPLT_CL = 1;
  1096. psb->m_cfg.collisions = btSoftBody::fCollision::CL_SS +
  1097. btSoftBody::fCollision::CL_RS;
  1098. psb->randomizeConstraints();
  1099. btMatrix3x3 m;
  1100. m.setEulerZYX(a.x(), a.y(), a.z());
  1101. psb->transform(btTransform(m, x));
  1102. psb->scale(btVector3(2, 2, 2));
  1103. psb->setTotalMass(50, true);
  1104. psb->generateClusters(16);
  1105. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  1106. return (psb);
  1107. }
  1108. };
  1109. for (int i = 0; i < 3; ++i)
  1110. {
  1111. Functor::Create(pdemo, btVector3(3 * i, 2, 0), btVector3(SIMD_PI / 2 * (1 - (i & 1)), SIMD_PI / 2 * (i & 1), 0));
  1112. }
  1113. }
  1114. //
  1115. static void Init_ClusterSocket(SoftDemo* pdemo)
  1116. {
  1117. btSoftBody* psb = Ctor_ClusterTorus(pdemo, btVector3(0, 0, 0), btVector3(SIMD_PI / 2, 0, SIMD_HALF_PI));
  1118. btRigidBody* prb = Ctor_BigPlate(pdemo, 50, 8);
  1119. psb->m_cfg.kDF = 1;
  1120. btSoftBody::LJoint::Specs lj;
  1121. lj.position = btVector3(0, 5, 0);
  1122. psb->appendLinearJoint(lj, prb);
  1123. }
  1124. //
  1125. static void Init_ClusterHinge(SoftDemo* pdemo)
  1126. {
  1127. btSoftBody* psb = Ctor_ClusterTorus(pdemo, btVector3(0, 0, 0), btVector3(SIMD_PI / 2, 0, SIMD_HALF_PI));
  1128. btRigidBody* prb = Ctor_BigPlate(pdemo, 50, 8);
  1129. psb->m_cfg.kDF = 1;
  1130. btSoftBody::AJoint::Specs aj;
  1131. aj.axis = btVector3(0, 0, 1);
  1132. psb->appendAngularJoint(aj, prb);
  1133. }
  1134. //
  1135. static void Init_ClusterCombine(SoftDemo* pdemo)
  1136. {
  1137. const btVector3 sz(2, 4, 2);
  1138. btSoftBody* psb0 = Ctor_ClusterTorus(pdemo, btVector3(0, 8, 0), btVector3(SIMD_PI / 2, 0, SIMD_HALF_PI), sz);
  1139. btSoftBody* psb1 = Ctor_ClusterTorus(pdemo, btVector3(0, 8, 10), btVector3(SIMD_PI / 2, 0, SIMD_HALF_PI), sz);
  1140. btSoftBody* psbs[] = {psb0, psb1};
  1141. for (int j = 0; j < 2; ++j)
  1142. {
  1143. psbs[j]->m_cfg.kDF = 1;
  1144. psbs[j]->m_cfg.kDP = 0;
  1145. psbs[j]->m_cfg.piterations = 1;
  1146. psbs[j]->m_clusters[0]->m_matching = 0.05;
  1147. psbs[j]->m_clusters[0]->m_ndamping = 0.05;
  1148. }
  1149. btSoftBody::AJoint::Specs aj;
  1150. aj.axis = btVector3(0, 0, 1);
  1151. aj.icontrol = &motorcontrol;
  1152. psb0->appendAngularJoint(aj, psb1);
  1153. btSoftBody::LJoint::Specs lj;
  1154. lj.position = btVector3(0, 8, 5);
  1155. psb0->appendLinearJoint(lj, psb1);
  1156. }
  1157. //
  1158. static void Init_ClusterCar(SoftDemo* pdemo)
  1159. {
  1160. // pdemo->setAzi(180);
  1161. const btVector3 origin(100, 80, 0);
  1162. const btQuaternion orientation(-SIMD_PI / 2, 0, 0);
  1163. const btScalar widthf = 8;
  1164. const btScalar widthr = 9;
  1165. const btScalar length = 8;
  1166. const btScalar height = 4;
  1167. const btVector3 wheels[] = {
  1168. btVector3(+widthf, -height, +length), // Front left
  1169. btVector3(-widthf, -height, +length), // Front right
  1170. btVector3(+widthr, -height, -length), // Rear left
  1171. btVector3(-widthr, -height, -length), // Rear right
  1172. };
  1173. btSoftBody* pa = Ctor_ClusterBunny(pdemo, btVector3(0, 0, 0), btVector3(0, 0, 0));
  1174. btSoftBody* pfl = Ctor_ClusterTorus(pdemo, wheels[0], btVector3(0, 0, SIMD_HALF_PI), btVector3(2, 4, 2));
  1175. btSoftBody* pfr = Ctor_ClusterTorus(pdemo, wheels[1], btVector3(0, 0, SIMD_HALF_PI), btVector3(2, 4, 2));
  1176. btSoftBody* prl = Ctor_ClusterTorus(pdemo, wheels[2], btVector3(0, 0, SIMD_HALF_PI), btVector3(2, 5, 2));
  1177. btSoftBody* prr = Ctor_ClusterTorus(pdemo, wheels[3], btVector3(0, 0, SIMD_HALF_PI), btVector3(2, 5, 2));
  1178. pfl->m_cfg.kDF =
  1179. pfr->m_cfg.kDF =
  1180. prl->m_cfg.kDF =
  1181. prr->m_cfg.kDF = 1;
  1182. btSoftBody::LJoint::Specs lspecs;
  1183. lspecs.cfm = 1;
  1184. lspecs.erp = 1;
  1185. lspecs.position = btVector3(0, 0, 0);
  1186. lspecs.position = wheels[0];
  1187. pa->appendLinearJoint(lspecs, pfl);
  1188. lspecs.position = wheels[1];
  1189. pa->appendLinearJoint(lspecs, pfr);
  1190. lspecs.position = wheels[2];
  1191. pa->appendLinearJoint(lspecs, prl);
  1192. lspecs.position = wheels[3];
  1193. pa->appendLinearJoint(lspecs, prr);
  1194. btSoftBody::AJoint::Specs aspecs;
  1195. aspecs.cfm = 1;
  1196. aspecs.erp = 1;
  1197. aspecs.axis = btVector3(1, 0, 0);
  1198. aspecs.icontrol = &steercontrol_f;
  1199. pa->appendAngularJoint(aspecs, pfl);
  1200. pa->appendAngularJoint(aspecs, pfr);
  1201. aspecs.icontrol = &motorcontrol;
  1202. pa->appendAngularJoint(aspecs, prl);
  1203. pa->appendAngularJoint(aspecs, prr);
  1204. pa->rotate(orientation);
  1205. pfl->rotate(orientation);
  1206. pfr->rotate(orientation);
  1207. prl->rotate(orientation);
  1208. prr->rotate(orientation);
  1209. pa->translate(origin);
  1210. pfl->translate(origin);
  1211. pfr->translate(origin);
  1212. prl->translate(origin);
  1213. prr->translate(origin);
  1214. pfl->m_cfg.piterations =
  1215. pfr->m_cfg.piterations =
  1216. prl->m_cfg.piterations =
  1217. prr->m_cfg.piterations = 1;
  1218. pfl->m_clusters[0]->m_matching =
  1219. pfr->m_clusters[0]->m_matching =
  1220. prl->m_clusters[0]->m_matching =
  1221. prr->m_clusters[0]->m_matching = 0.05;
  1222. pfl->m_clusters[0]->m_ndamping =
  1223. pfr->m_clusters[0]->m_ndamping =
  1224. prl->m_clusters[0]->m_ndamping =
  1225. prr->m_clusters[0]->m_ndamping = 0.05;
  1226. Ctor_LinearStair(pdemo, btVector3(0, -8, 0), btVector3(3, 2, 40), 0, 20);
  1227. Ctor_RbUpStack(pdemo, 50);
  1228. pdemo->m_autocam = true;
  1229. }
  1230. //
  1231. static void Init_ClusterRobot(SoftDemo* pdemo)
  1232. {
  1233. struct Functor
  1234. {
  1235. static btSoftBody* CreateBall(SoftDemo* pdemo, const btVector3& pos)
  1236. {
  1237. btSoftBody* psb = btSoftBodyHelpers::CreateEllipsoid(pdemo->m_softBodyWorldInfo, pos, btVector3(1, 1, 1) * 3, 512);
  1238. psb->m_materials[0]->m_kLST = 0.45;
  1239. psb->m_cfg.kVC = 20;
  1240. psb->setTotalMass(50, true);
  1241. psb->setPose(true, false);
  1242. psb->generateClusters(1);
  1243. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  1244. return (psb);
  1245. }
  1246. };
  1247. const btVector3 base = btVector3(0, 25, 8);
  1248. btSoftBody* psb0 = Functor::CreateBall(pdemo, base + btVector3(-8, 0, 0));
  1249. btSoftBody* psb1 = Functor::CreateBall(pdemo, base + btVector3(+8, 0, 0));
  1250. btSoftBody* psb2 = Functor::CreateBall(pdemo, base + btVector3(0, 0, +8 * btSqrt(2)));
  1251. const btVector3 ctr = (psb0->clusterCom(0) + psb1->clusterCom(0) + psb2->clusterCom(0)) / 3;
  1252. btCylinderShape* pshp = new btCylinderShape(btVector3(8, 1, 8));
  1253. btRigidBody* prb = pdemo->createRigidBody(50, btTransform(btQuaternion(0, 0, 0), ctr + btVector3(0, 5, 0)), pshp);
  1254. btSoftBody::LJoint::Specs ls;
  1255. ls.erp = 0.5f;
  1256. ls.position = psb0->clusterCom(0);
  1257. psb0->appendLinearJoint(ls, prb);
  1258. ls.position = psb1->clusterCom(0);
  1259. psb1->appendLinearJoint(ls, prb);
  1260. ls.position = psb2->clusterCom(0);
  1261. psb2->appendLinearJoint(ls, prb);
  1262. btBoxShape* pbox = new btBoxShape(btVector3(20, 1, 40));
  1263. btRigidBody* pgrn;
  1264. pgrn = pdemo->createRigidBody(0, btTransform(btQuaternion(0, -SIMD_HALF_PI / 2, 0), btVector3(0, 0, 0)), pbox);
  1265. pdemo->m_autocam = true;
  1266. }
  1267. //
  1268. static void Init_ClusterStackSoft(SoftDemo* pdemo)
  1269. {
  1270. for (int i = 0; i < 10; ++i)
  1271. {
  1272. btSoftBody* psb = Ctor_ClusterTorus(pdemo, btVector3(0, -9 + 8.25 * i, 0), btVector3(0, 0, 0));
  1273. psb->m_cfg.kDF = 1;
  1274. }
  1275. }
  1276. //
  1277. static void Init_ClusterStackMixed(SoftDemo* pdemo)
  1278. {
  1279. for (int i = 0; i < 10; ++i)
  1280. {
  1281. if ((i + 1) & 1)
  1282. {
  1283. Ctor_BigPlate(pdemo, 50, -9 + 4.25 * i);
  1284. }
  1285. else
  1286. {
  1287. btSoftBody* psb = Ctor_ClusterTorus(pdemo, btVector3(0, -9 + 4.25 * i, 0), btVector3(0, 0, 0));
  1288. psb->m_cfg.kDF = 1;
  1289. }
  1290. }
  1291. }
  1292. //
  1293. // TetraBunny
  1294. //
  1295. static void Init_TetraBunny(SoftDemo* pdemo)
  1296. {
  1297. btSoftBody* psb = btSoftBodyHelpers::CreateFromTetGenData(pdemo->m_softBodyWorldInfo,
  1298. TetraBunny::getElements(),
  1299. 0,
  1300. TetraBunny::getNodes(),
  1301. false, true, true);
  1302. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  1303. psb->rotate(btQuaternion(SIMD_PI / 2, 0, 0));
  1304. psb->setVolumeMass(150);
  1305. psb->m_cfg.piterations = 2;
  1306. //psb->m_cfg.piterations=1;
  1307. pdemo->m_cutting = false;
  1308. //psb->getCollisionShape()->setMargin(0.01);
  1309. psb->m_cfg.collisions = btSoftBody::fCollision::CL_SS + btSoftBody::fCollision::CL_RS
  1310. //+ btSoftBody::fCollision::CL_SELF
  1311. ;
  1312. ///pass zero in generateClusters to create cluster for each tetrahedron or triangle
  1313. psb->generateClusters(0);
  1314. //psb->m_materials[0]->m_kLST=.2;
  1315. psb->m_cfg.kDF = 10.;
  1316. }
  1317. //
  1318. // TetraCube
  1319. //
  1320. static void Init_TetraCube(SoftDemo* pdemo)
  1321. {
  1322. btSoftBody* psb = btSoftBodyHelpers::CreateFromTetGenData(pdemo->m_softBodyWorldInfo,
  1323. TetraCube::getElements(),
  1324. 0,
  1325. TetraCube::getNodes(),
  1326. false, true, true);
  1327. pdemo->getSoftDynamicsWorld()->addSoftBody(psb);
  1328. psb->scale(btVector3(4, 4, 4));
  1329. psb->translate(btVector3(0, 5, 0));
  1330. psb->setVolumeMass(300);
  1331. ///fix one vertex
  1332. //psb->setMass(0,0);
  1333. //psb->setMass(10,0);
  1334. //psb->setMass(20,0);
  1335. psb->m_cfg.piterations = 1;
  1336. //psb->generateClusters(128);
  1337. psb->generateClusters(16);
  1338. //psb->getCollisionShape()->setMargin(0.5);
  1339. psb->getCollisionShape()->setMargin(0.01);
  1340. psb->m_cfg.collisions = btSoftBody::fCollision::CL_SS + btSoftBody::fCollision::CL_RS
  1341. //+ btSoftBody::fCollision::CL_SELF
  1342. ;
  1343. psb->m_materials[0]->m_kLST = 0.8;
  1344. pdemo->m_cutting = false;
  1345. }
  1346. /* Init */
  1347. void (*demofncs[])(SoftDemo*) =
  1348. {
  1349. Init_Cloth,
  1350. Init_Pressure,
  1351. Init_Volume,
  1352. Init_Ropes,
  1353. Init_RopeAttach,
  1354. Init_ClothAttach,
  1355. Init_Sticks,
  1356. Init_CapsuleCollision,
  1357. Init_Collide,
  1358. Init_Collide2,
  1359. Init_Collide3,
  1360. Init_Impact,
  1361. Init_Aero,
  1362. Init_Aero2,
  1363. Init_Friction,
  1364. Init_Torus,
  1365. Init_TorusMatch,
  1366. Init_Bunny,
  1367. Init_BunnyMatch,
  1368. Init_Cutting1,
  1369. Init_ClusterDeform,
  1370. Init_ClusterCollide1,
  1371. Init_ClusterCollide2,
  1372. Init_ClusterSocket,
  1373. Init_ClusterHinge,
  1374. Init_ClusterCombine,
  1375. Init_ClusterCar,
  1376. Init_ClusterRobot,
  1377. Init_ClusterStackSoft,
  1378. Init_ClusterStackMixed,
  1379. Init_TetraCube,
  1380. Init_TetraBunny,
  1381. };
  1382. #if 0
  1383. void SoftDemo::clientResetScene()
  1384. {
  1385. m_azi = 0;
  1386. m_cameraDistance = 30.f;
  1387. m_cameraTargetPosition.setValue(0,0,0);
  1388. /* Clean up */
  1389. for(int i=m_dynamicsWorld->getNumCollisionObjects()-1;i>=0;i--)
  1390. {
  1391. btCollisionObject* obj=m_dynamicsWorld->getCollisionObjectArray()[i];
  1392. btRigidBody* body=btRigidBody::upcast(obj);
  1393. if(body&&body->getMotionState())
  1394. {
  1395. delete body->getMotionState();
  1396. }
  1397. while(m_dynamicsWorld->getNumConstraints())
  1398. {
  1399. btTypedConstraint* pc=m_dynamicsWorld->getConstraint(0);
  1400. m_dynamicsWorld->removeConstraint(pc);
  1401. delete pc;
  1402. }
  1403. btSoftBody* softBody = btSoftBody::upcast(obj);
  1404. if (softBody)
  1405. {
  1406. getSoftDynamicsWorld()->removeSoftBody(softBody);
  1407. } else
  1408. {
  1409. btRigidBody* body = btRigidBody::upcast(obj);
  1410. if (body)
  1411. m_dynamicsWorld->removeRigidBody(body);
  1412. else
  1413. m_dynamicsWorld->removeCollisionObject(obj);
  1414. }
  1415. delete obj;
  1416. }
  1417. }
  1418. #if 0
  1419. void SoftDemo::clientMoveAndDisplay()
  1420. {
  1421. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  1422. float ms = getDeltaTimeMicroseconds();
  1423. float dt = ms / 1000000.f;//1.0/60.;
  1424. if (m_dynamicsWorld)
  1425. {
  1426. if (sDemoMode)
  1427. {
  1428. static float demoCounter = DEMO_MODE_TIMEOUT;
  1429. demoCounter-= dt;
  1430. if (demoCounter<0)
  1431. {
  1432. demoCounter=DEMO_MODE_TIMEOUT;
  1433. current_demo++;
  1434. current_demo=current_demo%(sizeof(demofncs)/sizeof(demofncs[0]));
  1435. clientResetScene();
  1436. }
  1437. }
  1438. //#define FIXED_STEP
  1439. #ifdef FIXED_STEP
  1440. m_dynamicsWorld->stepSimulation(dt=1.0f/60.f,0);
  1441. #else
  1442. //during idle mode, just run 1 simulation step maximum, otherwise 4 at max
  1443. // int maxSimSubSteps = m_idle ? 1 : 4;
  1444. //if (m_idle)
  1445. // dt = 1.0/420.f;
  1446. int numSimSteps;
  1447. numSimSteps = m_dynamicsWorld->stepSimulation(dt);
  1448. //numSimSteps = m_dynamicsWorld->stepSimulation(dt,10,1./240.f);
  1449. #ifdef VERBOSE_TIMESTEPPING_CONSOLEOUTPUT
  1450. if (!numSimSteps)
  1451. printf("Interpolated transforms\n");
  1452. else
  1453. {
  1454. if (numSimSteps > maxSimSubSteps)
  1455. {
  1456. //detect dropping frames
  1457. printf("Dropped (%i) simulation steps out of %i\n",numSimSteps - maxSimSubSteps,numSimSteps);
  1458. } else
  1459. {
  1460. printf("Simulated (%i) steps\n",numSimSteps);
  1461. }
  1462. }
  1463. #endif //VERBOSE_TIMESTEPPING_CONSOLEOUTPUT
  1464. #endif
  1465. #ifdef USE_AMD_OPENCL
  1466. if (g_openCLSIMDSolver)
  1467. g_openCLSIMDSolver->copyBackToSoftBodies();
  1468. #endif //USE_AMD_OPENCL
  1469. if(m_drag)
  1470. {
  1471. m_node->m_v*=0;
  1472. }
  1473. m_softBodyWorldInfo.m_sparsesdf.GarbageCollect();
  1474. //optional but useful: debug drawing
  1475. }
  1476. #ifdef USE_QUICKPROF
  1477. btProfiler::beginBlock("render");
  1478. #endif //USE_QUICKPROF
  1479. renderme();
  1480. //render the graphics objects, with center of mass shift
  1481. updateCamera();
  1482. #ifdef USE_QUICKPROF
  1483. btProfiler::endBlock("render");
  1484. #endif
  1485. glFlush();
  1486. swapBuffers();
  1487. }
  1488. #endif
  1489. #if 0
  1490. void SoftDemo::renderme()
  1491. {
  1492. btIDebugDraw* idraw=m_dynamicsWorld->getDebugDrawer();
  1493. glDisable(GL_TEXTURE_2D);
  1494. glDisable(GL_LIGHTING);
  1495. m_dynamicsWorld->debugDrawWorld();
  1496. //int debugMode = m_dynamicsWorld->getDebugDrawer()? m_dynamicsWorld->getDebugDrawer()->getDebugMode() : -1;
  1497. btSoftRigidDynamicsWorld* softWorld = (btSoftRigidDynamicsWorld*)m_dynamicsWorld;
  1498. //btIDebugDraw* sdraw = softWorld ->getDebugDrawer();
  1499. for ( int i=0;i<softWorld->getSoftBodyArray().size();i++)
  1500. {
  1501. btSoftBody* psb=(btSoftBody*)softWorld->getSoftBodyArray()[i];
  1502. if (softWorld->getDebugDrawer() && !(softWorld->getDebugDrawer()->getDebugMode() & (btIDebugDraw::DBG_DrawWireframe)))
  1503. {
  1504. btSoftBodyHelpers::DrawFrame(psb,softWorld->getDebugDrawer());
  1505. btSoftBodyHelpers::Draw(psb,softWorld->getDebugDrawer(),softWorld->getDrawFlags());
  1506. }
  1507. }
  1508. /* Bodies */
  1509. btVector3 ps(0,0,0);
  1510. int nps=0;
  1511. btSoftBodyArray& sbs=getSoftDynamicsWorld()->getSoftBodyArray();
  1512. for(int ib=0;ib<sbs.size();++ib)
  1513. {
  1514. btSoftBody* psb=sbs[ib];
  1515. nps+=psb->m_nodes.size();
  1516. for(int i=0;i<psb->m_nodes.size();++i)
  1517. {
  1518. ps+=psb->m_nodes[i].m_x;
  1519. }
  1520. }
  1521. ps/=nps;
  1522. if(m_autocam)
  1523. m_cameraTargetPosition+=(ps-m_cameraTargetPosition)*0.05;
  1524. /* Anm */
  1525. if(!isIdle())
  1526. m_animtime=m_clock.getTimeMilliseconds()/1000.f;
  1527. /* Ray cast */
  1528. if(m_raycast)
  1529. {
  1530. /* Prepare rays */
  1531. const int res=64;
  1532. const btScalar fres=res-1;
  1533. const btScalar size=8;
  1534. const btScalar dist=10;
  1535. btTransform trs;
  1536. trs.setOrigin(ps);
  1537. btScalar rayLength = 1000.f;
  1538. const btScalar angle=m_animtime*0.2;
  1539. trs.setRotation(btQuaternion(angle,SIMD_PI/4,0));
  1540. btVector3 dir=trs.getBasis()*btVector3(0,-1,0);
  1541. trs.setOrigin(ps-dir*dist);
  1542. btAlignedObjectArray<btVector3> origins;
  1543. btAlignedObjectArray<btScalar> fractions;
  1544. origins.resize(res*res);
  1545. fractions.resize(res*res,1.f);
  1546. for(int y=0;y<res;++y)
  1547. {
  1548. for(int x=0;x<res;++x)
  1549. {
  1550. const int idx=y*res+x;
  1551. origins[idx]=trs*btVector3(-size+size*2*x/fres,dist,-size+size*2*y/fres);
  1552. }
  1553. }
  1554. /* Cast rays */
  1555. {
  1556. m_clock.reset();
  1557. if (sbs.size())
  1558. {
  1559. btVector3* org=&origins[0];
  1560. btScalar* fraction=&fractions[0];
  1561. btSoftBody** psbs=&sbs[0];
  1562. btSoftBody::sRayCast results;
  1563. for(int i=0,ni=origins.size(),nb=sbs.size();i<ni;++i)
  1564. {
  1565. for(int ib=0;ib<nb;++ib)
  1566. {
  1567. btVector3 rayFrom = *org;
  1568. btVector3 rayTo = rayFrom+dir*rayLength;
  1569. if(psbs[ib]->rayTest(rayFrom,rayTo,results))
  1570. {
  1571. *fraction=results.fraction;
  1572. }
  1573. }
  1574. ++org;++fraction;
  1575. }
  1576. long ms=btMax<long>(m_clock.getTimeMilliseconds(),1);
  1577. long rayperseconds=(1000*(origins.size()*sbs.size()))/ms;
  1578. printf("%d ms (%d rays/s)\r\n",int(ms),int(rayperseconds));
  1579. }
  1580. }
  1581. /* Draw rays */
  1582. const btVector3 c[]={ origins[0],
  1583. origins[res-1],
  1584. origins[res*(res-1)],
  1585. origins[res*(res-1)+res-1]};
  1586. idraw->drawLine(c[0],c[1],btVector3(0,0,0));
  1587. idraw->drawLine(c[1],c[3],btVector3(0,0,0));
  1588. idraw->drawLine(c[3],c[2],btVector3(0,0,0));
  1589. idraw->drawLine(c[2],c[0],btVector3(0,0,0));
  1590. for(int i=0,ni=origins.size();i<ni;++i)
  1591. {
  1592. const btScalar fraction=fractions[i];
  1593. const btVector3& org=origins[i];
  1594. if(fraction<1.f)
  1595. {
  1596. idraw->drawLine(org,org+dir*rayLength*fraction,btVector3(1,0,0));
  1597. }
  1598. else
  1599. {
  1600. idraw->drawLine(org,org-dir*rayLength*0.1,btVector3(0,0,0));
  1601. }
  1602. }
  1603. #undef RES
  1604. }
  1605. /* Water level */
  1606. static const btVector3 axis[]={btVector3(1,0,0),
  1607. btVector3(0,1,0),
  1608. btVector3(0,0,1)};
  1609. if(m_softBodyWorldInfo.water_density>0)
  1610. {
  1611. const btVector3 c= btVector3((btScalar)0.25,(btScalar)0.25,1);
  1612. const btScalar a= (btScalar)0.5;
  1613. const btVector3 n= m_softBodyWorldInfo.water_normal;
  1614. const btVector3 o= -n*m_softBodyWorldInfo.water_offset;
  1615. const btVector3 x= btCross(n,axis[n.minAxis()]).normalized();
  1616. const btVector3 y= btCross(x,n).normalized();
  1617. const btScalar s= 25;
  1618. idraw->drawTriangle(o-x*s-y*s,o+x*s-y*s,o+x*s+y*s,c,a);
  1619. idraw->drawTriangle(o-x*s-y*s,o+x*s+y*s,o-x*s+y*s,c,a);
  1620. }
  1621. //
  1622. int lineWidth=280;
  1623. int xStart = m_glutScreenWidth - lineWidth;
  1624. int yStart = 20;
  1625. if((getDebugMode() & btIDebugDraw::DBG_NoHelpText)==0)
  1626. {
  1627. setOrthographicProjection();
  1628. glDisable(GL_LIGHTING);
  1629. glColor3f(0, 0, 0);
  1630. char buf[124];
  1631. glRasterPos3f(xStart, yStart, 0);
  1632. if (sDemoMode)
  1633. {
  1634. sprintf(buf,"d to toggle demo mode (on)");
  1635. } else
  1636. {
  1637. sprintf(buf,"d to toggle demo mode (off)");
  1638. }
  1639. GLDebugDrawString(xStart,20,buf);
  1640. glRasterPos3f(xStart, yStart, 0);
  1641. sprintf(buf,"] for next demo (%d)",current_demo);
  1642. yStart+=20;
  1643. GLDebugDrawString(xStart,yStart,buf);
  1644. glRasterPos3f(xStart, yStart, 0);
  1645. sprintf(buf,"c to visualize clusters");
  1646. yStart+=20;
  1647. GLDebugDrawString(xStart,yStart,buf);
  1648. glRasterPos3f(xStart, yStart, 0);
  1649. sprintf(buf,"; to toggle camera mode");
  1650. yStart+=20;
  1651. GLDebugDrawString(xStart,yStart,buf);
  1652. glRasterPos3f(xStart, yStart, 0);
  1653. sprintf(buf,"n,m,l,k for power and steering");
  1654. yStart+=20;
  1655. GLDebugDrawString(xStart,yStart,buf);
  1656. resetPerspectiveProjection();
  1657. glEnable(GL_LIGHTING);
  1658. }
  1659. DemoApplication::renderme();
  1660. }
  1661. #endif
  1662. #endif
  1663. void SoftDemo::setDrawClusters(bool drawClusters)
  1664. {
  1665. if (drawClusters)
  1666. {
  1667. getSoftDynamicsWorld()->setDrawFlags(getSoftDynamicsWorld()->getDrawFlags() | fDrawFlags::Clusters);
  1668. }
  1669. else
  1670. {
  1671. getSoftDynamicsWorld()->setDrawFlags(getSoftDynamicsWorld()->getDrawFlags() & (~fDrawFlags::Clusters));
  1672. }
  1673. }
  1674. #if 0
  1675. void SoftDemo::keyboardCallback(unsigned char key, int x, int y)
  1676. {
  1677. switch(key)
  1678. {
  1679. case 'd': sDemoMode = !sDemoMode; break;
  1680. case 'n': motorcontrol.maxtorque=10;motorcontrol.goal+=1;break;
  1681. case 'm': motorcontrol.maxtorque=10;motorcontrol.goal-=1;break;
  1682. case 'l': steercontrol_f.angle+=0.1;steercontrol_r.angle+=0.1;break;
  1683. case 'k': steercontrol_f.angle-=0.1;steercontrol_r.angle-=0.1;break;
  1684. case ']': ++current_demo;clientResetScene();break;
  1685. case '[': --current_demo;clientResetScene();break;
  1686. case ',': m_raycast=!m_raycast;break;
  1687. case ';': m_autocam=!m_autocam;break;
  1688. case 'c': getSoftDynamicsWorld()->setDrawFlags(getSoftDynamicsWorld()->getDrawFlags()^fDrawFlags::Clusters);break;
  1689. case '`':
  1690. {
  1691. btSoftBodyArray& sbs=getSoftDynamicsWorld()->getSoftBodyArray();
  1692. for(int ib=0;ib<sbs.size();++ib)
  1693. {
  1694. btSoftBody* psb=sbs[ib];
  1695. psb->staticSolve(128);
  1696. }
  1697. }
  1698. break;
  1699. default: DemoApplication::keyboardCallback(key,x,y);
  1700. }
  1701. }
  1702. #endif
  1703. //
  1704. void SoftDemo::mouseMotionFunc(int x, int y)
  1705. {
  1706. if (m_node && (m_results.fraction < 1.f))
  1707. {
  1708. if (!m_drag)
  1709. {
  1710. #define SQ(_x_) (_x_) * (_x_)
  1711. if ((SQ(x - m_lastmousepos[0]) + SQ(y - m_lastmousepos[1])) > 6)
  1712. {
  1713. m_drag = true;
  1714. }
  1715. #undef SQ
  1716. }
  1717. if (m_drag)
  1718. {
  1719. m_lastmousepos[0] = x;
  1720. m_lastmousepos[1] = y;
  1721. }
  1722. }
  1723. }
  1724. #if 0
  1725. //
  1726. void SoftDemo::mouseFunc(int button, int state, int x, int y)
  1727. {
  1728. if(button==0)
  1729. {
  1730. switch(state)
  1731. {
  1732. case 0:
  1733. {
  1734. m_results.fraction=1.f;
  1735. DemoApplication::mouseFunc(button,state,x,y);
  1736. if(!m_pickConstraint)
  1737. {
  1738. const btVector3 rayFrom=m_cameraPosition;
  1739. const btVector3 rayTo=getRayTo(x,y);
  1740. const btVector3 rayDir=(rayTo-rayFrom).normalized();
  1741. btSoftBodyArray& sbs=getSoftDynamicsWorld()->getSoftBodyArray();
  1742. for(int ib=0;ib<sbs.size();++ib)
  1743. {
  1744. btSoftBody* psb=sbs[ib];
  1745. btSoftBody::sRayCast res;
  1746. if(psb->rayTest(rayFrom,rayTo,res))
  1747. {
  1748. m_results=res;
  1749. }
  1750. }
  1751. if(m_results.fraction<1.f)
  1752. {
  1753. m_impact = rayFrom+(rayTo-rayFrom)*m_results.fraction;
  1754. m_drag = m_cutting ? false : true;
  1755. m_lastmousepos[0] = x;
  1756. m_lastmousepos[1] = y;
  1757. m_node = 0;
  1758. switch(m_results.feature)
  1759. {
  1760. case btSoftBody::eFeature::Tetra:
  1761. {
  1762. btSoftBody::Tetra& tet=m_results.body->m_tetras[m_results.index];
  1763. m_node=tet.m_n[0];
  1764. for(int i=1;i<4;++i)
  1765. {
  1766. if( (m_node->m_x-m_impact).length2()>
  1767. (tet.m_n[i]->m_x-m_impact).length2())
  1768. {
  1769. m_node=tet.m_n[i];
  1770. }
  1771. }
  1772. break;
  1773. }
  1774. case btSoftBody::eFeature::Face:
  1775. {
  1776. btSoftBody::Face& f=m_results.body->m_faces[m_results.index];
  1777. m_node=f.m_n[0];
  1778. for(int i=1;i<3;++i)
  1779. {
  1780. if( (m_node->m_x-m_impact).length2()>
  1781. (f.m_n[i]->m_x-m_impact).length2())
  1782. {
  1783. m_node=f.m_n[i];
  1784. }
  1785. }
  1786. }
  1787. break;
  1788. }
  1789. if(m_node) m_goal=m_node->m_x;
  1790. return;
  1791. }
  1792. }
  1793. }
  1794. break;
  1795. case 1:
  1796. if((!m_drag)&&m_cutting&&(m_results.fraction<1.f))
  1797. {
  1798. ImplicitSphere isphere(m_impact,1);
  1799. printf("Mass before: %f\r\n",m_results.body->getTotalMass());
  1800. m_results.body->refine(&isphere,0.0001,true);
  1801. printf("Mass after: %f\r\n",m_results.body->getTotalMass());
  1802. }
  1803. m_results.fraction=1.f;
  1804. m_drag=false;
  1805. DemoApplication::mouseFunc(button,state,x,y);
  1806. break;
  1807. }
  1808. }
  1809. else
  1810. {
  1811. DemoApplication::mouseFunc(button,state,x,y);
  1812. }
  1813. }
  1814. #endif
  1815. void SoftDemo::initPhysics()
  1816. {
  1817. ///create concave ground mesh
  1818. m_guiHelper->setUpAxis(1);
  1819. // m_azi = 0;
  1820. //reset and disable motorcontrol at the start
  1821. motorcontrol.goal = 0;
  1822. motorcontrol.maxtorque = 0;
  1823. btCollisionShape* groundShape = 0;
  1824. {
  1825. int i;
  1826. int j;
  1827. const int NUM_VERTS_X = 30;
  1828. const int NUM_VERTS_Y = 30;
  1829. const int totalVerts = NUM_VERTS_X * NUM_VERTS_Y;
  1830. const int totalTriangles = 2 * (NUM_VERTS_X - 1) * (NUM_VERTS_Y - 1);
  1831. gGroundVertices = new btVector3[totalVerts];
  1832. gGroundIndices = new int[totalTriangles * 3];
  1833. btScalar offset(-50);
  1834. for (i = 0; i < NUM_VERTS_X; i++)
  1835. {
  1836. for (j = 0; j < NUM_VERTS_Y; j++)
  1837. {
  1838. gGroundVertices[i + j * NUM_VERTS_X].setValue((i - NUM_VERTS_X * 0.5f) * TRIANGLE_SIZE,
  1839. //0.f,
  1840. waveheight * sinf((float)i) * cosf((float)j + offset),
  1841. (j - NUM_VERTS_Y * 0.5f) * TRIANGLE_SIZE);
  1842. }
  1843. }
  1844. int vertStride = sizeof(btVector3);
  1845. int indexStride = 3 * sizeof(int);
  1846. int index = 0;
  1847. for (i = 0; i < NUM_VERTS_X - 1; i++)
  1848. {
  1849. for (int j = 0; j < NUM_VERTS_Y - 1; j++)
  1850. {
  1851. gGroundIndices[index++] = j * NUM_VERTS_X + i;
  1852. gGroundIndices[index++] = (j + 1) * NUM_VERTS_X + i + 1;
  1853. gGroundIndices[index++] = j * NUM_VERTS_X + i + 1;
  1854. ;
  1855. gGroundIndices[index++] = j * NUM_VERTS_X + i;
  1856. gGroundIndices[index++] = (j + 1) * NUM_VERTS_X + i;
  1857. gGroundIndices[index++] = (j + 1) * NUM_VERTS_X + i + 1;
  1858. }
  1859. }
  1860. btTriangleIndexVertexArray* indexVertexArrays = new btTriangleIndexVertexArray(totalTriangles,
  1861. gGroundIndices,
  1862. indexStride,
  1863. totalVerts, (btScalar*)&gGroundVertices[0].x(), vertStride);
  1864. bool useQuantizedAabbCompression = true;
  1865. groundShape = new btBvhTriangleMeshShape(indexVertexArrays, useQuantizedAabbCompression);
  1866. groundShape->setMargin(0.5);
  1867. }
  1868. m_collisionShapes.push_back(groundShape);
  1869. btCollisionShape* groundBox = new btBoxShape(btVector3(100, CUBE_HALF_EXTENTS, 100));
  1870. m_collisionShapes.push_back(groundBox);
  1871. btCompoundShape* cylinderCompound = new btCompoundShape;
  1872. btCollisionShape* cylinderShape = new btCylinderShape(btVector3(CUBE_HALF_EXTENTS, CUBE_HALF_EXTENTS, CUBE_HALF_EXTENTS));
  1873. btTransform localTransform;
  1874. localTransform.setIdentity();
  1875. cylinderCompound->addChildShape(localTransform, cylinderShape);
  1876. btQuaternion orn(btVector3(0, 1, 0), SIMD_PI);
  1877. localTransform.setRotation(orn);
  1878. cylinderCompound->addChildShape(localTransform, cylinderShape);
  1879. m_collisionShapes.push_back(cylinderCompound);
  1880. m_dispatcher = 0;
  1881. ///register some softbody collision algorithms on top of the default btDefaultCollisionConfiguration
  1882. m_collisionConfiguration = new btSoftBodyRigidBodyCollisionConfiguration();
  1883. m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
  1884. m_softBodyWorldInfo.m_dispatcher = m_dispatcher;
  1885. ////////////////////////////
  1886. ///Register softbody versus softbody collision algorithm
  1887. ///Register softbody versus rigidbody collision algorithm
  1888. ////////////////////////////
  1889. btVector3 worldAabbMin(-1000, -1000, -1000);
  1890. btVector3 worldAabbMax(1000, 1000, 1000);
  1891. m_broadphase = new btAxisSweep3(worldAabbMin, worldAabbMax, maxProxies);
  1892. m_softBodyWorldInfo.m_broadphase = m_broadphase;
  1893. btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver();
  1894. m_solver = solver;
  1895. btSoftBodySolver* softBodySolver = 0;
  1896. #ifdef USE_AMD_OPENCL
  1897. static bool once = true;
  1898. if (once)
  1899. {
  1900. once = false;
  1901. initCL(0, 0);
  1902. }
  1903. if (g_openCLSIMDSolver)
  1904. delete g_openCLSIMDSolver;
  1905. if (g_softBodyOutput)
  1906. delete g_softBodyOutput;
  1907. if (1)
  1908. {
  1909. g_openCLSIMDSolver = new btOpenCLSoftBodySolverSIMDAware(g_cqCommandQue, g_cxMainContext);
  1910. // g_openCLSIMDSolver = new btOpenCLSoftBodySolver( g_cqCommandQue, g_cxMainContext);
  1911. g_openCLSIMDSolver->setCLFunctions(new CachingCLFunctions(g_cqCommandQue, g_cxMainContext));
  1912. }
  1913. softBodySolver = g_openCLSIMDSolver;
  1914. g_softBodyOutput = new btSoftBodySolverOutputCLtoCPU;
  1915. #endif //USE_AMD_OPENCL
  1916. btDiscreteDynamicsWorld* world = new btSoftRigidDynamicsWorld(m_dispatcher, m_broadphase, m_solver, m_collisionConfiguration, softBodySolver);
  1917. m_dynamicsWorld = world;
  1918. m_dynamicsWorld->setInternalTickCallback(pickingPreTickCallback, this, true);
  1919. m_dynamicsWorld->getDispatchInfo().m_enableSPU = true;
  1920. m_dynamicsWorld->setGravity(btVector3(0, -10, 0));
  1921. m_softBodyWorldInfo.m_gravity.setValue(0, -10, 0);
  1922. m_guiHelper->createPhysicsDebugDrawer(world);
  1923. // clientResetScene();
  1924. m_softBodyWorldInfo.m_sparsesdf.Initialize();
  1925. // clientResetScene();
  1926. //create ground object
  1927. btTransform tr;
  1928. tr.setIdentity();
  1929. tr.setOrigin(btVector3(0, -12, 0));
  1930. btCollisionObject* newOb = new btCollisionObject();
  1931. newOb->setWorldTransform(tr);
  1932. newOb->setInterpolationWorldTransform(tr);
  1933. int lastDemo = (sizeof(demofncs) / sizeof(demofncs[0])) - 1;
  1934. if (current_demo < 0)
  1935. current_demo = lastDemo;
  1936. if (current_demo > lastDemo)
  1937. current_demo = 0;
  1938. if (current_demo > 19)
  1939. {
  1940. newOb->setCollisionShape(m_collisionShapes[0]);
  1941. }
  1942. else
  1943. {
  1944. newOb->setCollisionShape(m_collisionShapes[1]);
  1945. }
  1946. m_dynamicsWorld->addCollisionObject(newOb);
  1947. m_softBodyWorldInfo.m_sparsesdf.Reset();
  1948. motorcontrol.goal = 0;
  1949. motorcontrol.maxtorque = 0;
  1950. m_softBodyWorldInfo.air_density = (btScalar)1.2;
  1951. m_softBodyWorldInfo.water_density = 0;
  1952. m_softBodyWorldInfo.water_offset = 0;
  1953. m_softBodyWorldInfo.water_normal = btVector3(0, 0, 0);
  1954. m_softBodyWorldInfo.m_gravity.setValue(0, -10, 0);
  1955. m_autocam = false;
  1956. m_raycast = false;
  1957. m_cutting = false;
  1958. m_results.fraction = 1.f;
  1959. demofncs[current_demo](this);
  1960. m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
  1961. }
  1962. void SoftDemo::exitPhysics()
  1963. {
  1964. //cleanup in the reverse order of creation/initialization
  1965. //remove the rigidbodies from the dynamics world and delete them
  1966. int i;
  1967. for (i = m_dynamicsWorld->getNumCollisionObjects() - 1; i >= 0; i--)
  1968. {
  1969. btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i];
  1970. btRigidBody* body = btRigidBody::upcast(obj);
  1971. if (body && body->getMotionState())
  1972. {
  1973. delete body->getMotionState();
  1974. }
  1975. m_dynamicsWorld->removeCollisionObject(obj);
  1976. delete obj;
  1977. }
  1978. //delete collision shapes
  1979. for (int j = 0; j < m_collisionShapes.size(); j++)
  1980. {
  1981. btCollisionShape* shape = m_collisionShapes[j];
  1982. m_collisionShapes[j] = 0;
  1983. delete shape;
  1984. }
  1985. //delete dynamics world
  1986. delete m_dynamicsWorld;
  1987. m_dynamicsWorld = 0;
  1988. //delete solver
  1989. delete m_solver;
  1990. //delete broadphase
  1991. delete m_broadphase;
  1992. //delete dispatcher
  1993. delete m_dispatcher;
  1994. delete m_collisionConfiguration;
  1995. }
  1996. class CommonExampleInterface* SoftDemoCreateFunc(struct CommonExampleOptions& options)
  1997. {
  1998. current_demo = options.m_option;
  1999. return new SoftDemo(options.m_guiHelper);
  2000. }