demo_motion.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 = dDOT(fdir1, vel);
  91. motion2 = dDOT(fdir2, vel);
  92. motionN = dDOT(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 = dDOT(mov2_vel, motiondir1);
  109. contact.surface.motion2 = inv * dDOT(mov2_vel, motiondir2);
  110. contact.surface.motionN = inv * dDOT(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. dBodyDestroy (obj[i].body);
  189. for (k=0; k < GPB; k++) {
  190. if (obj[i].geom[k]) dGeomDestroy (obj[i].geom[k]);
  191. }
  192. memset (&obj[i],0,sizeof(obj[i]));
  193. }
  194. obj[i].body = dBodyCreate (world);
  195. for (k=0; k<3; k++) sides[k] = dRandReal()*0.5+0.1;
  196. dMatrix3 R;
  197. if (random_pos)
  198. {
  199. dBodySetPosition (obj[i].body,
  200. dRandReal()*2-1 + platpos[0],
  201. dRandReal()*2-1 + platpos[1],
  202. dRandReal()+2 + platpos[2]);
  203. dRFromAxisAndAngle (R,dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
  204. dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
  205. }
  206. else
  207. {
  208. dBodySetPosition (obj[i].body,
  209. platpos[0],
  210. platpos[1],
  211. platpos[2]+2);
  212. dRSetIdentity (R);
  213. }
  214. dBodySetRotation (obj[i].body,R);
  215. dBodySetData (obj[i].body,(void*) i);
  216. if (cmd == 'b') {
  217. dMassSetBox (&m,DENSITY,sides[0],sides[1],sides[2]);
  218. obj[i].geom[0] = dCreateBox (space,sides[0],sides[1],sides[2]);
  219. }
  220. else if (cmd == 'c') {
  221. sides[0] *= 0.5;
  222. dMassSetCapsule (&m,DENSITY,3,sides[0],sides[1]);
  223. obj[i].geom[0] = dCreateCapsule (space,sides[0],sides[1]);
  224. }
  225. else if (cmd == 'y') {
  226. dMassSetCylinder (&m,DENSITY,3,sides[0],sides[1]);
  227. obj[i].geom[0] = dCreateCylinder (space,sides[0],sides[1]);
  228. }
  229. else if (cmd == 's') {
  230. sides[0] *= 0.5;
  231. dMassSetSphere (&m,DENSITY,sides[0]);
  232. obj[i].geom[0] = dCreateSphere (space,sides[0]);
  233. }
  234. if (!setBody)
  235. for (k=0; k < GPB; k++) {
  236. if (obj[i].geom[k]) dGeomSetBody (obj[i].geom[k],obj[i].body);
  237. }
  238. dBodySetMass (obj[i].body,&m);
  239. }
  240. else if (cmd == 'a') {
  241. show_aabb ^= 1;
  242. }
  243. else if (cmd == 't') {
  244. show_contacts ^= 1;
  245. }
  246. else if (cmd == 'r') {
  247. random_pos ^= 1;
  248. }
  249. else if (cmd == '1') {
  250. write_world = 1;
  251. }
  252. else if (cmd == ' ') {
  253. mov_time = 0;
  254. }
  255. else if (cmd == 'm') {
  256. mov_type = mov_type==1 ? 2 : 1;
  257. mov_time = 0;
  258. }
  259. }
  260. // draw a geom
  261. void drawGeom (dGeomID g, const dReal *pos, const dReal *R, int show_aabb)
  262. {
  263. int i;
  264. if (!g) return;
  265. if (!pos) pos = dGeomGetPosition (g);
  266. if (!R) R = dGeomGetRotation (g);
  267. int type = dGeomGetClass (g);
  268. if (type == dBoxClass) {
  269. dVector3 sides;
  270. dGeomBoxGetLengths (g,sides);
  271. dsDrawBox (pos,R,sides);
  272. }
  273. else if (type == dSphereClass) {
  274. dsDrawSphere (pos,R,dGeomSphereGetRadius (g));
  275. }
  276. else if (type == dCapsuleClass) {
  277. dReal radius,length;
  278. dGeomCapsuleGetParams (g,&radius,&length);
  279. dsDrawCapsule (pos,R,length,radius);
  280. }
  281. else if (type == dCylinderClass) {
  282. dReal radius,length;
  283. dGeomCylinderGetParams (g,&radius,&length);
  284. dsDrawCylinder (pos,R,length,radius);
  285. }
  286. else if (type == dGeomTransformClass) {
  287. dGeomID g2 = dGeomTransformGetGeom (g);
  288. const dReal *pos2 = dGeomGetPosition (g2);
  289. const dReal *R2 = dGeomGetRotation (g2);
  290. dVector3 actual_pos;
  291. dMatrix3 actual_R;
  292. dMULTIPLY0_331 (actual_pos,R,pos2);
  293. actual_pos[0] += pos[0];
  294. actual_pos[1] += pos[1];
  295. actual_pos[2] += pos[2];
  296. dMULTIPLY0_333 (actual_R,R,R2);
  297. drawGeom (g2,actual_pos,actual_R,0);
  298. }
  299. if (show_body) {
  300. dBodyID body = dGeomGetBody(g);
  301. if (body) {
  302. const dReal *bodypos = dBodyGetPosition (body);
  303. const dReal *bodyr = dBodyGetRotation (body);
  304. dReal bodySides[3] = { 0.1, 0.1, 0.1 };
  305. dsSetColorAlpha(0,1,0,1);
  306. dsDrawBox(bodypos,bodyr,bodySides);
  307. }
  308. }
  309. if (show_aabb) {
  310. // draw the bounding box for this geom
  311. dReal aabb[6];
  312. dGeomGetAABB (g,aabb);
  313. dVector3 bbpos;
  314. for (i=0; i<3; i++) bbpos[i] = 0.5*(aabb[i*2] + aabb[i*2+1]);
  315. dVector3 bbsides;
  316. for (i=0; i<3; i++) bbsides[i] = aabb[i*2+1] - aabb[i*2];
  317. dMatrix3 RI;
  318. dRSetIdentity (RI);
  319. dsSetColorAlpha (1,0,0,0.5);
  320. dsDrawBox (bbpos,RI,bbsides);
  321. }
  322. }
  323. // simulation loop
  324. static void updatecam()
  325. {
  326. xyz[0] = platpos[0] + 3.3;
  327. xyz[1] = platpos[1] - 1.8;
  328. xyz[2] = platpos[2] + 2;
  329. dsSetViewpoint (xyz, hpr);
  330. }
  331. static void simLoop (int pause)
  332. {
  333. const dReal stepsize = 0.02;
  334. dsSetColor (0,0,2);
  335. dSpaceCollide (space,0,&nearCallback);
  336. if (!pause) {
  337. if (mov_type == 1)
  338. moveplat_1(stepsize);
  339. else
  340. moveplat_2(stepsize);
  341. dGeomSetPosition(platform, platpos[0], platpos[1], platpos[2]);
  342. updatecam();
  343. dWorldQuickStep (world,stepsize);
  344. //dWorldStep (world,stepsize);
  345. }
  346. if (write_world) {
  347. FILE *f = fopen ("state.dif","wt");
  348. if (f) {
  349. dWorldExportDIF (world,f,"X");
  350. fclose (f);
  351. }
  352. write_world = 0;
  353. }
  354. // remove all contact joints
  355. dJointGroupEmpty (contactgroup);
  356. dsSetColor (1,1,0);
  357. dsSetTexture (DS_WOOD);
  358. for (int i=0; i<num; i++) {
  359. for (int j=0; j < GPB; j++) {
  360. if (! dBodyIsEnabled (obj[i].body)) {
  361. dsSetColor (1,0.8,0);
  362. }
  363. else {
  364. dsSetColor (1,1,0);
  365. }
  366. drawGeom (obj[i].geom[j],0,0,show_aabb);
  367. }
  368. }
  369. dsSetColor (1,0,0);
  370. drawGeom (platform,0,0,show_aabb);
  371. //usleep(5000);
  372. }
  373. int main (int argc, char **argv)
  374. {
  375. // setup pointers to drawstuff callback functions
  376. dsFunctions fn;
  377. fn.version = DS_VERSION;
  378. fn.start = &start;
  379. fn.step = &simLoop;
  380. fn.command = &command;
  381. fn.stop = 0;
  382. fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
  383. if(argc==2)
  384. {
  385. fn.path_to_textures = argv[1];
  386. }
  387. // create world
  388. dInitODE();
  389. world = dWorldCreate();
  390. //space = dHashSpaceCreate (0);
  391. dVector3 center = {0,0,0}, extents = { 100, 100, 100};
  392. space = dQuadTreeSpaceCreate(0, center, extents, 5);
  393. contactgroup = dJointGroupCreate (0);
  394. dWorldSetGravity (world,0,0,-0.5);
  395. dWorldSetCFM (world,1e-5);
  396. dWorldSetLinearDamping(world, 0.00001);
  397. dWorldSetAngularDamping(world, 0.005);
  398. dWorldSetMaxAngularSpeed(world, 200);
  399. dWorldSetContactSurfaceLayer (world,0.001);
  400. ground = dCreatePlane (space,0,0,1,0);
  401. memset (obj,0,sizeof(obj));
  402. // create lift platform
  403. platform = dCreateBox(space, 4, 4, 1);
  404. dGeomSetCategoryBits(ground, 1ul);
  405. dGeomSetCategoryBits(platform, 2ul);
  406. dGeomSetCollideBits(ground, ~2ul);
  407. dGeomSetCollideBits(platform, ~1ul);
  408. // run simulation
  409. dsSimulationLoop (argc,argv,352,288,&fn);
  410. dJointGroupDestroy (contactgroup);
  411. dSpaceDestroy (space);
  412. dWorldDestroy (world);
  413. dCloseODE();
  414. return 0;
  415. }
  416. // Local Variables:
  417. // c-basic-offset:4
  418. // End: