SoftDemo.cpp 59 KB

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