demo_motion.cpp 16 KB

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