demo_moving_convex.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*************************************************************************
  2. * *
  3. * Open Dynamics Engine, Copyright (C) 2001-2003 Russell L. Smith. *
  4. * All rights reserved. Email: [email protected] Web: www.q12.org *
  5. * *
  6. * This library is free software; you can redistribute it and/or *
  7. * modify it under the terms of EITHER: *
  8. * (1) The GNU Lesser General Public License as published by the Free *
  9. * Software Foundation; either version 2.1 of the License, or (at *
  10. * your option) any later version. The text of the GNU Lesser *
  11. * General Public License is included with this library in the *
  12. * file LICENSE.TXT. *
  13. * (2) The BSD-style license that is included with this library in *
  14. * the file LICENSE-BSD.TXT. *
  15. * *
  16. * This library is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
  19. * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
  20. * *
  21. *************************************************************************/
  22. #include <ode/ode.h>
  23. #include <drawstuff/drawstuff.h>
  24. #include "texturepath.h"
  25. #include "bunny_geom.h"
  26. #include "convex_bunny_geom.h"
  27. #ifdef _MSC_VER
  28. #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
  29. #endif
  30. // select correct drawing functions
  31. #ifdef dDOUBLE
  32. #define dsDrawBox dsDrawBoxD
  33. #define dsDrawSphere dsDrawSphereD
  34. #define dsDrawCylinder dsDrawCylinderD
  35. #define dsDrawCapsule dsDrawCapsuleD
  36. #define dsDrawLine dsDrawLineD
  37. #define dsDrawTriangle dsDrawTriangleD
  38. #define dsDrawConvex dsDrawConvexD
  39. #endif
  40. // some constants
  41. #define NUM 200 // max number of objects
  42. #define DENSITY (5.0) // density of all objects
  43. #define GPB 3 // maximum number of geometries per body
  44. #define MAX_CONTACTS 64 // maximum number of contact points per body
  45. // dynamics and collision objects
  46. struct MyObject
  47. {
  48. dBodyID body; // the body
  49. dGeomID geom[GPB]; // geometries representing this body
  50. };
  51. static int num=0; // number of objects in simulation
  52. static int nextobj=0; // next object to recycle if num==NUM
  53. static dWorldID world;
  54. static dSpaceID space;
  55. static MyObject obj[NUM];
  56. static dJointGroupID contactgroup;
  57. static int selected = -1; // selected object
  58. static int show_aabb = 0; // show geom AABBs?
  59. static int show_contacts = 0; // show contact points?
  60. static int random_pos = 1; // drop objects from random position?
  61. typedef dReal dVector3R[3];
  62. // this is called by dSpaceCollide when two objects in space are
  63. // potentially colliding.
  64. static void nearCallback( void *, dGeomID o1, dGeomID o2 )
  65. {
  66. int i;
  67. // if (o1->body && o2->body) return;
  68. // exit without doing anything if the two bodies are connected by a joint
  69. dBodyID b1 = dGeomGetBody( o1 );
  70. dBodyID b2 = dGeomGetBody( o2 );
  71. if ( b1 && b2 && dAreConnectedExcluding( b1,b2,dJointTypeContact ) ) return;
  72. dContact contact[MAX_CONTACTS]; // up to MAX_CONTACTS contacts per box-box
  73. for ( i=0; i<MAX_CONTACTS; i++ )
  74. {
  75. contact[i].surface.mode = dContactBounce | dContactSoftCFM;
  76. contact[i].surface.mu = dInfinity;
  77. contact[i].surface.mu2 = 0;
  78. contact[i].surface.bounce = 0.1;
  79. contact[i].surface.bounce_vel = 0.1;
  80. contact[i].surface.soft_cfm = 0.01;
  81. }
  82. if ( int numc = dCollide( o1,o2,MAX_CONTACTS,&contact[0].geom,
  83. sizeof( dContact ) ) )
  84. {
  85. dMatrix3 RI;
  86. dRSetIdentity( RI );
  87. const dReal ss[3] = {0.02,0.02,0.02};
  88. for ( i=0; i<numc; i++ )
  89. {
  90. dJointID c = dJointCreateContact( world,contactgroup,contact+i );
  91. dJointAttach( c,b1,b2 );
  92. if ( show_contacts ) dsDrawBox( contact[i].geom.pos,RI,ss );
  93. }
  94. }
  95. }
  96. // start simulation - set viewpoint
  97. static void start()
  98. {
  99. dAllocateODEDataForThread( dAllocateMaskAll );
  100. static float xyz[3] = {2.1640f,-1.3079f,1.7600f};
  101. static float hpr[3] = {125.5000f,-17.0000f,0.0000f};
  102. dsSetViewpoint( xyz,hpr );
  103. printf( "To drop another object, press:\n" );
  104. printf( " b for box.\n" );
  105. printf( " s for sphere.\n" );
  106. printf( " c for capsule.\n" );
  107. printf( " v for a convex.\n" );
  108. printf( "To select an object, press space.\n" );
  109. printf( "To disable the selected object, press d.\n" );
  110. printf( "To enable the selected object, press e.\n" );
  111. printf( "To toggle showing the geom AABBs, press a.\n" );
  112. printf( "To toggle showing the contact points, press t.\n" );
  113. printf( "To toggle dropping from random position/orientation, press r.\n" );
  114. }
  115. char locase( char c )
  116. {
  117. if ( c >= 'A' && c <= 'Z' ) return c - ( 'a'-'A' );
  118. else return c;
  119. }
  120. // called when a key pressed
  121. static void command( int cmd )
  122. {
  123. int i,k;
  124. dReal sides[3];
  125. dMass m;
  126. cmd = locase( cmd );
  127. if ( cmd == 'v' || cmd == 'b' || cmd == 'c' || cmd == 's' || cmd == 'y')
  128. {
  129. if ( num < NUM )
  130. {
  131. i = num;
  132. num++;
  133. }
  134. else
  135. {
  136. i = nextobj;
  137. nextobj++;
  138. if ( nextobj >= num ) nextobj = 0;
  139. // destroy the body and geoms for slot i
  140. dBodyDestroy( obj[i].body );
  141. for ( k=0; k < GPB; k++ )
  142. {
  143. if ( obj[i].geom[k] ) dGeomDestroy( obj[i].geom[k] );
  144. }
  145. memset( &obj[i],0,sizeof( obj[i] ) );
  146. }
  147. obj[i].body = dBodyCreate( world );
  148. for ( k=0; k<3; k++ ) sides[k] = dRandReal()*0.5+0.1;
  149. dMatrix3 R;
  150. if ( random_pos )
  151. {
  152. dBodySetPosition( obj[i].body,
  153. dRandReal()*2-1,dRandReal()*2-1,dRandReal()+3 );
  154. dRFromAxisAndAngle( R,dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
  155. dRandReal()*2.0-1.0,dRandReal()*10.0-5.0 );
  156. }
  157. else
  158. {
  159. dReal maxheight = 0;
  160. for ( k=0; k<num; k++ )
  161. {
  162. const dReal *pos = dBodyGetPosition( obj[k].body );
  163. if ( pos[2] > maxheight ) maxheight = pos[2];
  164. }
  165. dBodySetPosition( obj[i].body, 0,0,maxheight+1 );
  166. dRFromAxisAndAngle( R,0,0,1,dRandReal()*10.0-5.0 );
  167. }
  168. dBodySetRotation( obj[i].body,R );
  169. dBodySetData( obj[i].body,( void* )( size_t )i );
  170. if ( cmd == 'b' )
  171. {
  172. dMassSetBox( &m,DENSITY,sides[0],sides[1],sides[2] );
  173. obj[i].geom[0] = dCreateBox( space,sides[0],sides[1],sides[2] );
  174. }
  175. else if ( cmd == 'c' )
  176. {
  177. sides[0] *= 0.5;
  178. dMassSetCapsule( &m,DENSITY,3,sides[0],sides[1] );
  179. obj[i].geom[0] = dCreateCapsule( space,sides[0],sides[1] );
  180. }
  181. else if (cmd == 'y') {
  182. dMassSetCylinder (&m,DENSITY,3,sides[0],sides[1]);
  183. obj[i].geom[0] = dCreateCylinder (space,sides[0],sides[1]);
  184. }
  185. else if ( cmd == 's' )
  186. {
  187. sides[0] *= 0.5;
  188. dMassSetSphere( &m,DENSITY,sides[0] );
  189. obj[i].geom[0] = dCreateSphere( space,sides[0] );
  190. }
  191. else if ( cmd == 'v' )
  192. {
  193. obj[i].geom[0] = dCreateConvex( space,
  194. convexBunnyPlanes,
  195. convexBunnyPlaneCount,
  196. convexBunnyPoints,
  197. convexBunnyPointCount,
  198. convexBunnyPolygons );
  199. /// Use equivalent TriMesh to set mass
  200. dTriMeshDataID new_tmdata = dGeomTriMeshDataCreate();
  201. dGeomTriMeshDataBuildSingle( new_tmdata, &Vertices[0], 3 * sizeof( float ), VertexCount,
  202. ( dTriIndex* )&Indices[0], IndexCount, 3 * sizeof( dTriIndex ) );
  203. dGeomTriMeshDataPreprocess2( new_tmdata, (1U << dTRIDATAPREPROCESS_BUILD_FACE_ANGLES), NULL );
  204. dGeomID triMesh = dCreateTriMesh( 0, new_tmdata, 0, 0, 0 );
  205. dMassSetTrimesh( &m, DENSITY, triMesh );
  206. dGeomDestroy( triMesh );
  207. dGeomTriMeshDataDestroy( new_tmdata );
  208. printf( "mass at %f %f %f\n", m.c[0], m.c[1], m.c[2] );
  209. dGeomSetPosition( obj[i].geom[0], -m.c[0], -m.c[1], -m.c[2] );
  210. dMassTranslate( &m, -m.c[0], -m.c[1], -m.c[2] );
  211. }
  212. for ( k=0; k < GPB; k++ )
  213. {
  214. if ( obj[i].geom[k] ) dGeomSetBody( obj[i].geom[k],obj[i].body );
  215. }
  216. dBodySetMass( obj[i].body,&m );
  217. }
  218. if ( cmd == ' ' )
  219. {
  220. selected++;
  221. if ( selected >= num ) selected = 0;
  222. if ( selected < 0 ) selected = 0;
  223. }
  224. else if ( cmd == 'd' && selected >= 0 && selected < num )
  225. {
  226. dBodyDisable( obj[selected].body );
  227. }
  228. else if ( cmd == 'e' && selected >= 0 && selected < num )
  229. {
  230. dBodyEnable( obj[selected].body );
  231. }
  232. else if ( cmd == 'a' )
  233. {
  234. show_aabb ^= 1;
  235. }
  236. else if ( cmd == 't' )
  237. {
  238. show_contacts ^= 1;
  239. }
  240. else if ( cmd == 'r' )
  241. {
  242. random_pos ^= 1;
  243. }
  244. }
  245. // draw a geom
  246. void drawGeom( dGeomID g, const dReal *pos, const dReal *R, int show_aabb )
  247. {
  248. if ( !g ) return;
  249. if ( !pos ) pos = dGeomGetPosition( g );
  250. if ( !R ) R = dGeomGetRotation( g );
  251. int type = dGeomGetClass( g );
  252. if ( type == dBoxClass )
  253. {
  254. dVector3 sides;
  255. dGeomBoxGetLengths( g,sides );
  256. dsDrawBox( pos,R,sides );
  257. }
  258. else if ( type == dSphereClass )
  259. {
  260. dsDrawSphere( pos,R,dGeomSphereGetRadius( g ) );
  261. }
  262. else if (type == dCylinderClass) {
  263. dReal radius,length;
  264. dGeomCylinderGetParams (g,&radius,&length);
  265. dsDrawCylinder (pos,R,length,radius);
  266. }
  267. else if ( type == dCapsuleClass )
  268. {
  269. dReal radius,length;
  270. dGeomCapsuleGetParams( g,&radius,&length );
  271. dsDrawCapsule( pos,R,length,radius );
  272. }
  273. else if ( type == dConvexClass )
  274. {
  275. dsDrawConvex( pos,R,
  276. convexBunnyPlanes,
  277. convexBunnyPlaneCount,
  278. convexBunnyPoints,
  279. convexBunnyPointCount,
  280. convexBunnyPolygons );
  281. }
  282. if ( show_aabb )
  283. {
  284. // draw the bounding box for this geom
  285. dReal aabb[6];
  286. dGeomGetAABB( g,aabb );
  287. dVector3 bbpos;
  288. for ( int i=0; i<3; i++ ) bbpos[i] = 0.5*( aabb[i*2] + aabb[i*2+1] );
  289. dVector3 bbsides;
  290. for ( int j=0; j<3; j++ ) bbsides[j] = aabb[j*2+1] - aabb[j*2];
  291. dMatrix3 RI;
  292. dRSetIdentity( RI );
  293. dsSetColorAlpha( 1,0,0,0.5 );
  294. dsDrawBox( bbpos,RI,bbsides );
  295. }
  296. }
  297. // simulation loop
  298. static void simLoop( int pause )
  299. {
  300. dsSetColor( 0,0,2 );
  301. dSpaceCollide( space,0,&nearCallback );
  302. if ( !pause ) dWorldQuickStep( world,0.05 );
  303. for ( int j = 0; j < dSpaceGetNumGeoms( space ); j++ )
  304. {
  305. dSpaceGetGeom( space, j );
  306. }
  307. // remove all contact joints
  308. dJointGroupEmpty( contactgroup );
  309. dsSetColor( 1,1,0 );
  310. dsSetTexture( DS_WOOD );
  311. for ( int i=0; i<num; i++ )
  312. {
  313. for ( int j=0; j < GPB; j++ )
  314. {
  315. if ( obj[i].geom[j] )
  316. {
  317. if ( i==selected )
  318. {
  319. dsSetColor( 0,0.7,1 );
  320. }
  321. else if ( ! dBodyIsEnabled( obj[i].body ) )
  322. {
  323. dsSetColor( 1,0,0 );
  324. }
  325. else
  326. {
  327. dsSetColor( 1,1,0 );
  328. }
  329. drawGeom( obj[i].geom[j],0,0,show_aabb );
  330. }
  331. }
  332. }
  333. }
  334. int main( int argc, char **argv )
  335. {
  336. // setup pointers to drawstuff callback functions
  337. dsFunctions fn;
  338. fn.version = DS_VERSION;
  339. fn.start = &start;
  340. fn.step = &simLoop;
  341. fn.command = &command;
  342. fn.stop = 0;
  343. fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
  344. // create world
  345. dInitODE2( 0 );
  346. world = dWorldCreate();
  347. space = dSimpleSpaceCreate( 0 );
  348. contactgroup = dJointGroupCreate( 0 );
  349. dWorldSetGravity( world,0,0,-0.5 );
  350. dWorldSetCFM( world,1e-5 );
  351. dCreatePlane( space,0,0,1,0 );
  352. memset( obj,0,sizeof( obj ) );
  353. // run simulation
  354. dsSimulationLoop( argc,argv,352,288,&fn );
  355. dJointGroupDestroy( contactgroup );
  356. dSpaceDestroy( space );
  357. dWorldDestroy( world );
  358. dCloseODE();
  359. return 0;
  360. }