demo_motion.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. This demo shows how to use dContactMotionN in a lifting platform.
  3. */
  4. //#include <unistd.h> // for usleep()
  5. #include <ode/ode.h>
  6. #include <drawstuff/drawstuff.h>
  7. #include "texturepath.h"
  8. #ifdef _MSC_VER
  9. #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
  10. #endif
  11. // select correct drawing functions
  12. #ifdef dDOUBLE
  13. #define dsDrawBox dsDrawBoxD
  14. #define dsDrawSphere dsDrawSphereD
  15. #define dsDrawCylinder dsDrawCylinderD
  16. #define dsDrawCapsule dsDrawCapsuleD
  17. #define dsDrawConvex dsDrawConvexD
  18. #endif
  19. // some constants
  20. #define NUM 100 // max number of objects
  21. #define DENSITY (5.0) // density of all objects
  22. #define GPB 3 // maximum number of geometries per body
  23. #define MAX_CONTACTS 8 // maximum number of contact points per body
  24. #define USE_GEOM_OFFSET 1
  25. // dynamics and collision objects
  26. struct MyObject {
  27. dBodyID body; // the body
  28. dGeomID geom[GPB]; // geometries representing this body
  29. };
  30. static int num=0; // number of objects in simulation
  31. static int nextobj=0; // next object to recycle if num==NUM
  32. static dWorldID world;
  33. static dSpaceID space;
  34. static MyObject obj[NUM];
  35. static dJointGroupID contactgroup;
  36. static int show_aabb = 0; // show geom AABBs?
  37. static int show_contacts = 0; // show contact points?
  38. static int random_pos = 1; // drop objects from random position?
  39. static int write_world = 0;
  40. static int show_body = 0;
  41. static dGeomID platform, ground;
  42. dVector3 platpos = {0, 0, 0};
  43. int mov_type = 2;
  44. dReal mov_time = 0;
  45. const dReal mov1_speed = 0.2;
  46. dVector3 mov2_vel = { 0.2, 0.1, 0.25};
  47. /****************************************************************
  48. * Movement 1: move platform up, reset every 80 units of time. *
  49. * This is the simplest case *
  50. ****************************************************************/
  51. static void moveplat_1(dReal stepsize)
  52. {
  53. mov_time += stepsize;
  54. if (mov_time > 80)
  55. mov_time = 0;
  56. platpos[0] = platpos[1] = 0;
  57. // the platform moves up (Z) at constant speed: mov1_speed
  58. platpos[2] = mov1_speed * mov_time;
  59. }
  60. // Generate contact info for movement 1
  61. static void contactplat_1(dContact &contact)
  62. {
  63. contact.surface.mode |= dContactMotionN;
  64. contact.surface.motionN = mov1_speed;
  65. }
  66. /****************************************************************
  67. * Movement 2: move platform along direction mov2_vel, reset *
  68. * every 80 units of time. *
  69. * This is the most general case: the geom moves along *
  70. * an arbitrary direction. *
  71. ****************************************************************/
  72. static void moveplat_2(dReal stepsize)
  73. {
  74. mov_time += stepsize;
  75. if (mov_time > 80)
  76. mov_time = 0;
  77. // the platform moves at constant speed: mov2_speed
  78. platpos[0] = mov2_vel[0] * mov_time;
  79. platpos[1] = mov2_vel[1] * mov_time;
  80. platpos[2] = mov2_vel[2] * mov_time;
  81. }
  82. // Generate contact info for movement 1
  83. static void contactplat_2(dContact &contact)
  84. {
  85. /*
  86. For arbitrary contact directions we need to project the moving
  87. geom's velocity against the contact normal and fdir1, fdir2
  88. (obtained with dPlaneSpace()). Assuming moving geom=g2
  89. (so the contact joint is in the moving geom's reference frame):
  90. motion1 = dCalcVectorDot3(fdir1, vel);
  91. motion2 = dCalcVectorDot3(fdir2, vel);
  92. motionN = dCalcVectorDot3(normal, vel);
  93. For geom=g1 just negate motionN and motion2. fdir1 is an arbitrary
  94. vector, so there's no need to negate motion1.
  95. */
  96. contact.surface.mode |=
  97. dContactMotionN | // velocity along normal
  98. dContactMotion1 | dContactMotion2 | // and along the contact plane
  99. dContactFDir1; // don't forget to set the direction 1
  100. // This is a convenience function: given a vector, it finds other 2 perpendicular vectors
  101. dVector3 motiondir1, motiondir2;
  102. dPlaneSpace(contact.geom.normal, motiondir1, motiondir2);
  103. for (int i=0; i<3; ++i)
  104. contact.fdir1[i] = motiondir1[i];
  105. dReal inv = 1;
  106. if (contact.geom.g1 == platform)
  107. inv = -1;
  108. contact.surface.motion1 = dCalcVectorDot3(mov2_vel, motiondir1);
  109. contact.surface.motion2 = inv * dCalcVectorDot3(mov2_vel, motiondir2);
  110. contact.surface.motionN = inv * dCalcVectorDot3(mov2_vel, contact.geom.normal);
  111. }
  112. static void nearCallback (void *data, dGeomID o1, dGeomID o2)
  113. {
  114. dMatrix3 RI;
  115. static const dReal ss[3] = {0.02,0.02,0.02};
  116. dContact contact[MAX_CONTACTS];
  117. int numc = dCollide (o1, o2, MAX_CONTACTS,
  118. &contact[0].geom, sizeof(dContact));
  119. if (numc)
  120. dRSetIdentity(RI);
  121. bool isplatform = (o1 == platform) || (o2 == platform);
  122. for (int i=0; i< numc; i++) {
  123. contact[i].surface.mode = dContactBounce;
  124. contact[i].surface.mu = 1;
  125. contact[i].surface.bounce = 0.25;
  126. contact[i].surface.bounce_vel = 0.01;
  127. if (isplatform) {
  128. switch (mov_type) {
  129. case 1:
  130. contactplat_1(contact[i]);
  131. break;
  132. case 2:
  133. contactplat_2(contact[i]);
  134. break;
  135. }
  136. }
  137. dJointID c = dJointCreateContact (world,contactgroup,contact+i);
  138. dJointAttach (c, dGeomGetBody(o1), dGeomGetBody(o2));
  139. if (show_contacts)
  140. dsDrawBox (contact[i].geom.pos, RI, ss);
  141. }
  142. }
  143. // start simulation - set viewpoint
  144. static float xyz[3] = {2.1106f,-1.3007,2.f};
  145. static float hpr[3] = {150.f,-13.5000f,0.0000f};
  146. static void start()
  147. {
  148. //dAllocateODEDataForThread(dAllocateMaskAll);
  149. dsSetViewpoint (xyz,hpr);
  150. printf ("To drop another object, press:\n");
  151. printf (" b for box.\n");
  152. printf (" s for sphere.\n");
  153. printf (" c for capsule.\n");
  154. printf (" y for cylinder.\n");
  155. printf ("Press m to change the movement type\n");
  156. printf ("Press space to reset the platform\n");
  157. printf ("To toggle showing the geom AABBs, press a.\n");
  158. printf ("To toggle showing the contact points, press t.\n");
  159. printf ("To toggle dropping from random position/orientation, press r.\n");
  160. printf ("To save the current state to 'state.dif', press 1.\n");
  161. }
  162. char locase (char c)
  163. {
  164. if (c >= 'A' && c <= 'Z') return c - ('a'-'A');
  165. else return c;
  166. }
  167. // called when a key pressed
  168. static void command (int cmd)
  169. {
  170. size_t i;
  171. int k;
  172. dReal sides[3];
  173. dMass m;
  174. int setBody;
  175. cmd = locase (cmd);
  176. if (cmd == 'b' || cmd == 's' || cmd == 'c' || cmd == 'y')
  177. {
  178. setBody = 0;
  179. if (num < NUM) {
  180. i = num;
  181. num++;
  182. }
  183. else {
  184. i = nextobj;
  185. nextobj++;
  186. if (nextobj >= num) nextobj = 0;
  187. // destroy the body and geoms for slot i
  188. if (obj[i].body) {
  189. dBodyDestroy (obj[i].body);
  190. }
  191. for (k=0; k < GPB; k++) {
  192. if (obj[i].geom[k]) {
  193. dGeomDestroy (obj[i].geom[k]);
  194. }
  195. }
  196. memset (&obj[i],0,sizeof(obj[i]));
  197. }
  198. obj[i].body = dBodyCreate (world);
  199. for (k=0; k<3; k++) sides[k] = dRandReal()*0.5+0.1;
  200. dMatrix3 R;
  201. if (random_pos)
  202. {
  203. dBodySetPosition (obj[i].body,
  204. dRandReal()*2-1 + platpos[0],
  205. dRandReal()*2-1 + platpos[1],
  206. dRandReal()+2 + platpos[2]);
  207. dRFromAxisAndAngle (R,dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
  208. dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
  209. }
  210. else
  211. {
  212. dBodySetPosition (obj[i].body,
  213. platpos[0],
  214. platpos[1],
  215. platpos[2]+2);
  216. dRSetIdentity (R);
  217. }
  218. dBodySetRotation (obj[i].body,R);
  219. dBodySetData (obj[i].body,(void*) i);
  220. if (cmd == 'b') {
  221. dMassSetBox (&m,DENSITY,sides[0],sides[1],sides[2]);
  222. obj[i].geom[0] = dCreateBox (space,sides[0],sides[1],sides[2]);
  223. }
  224. else if (cmd == 'c') {
  225. sides[0] *= 0.5;
  226. dMassSetCapsule (&m,DENSITY,3,sides[0],sides[1]);
  227. obj[i].geom[0] = dCreateCapsule (space,sides[0],sides[1]);
  228. }
  229. else if (cmd == 'y') {
  230. dMassSetCylinder (&m,DENSITY,3,sides[0],sides[1]);
  231. obj[i].geom[0] = dCreateCylinder (space,sides[0],sides[1]);
  232. }
  233. else if (cmd == 's') {
  234. sides[0] *= 0.5;
  235. dMassSetSphere (&m,DENSITY,sides[0]);
  236. obj[i].geom[0] = dCreateSphere (space,sides[0]);
  237. }
  238. if (!setBody)
  239. for (k=0; k < GPB; k++) {
  240. if (obj[i].geom[k]) {
  241. dGeomSetBody (obj[i].geom[k],obj[i].body);
  242. }
  243. }
  244. dBodySetMass (obj[i].body,&m);
  245. }
  246. else if (cmd == 'a') {
  247. show_aabb ^= 1;
  248. }
  249. else if (cmd == 't') {
  250. show_contacts ^= 1;
  251. }
  252. else if (cmd == 'r') {
  253. random_pos ^= 1;
  254. }
  255. else if (cmd == '1') {
  256. write_world = 1;
  257. }
  258. else if (cmd == ' ') {
  259. mov_time = 0;
  260. }
  261. else if (cmd == 'm') {
  262. mov_type = mov_type==1 ? 2 : 1;
  263. mov_time = 0;
  264. }
  265. }
  266. // draw a geom
  267. void drawGeom (dGeomID g, const dReal *pos, const dReal *R, int show_aabb)
  268. {
  269. int i;
  270. if (!g) return;
  271. if (!pos) pos = dGeomGetPosition (g);
  272. if (!R) R = dGeomGetRotation (g);
  273. int type = dGeomGetClass (g);
  274. if (type == dBoxClass) {
  275. dVector3 sides;
  276. dGeomBoxGetLengths (g,sides);
  277. dsDrawBox (pos,R,sides);
  278. }
  279. else if (type == dSphereClass) {
  280. dsDrawSphere (pos,R,dGeomSphereGetRadius (g));
  281. }
  282. else if (type == dCapsuleClass) {
  283. dReal radius,length;
  284. dGeomCapsuleGetParams (g,&radius,&length);
  285. dsDrawCapsule (pos,R,length,radius);
  286. }
  287. else if (type == dCylinderClass) {
  288. dReal radius,length;
  289. dGeomCylinderGetParams (g,&radius,&length);
  290. dsDrawCylinder (pos,R,length,radius);
  291. }
  292. else if (type == dGeomTransformClass) {
  293. dGeomID g2 = dGeomTransformGetGeom (g);
  294. const dReal *pos2 = dGeomGetPosition (g2);
  295. const dReal *R2 = dGeomGetRotation (g2);
  296. dVector3 actual_pos;
  297. dMatrix3 actual_R;
  298. dMultiply0_331 (actual_pos,R,pos2);
  299. actual_pos[0] += pos[0];
  300. actual_pos[1] += pos[1];
  301. actual_pos[2] += pos[2];
  302. dMultiply0_333 (actual_R,R,R2);
  303. drawGeom (g2,actual_pos,actual_R,0);
  304. }
  305. if (show_body) {
  306. dBodyID body = dGeomGetBody(g);
  307. if (body) {
  308. const dReal *bodypos = dBodyGetPosition (body);
  309. const dReal *bodyr = dBodyGetRotation (body);
  310. dReal bodySides[3] = { 0.1, 0.1, 0.1 };
  311. dsSetColorAlpha(0,1,0,1);
  312. dsDrawBox(bodypos,bodyr,bodySides);
  313. }
  314. }
  315. if (show_aabb) {
  316. // draw the bounding box for this geom
  317. dReal aabb[6];
  318. dGeomGetAABB (g,aabb);
  319. dVector3 bbpos;
  320. for (i=0; i<3; i++) bbpos[i] = 0.5*(aabb[i*2] + aabb[i*2+1]);
  321. dVector3 bbsides;
  322. for (i=0; i<3; i++) bbsides[i] = aabb[i*2+1] - aabb[i*2];
  323. dMatrix3 RI;
  324. dRSetIdentity (RI);
  325. dsSetColorAlpha (1,0,0,0.5);
  326. dsDrawBox (bbpos,RI,bbsides);
  327. }
  328. }
  329. // simulation loop
  330. static void updatecam()
  331. {
  332. xyz[0] = platpos[0] + 3.3;
  333. xyz[1] = platpos[1] - 1.8;
  334. xyz[2] = platpos[2] + 2;
  335. dsSetViewpoint (xyz, hpr);
  336. }
  337. static void simLoop (int pause)
  338. {
  339. const dReal stepsize = 0.02;
  340. dsSetColor (0,0,2);
  341. dSpaceCollide (space,0,&nearCallback);
  342. if (!pause) {
  343. if (mov_type == 1)
  344. moveplat_1(stepsize);
  345. else
  346. moveplat_2(stepsize);
  347. dGeomSetPosition(platform, platpos[0], platpos[1], platpos[2]);
  348. updatecam();
  349. dWorldQuickStep (world,stepsize);
  350. //dWorldStep (world,stepsize);
  351. }
  352. if (write_world) {
  353. FILE *f = fopen ("state.dif","wt");
  354. if (f) {
  355. dWorldExportDIF (world,f,"X");
  356. fclose (f);
  357. }
  358. write_world = 0;
  359. }
  360. // remove all contact joints
  361. dJointGroupEmpty (contactgroup);
  362. dsSetColor (1,1,0);
  363. dsSetTexture (DS_WOOD);
  364. for (int i=0; i<num; i++) {
  365. for (int j=0; j < GPB; j++) {
  366. if (! dBodyIsEnabled (obj[i].body)) {
  367. dsSetColor (1,0.8,0);
  368. }
  369. else {
  370. dsSetColor (1,1,0);
  371. }
  372. drawGeom (obj[i].geom[j],0,0,show_aabb);
  373. }
  374. }
  375. dsSetColor (1,0,0);
  376. drawGeom (platform,0,0,show_aabb);
  377. //usleep(5000);
  378. }
  379. int main (int argc, char **argv)
  380. {
  381. // setup pointers to drawstuff callback functions
  382. dsFunctions fn;
  383. fn.version = DS_VERSION;
  384. fn.start = &start;
  385. fn.step = &simLoop;
  386. fn.command = &command;
  387. fn.stop = 0;
  388. fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
  389. // create world
  390. dInitODE();
  391. world = dWorldCreate();
  392. #if 1
  393. space = dHashSpaceCreate (0);
  394. #elif 0
  395. dVector3 center = {0,0,0}, extents = { 100, 100, 100};
  396. space = dQuadTreeSpaceCreate(0, center, extents, 5);
  397. #elif 0
  398. space = dSweepAndPruneSpaceCreate (0, dSAP_AXES_XYZ);
  399. #else
  400. space = dSimpleSpaceCreate(0);
  401. #endif
  402. contactgroup = dJointGroupCreate (0);
  403. dWorldSetGravity (world,0,0,-0.5);
  404. dWorldSetCFM (world,1e-5);
  405. dWorldSetLinearDamping(world, 0.00001);
  406. dWorldSetAngularDamping(world, 0.005);
  407. dWorldSetMaxAngularSpeed(world, 200);
  408. dWorldSetContactSurfaceLayer (world,0.001);
  409. ground = dCreatePlane (space,0,0,1,0);
  410. memset (obj,0,sizeof(obj));
  411. // create lift platform
  412. platform = dCreateBox(space, 4, 4, 1);
  413. dGeomSetCategoryBits(ground, 1ul);
  414. dGeomSetCategoryBits(platform, 2ul);
  415. dGeomSetCollideBits(ground, ~2ul);
  416. dGeomSetCollideBits(platform, ~1ul);
  417. // run simulation
  418. dsSimulationLoop (argc,argv,352,288,&fn);
  419. dJointGroupDestroy (contactgroup);
  420. dSpaceDestroy (space);
  421. dWorldDestroy (world);
  422. dCloseODE();
  423. return 0;
  424. }
  425. // Local Variables:
  426. // c-basic-offset:4
  427. // End: