demo_jointPR.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 file try to demonstrate how the PR joint is working.
  24. The axisP is draw in red and the axisR is in green
  25. */
  26. #include <ode/ode.h>
  27. #include <drawstuff/drawstuff.h>
  28. #include <iostream>
  29. #include <math.h>
  30. #include "texturepath.h"
  31. #ifdef _MSC_VER
  32. #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
  33. #endif
  34. // select correct drawing functions
  35. #ifdef dDOUBLE
  36. #define dsDrawBox dsDrawBoxD
  37. #endif
  38. // physics parameters
  39. #define BOX1_LENGTH 2 // Size along the X axis
  40. #define BOX1_WIDTH 1 // Size along the Y axis
  41. #define BOX1_HEIGHT 0.4 // Size along the Z axis (up) since gravity is (0,0,-10)
  42. #define BOX2_LENGTH 0.2
  43. #define BOX2_WIDTH 0.1
  44. #define BOX2_HEIGHT 0.4
  45. #define Mass1 10
  46. #define Mass2 0.1
  47. #define PRISMATIC_ONLY 1
  48. #define ROTOIDE_ONLY 2
  49. int flag = 0;
  50. //camera view
  51. static float xyz[3] = {2.0f,-3.5f,2.0000f};
  52. static float hpr[3] = {90.000f,-25.5000f,0.0000f};
  53. //world,space,body & geom
  54. static dWorldID world;
  55. static dSpaceID space;
  56. static dSpaceID box1_space;
  57. static dBodyID box1_body[1];
  58. static dBodyID box2_body[1];
  59. static dJointID joint[1];
  60. static dJointGroupID contactgroup;
  61. static dGeomID ground;
  62. static dGeomID box1[1];
  63. static dGeomID box2[1];
  64. //collision detection
  65. static void nearCallback (void *data, dGeomID o1, dGeomID o2)
  66. {
  67. int i,n;
  68. dBodyID b1 = dGeomGetBody(o1);
  69. dBodyID b2 = dGeomGetBody(o2);
  70. if (b1 && b2 && dAreConnectedExcluding (b1,b2,dJointTypeContact)) return;
  71. const int N = 10;
  72. dContact contact[N];
  73. n = dCollide (o1,o2,N,&contact[0].geom,sizeof(dContact));
  74. if (n > 0)
  75. {
  76. for (i=0; i<n; i++)
  77. {
  78. contact[i].surface.mode = dContactSlip1 | dContactSlip2 |
  79. dContactSoftERP | dContactSoftCFM | dContactApprox1;
  80. contact[i].surface.mu = 0.1;
  81. contact[i].surface.slip1 = 0.02;
  82. contact[i].surface.slip2 = 0.02;
  83. contact[i].surface.soft_erp = 0.1;
  84. contact[i].surface.soft_cfm = 0.0001;
  85. dJointID c = dJointCreateContact (world,contactgroup,&contact[i]);
  86. dJointAttach (c,dGeomGetBody(contact[i].geom.g1),dGeomGetBody(contact[i].geom.g2));
  87. }
  88. }
  89. }
  90. // start simulation - set viewpoint
  91. static void start()
  92. {
  93. dAllocateODEDataForThread(dAllocateMaskAll);
  94. dsSetViewpoint (xyz,hpr);
  95. printf ("Press 'd' to add force along positive x direction.\nPress 'a' to add force along negative x direction.\n");
  96. printf ("Press 'w' to add force along positive y direction.\nPress 's' to add force along negative y direction.\n");
  97. printf ("Press 'e' to add torque around positive z direction.\nPress 'q' to add torque around negative z direction.\n");
  98. printf ("Press 'o' to add force around positive x direction \n");
  99. printf("Press 'v' to give a defined velocity and add a FMax to the rotoide axis\n");
  100. printf("Press 'c' to set the velocity to zero and remove the FMax\n");
  101. printf("Press 'l' to add limits (-0.5 to 0.5rad) on the rotoide axis\n");
  102. printf("Press 'k' to remove the limits on the rotoide axis\n");
  103. printf("Press 'i' to get joint info\n");
  104. }
  105. // function to update camera position at each step.
  106. void update()
  107. {
  108. // const dReal *a =(dBodyGetPosition (box1_body[0]));
  109. // float dx=a[0];
  110. // float dy=a[1];
  111. // float dz=a[2];
  112. // xyz[0]=dx;
  113. // xyz[1]=dy-5;
  114. // xyz[2]=dz+2;
  115. // hpr[1]=-22.5000f;
  116. // dsSetViewpoint (xyz,hpr);
  117. }
  118. // called when a key pressed
  119. static void command (int cmd)
  120. {
  121. switch (cmd)
  122. {
  123. case 'w':
  124. case 'W':
  125. dBodyAddForce(box2_body[0],0,500,0);
  126. std::cout<<(dBodyGetPosition(box2_body[0])[1]-dBodyGetPosition(box1_body[0])[1])<<'\n';
  127. break;
  128. case 's':
  129. case 'S':
  130. dBodyAddForce(box2_body[0],0,-500,0);
  131. std::cout<<(dBodyGetPosition(box2_body[0])[1]-dBodyGetPosition(box1_body[0])[1])<<'\n';
  132. break;
  133. case 'd':
  134. case 'D':
  135. dBodyAddForce(box2_body[0],500,0,0);
  136. std::cout<<(dBodyGetPosition(box2_body[0])[0]-dBodyGetPosition(box1_body[0])[0])<<'\n';
  137. break;
  138. case 'a':
  139. case 'A':
  140. dBodyAddForce(box2_body[0],-500,0,0);
  141. std::cout<<(dBodyGetPosition(box2_body[0])[0]-dBodyGetPosition(box1_body[0])[0])<<'\n';
  142. break;
  143. case 'e':
  144. case 'E':
  145. dBodyAddRelTorque(box2_body[0],0,0,200);
  146. break;
  147. case 'q':
  148. case 'Q':
  149. dBodyAddRelTorque(box2_body[0],0,0,-200);
  150. break;
  151. case 'o':
  152. case 'O':
  153. dBodyAddForce(box1_body[0],10000,0,0);
  154. break;
  155. case 'v':
  156. case 'V':
  157. dJointSetPRParam(joint[0], dParamVel2, 2);
  158. dJointSetPRParam(joint[0], dParamFMax2, 500);
  159. break;
  160. case 'c':
  161. case 'C':
  162. dJointSetPRParam(joint[0], dParamVel2, 0);
  163. dJointSetPRParam(joint[0], dParamFMax2, 0);
  164. break;
  165. case 'l':
  166. case 'L':
  167. dJointSetPRParam(joint[0], dParamLoStop2, -0.5);
  168. dJointSetPRParam(joint[0], dParamHiStop2, 0.5);
  169. break;
  170. case 'k':
  171. case 'K':
  172. dJointSetPRParam(joint[0], dParamLoStop2, -dInfinity);
  173. dJointSetPRParam(joint[0], dParamHiStop2, dInfinity);
  174. break;
  175. case 'i':
  176. case 'I':
  177. dVector3 anchor;
  178. dJointGetPRAnchor(joint[0], anchor);
  179. dReal angle = dJointGetPRAngle(joint[0]);
  180. dReal w = dJointGetPRAngleRate(joint[0]);
  181. dReal l = dJointGetPRPosition(joint[0]);
  182. dReal v = dJointGetPRPositionRate(joint[0]);
  183. printf("Anchor: [%6.4f, %6.4f, %6.4f]\n", anchor[0], anchor[1], anchor[2]);
  184. printf("Position: %7.4f, Rate: %7.4f\n", l, v);
  185. printf("Angle: %7.4f, Rate: %7.4f\n", angle, w);
  186. break;
  187. }
  188. }
  189. // simulation loop
  190. static void simLoop (int pause)
  191. {
  192. if (!pause)
  193. {
  194. //draw 2 boxes
  195. dVector3 ss;
  196. dsSetTexture (DS_WOOD);
  197. const dReal *posBox2 = dGeomGetPosition(box2[0]);
  198. const dReal *rotBox2 = dGeomGetRotation(box2[0]);
  199. dsSetColor (1,1,0);
  200. dGeomBoxGetLengths (box2[0],ss);
  201. dsDrawBox (posBox2, rotBox2, ss);
  202. const dReal *posBox1 = dGeomGetPosition(box1[0]);
  203. const dReal *rotBox1 = dGeomGetRotation(box1[0]);
  204. dsSetColor (1,1,2);
  205. dGeomBoxGetLengths (box1[0], ss);
  206. dsDrawBox (posBox1, rotBox1, ss);
  207. dVector3 anchorPos;
  208. dJointGetPRAnchor (joint[0], anchorPos);
  209. // Draw the axisP
  210. if (ROTOIDE_ONLY != flag )
  211. {
  212. dsSetColor (1,0,0);
  213. dVector3 sizeP = {0, 0.1, 0.1};
  214. for (int i=0; i<3; ++i)
  215. sizeP[0] += (anchorPos[i] - posBox1[i])*(anchorPos[i] - posBox1[i]);
  216. sizeP[0] = sqrt(sizeP[0]);
  217. dVector3 posAxisP;
  218. for (int i=0; i<3; ++i)
  219. posAxisP[i] = posBox1[i] + (anchorPos[i] - posBox1[i])/2.0;
  220. dsDrawBox (posAxisP, rotBox1, sizeP);
  221. }
  222. // Draw the axisR
  223. if (PRISMATIC_ONLY != flag )
  224. {
  225. dsSetColor (0,1,0);
  226. dVector3 sizeR = {0, 0.1, 0.1};
  227. for (int i=0; i<3; ++i)
  228. sizeR[0] += (anchorPos[i] - posBox2[i])*(anchorPos[i] - posBox2[i]);
  229. sizeR[0] = sqrt(sizeR[0]);
  230. dVector3 posAxisR;
  231. for (int i=0; i<3; ++i)
  232. posAxisR[i] = posBox2[i] + (anchorPos[i] - posBox2[i])/2.0;
  233. dsDrawBox (posAxisR, rotBox2, sizeR);
  234. }
  235. dSpaceCollide (space,0,&nearCallback);
  236. dWorldQuickStep (world,0.0001);
  237. update();
  238. dJointGroupEmpty (contactgroup);
  239. }
  240. }
  241. void Help(char **argv)
  242. {
  243. printf("%s ", argv[0]);
  244. printf(" -h | --help : print this help\n");
  245. printf(" -b | --both : Display how the complete joint works\n");
  246. printf(" Default behavior\n");
  247. printf(" -p | --prismatic-only : Display how the prismatic part works\n");
  248. printf(" The anchor pts is set at the center of body 2\n");
  249. printf(" -r | --rotoide-only : Display how the rotoide part works\n");
  250. printf(" The anchor pts is set at the center of body 1\n");
  251. printf(" -t | --texture-path path : Path to the texture.\n");
  252. printf(" Default = %s\n", DRAWSTUFF_TEXTURE_PATH);
  253. printf("--------------------------------------------------\n");
  254. printf("Hit any key to continue:");
  255. getchar();
  256. exit(0);
  257. }
  258. int main (int argc, char **argv)
  259. {
  260. // setup pointers to drawstuff callback functions
  261. dsFunctions fn;
  262. fn.version = DS_VERSION;
  263. fn.start = &start;
  264. fn.step = &simLoop;
  265. fn.command = &command;
  266. fn.stop = 0;
  267. fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
  268. if (argc >= 2 )
  269. {
  270. for (int i=1; i < argc; ++i)
  271. {
  272. if ( 0 == strcmp("-h", argv[i]) || 0 == strcmp("--help", argv[i]) )
  273. Help(argv);
  274. if (!flag && (0 == strcmp("-p", argv[i]) ||0 == strcmp("--prismatic-only", argv[i])) )
  275. flag = PRISMATIC_ONLY;
  276. if (!flag && (0 == strcmp("-r", argv[i]) || 0 == strcmp("--rotoide-only", argv[i])) )
  277. flag = ROTOIDE_ONLY;
  278. if (0 == strcmp("-t", argv[i]) || 0 == strcmp("--texture-path", argv[i]))
  279. {
  280. int j = i+1;
  281. if ( j+1 > argc || // Check if we have enough arguments
  282. argv[j] == '\0' || // We should have a path here
  283. argv[j][0] == '-' ) // We should have a path not a command line
  284. Help(argv);
  285. else
  286. fn.path_to_textures = argv[++i]; // Increase i since we use this argument
  287. }
  288. }
  289. }
  290. dInitODE2(0);
  291. // create world
  292. world = dWorldCreate();
  293. space = dHashSpaceCreate (0);
  294. contactgroup = dJointGroupCreate (0);
  295. dWorldSetGravity (world,0,0,-10);
  296. ground = dCreatePlane (space,0,0,1,0);
  297. //create two boxes
  298. dMass m;
  299. box1_body[0] = dBodyCreate (world);
  300. dMassSetBox (&m,1,BOX1_LENGTH,BOX1_WIDTH,BOX1_HEIGHT);
  301. dMassAdjust (&m,Mass1);
  302. dBodySetMass (box1_body[0],&m);
  303. box1[0] = dCreateBox (0,BOX1_LENGTH,BOX1_WIDTH,BOX1_HEIGHT);
  304. dGeomSetBody (box1[0],box1_body[0]);
  305. box2_body[0] = dBodyCreate (world);
  306. dMassSetBox (&m,10,BOX2_LENGTH,BOX2_WIDTH,BOX2_HEIGHT);
  307. dMassAdjust (&m,Mass2);
  308. dBodySetMass (box2_body[0],&m);
  309. box2[0] = dCreateBox (0,BOX2_LENGTH,BOX2_WIDTH,BOX2_HEIGHT);
  310. dGeomSetBody (box2[0],box2_body[0]);
  311. //set the initial positions of body1 and body2
  312. dMatrix3 R;
  313. dRSetIdentity(R);
  314. dBodySetPosition (box1_body[0],0,0,BOX1_HEIGHT/2.0);
  315. dBodySetRotation (box1_body[0], R);
  316. dBodySetPosition (box2_body[0],
  317. 2.1,
  318. 0.0,
  319. BOX2_HEIGHT/2.0);
  320. dBodySetRotation (box2_body[0], R);
  321. //set PR joint
  322. joint[0] = dJointCreatePR(world,0);
  323. dJointAttach (joint[0],box1_body[0],box2_body[0]);
  324. switch (flag)
  325. {
  326. case PRISMATIC_ONLY:
  327. dJointSetPRAnchor (joint[0],
  328. 2.1,
  329. 0.0,
  330. BOX2_HEIGHT/2.0);
  331. dJointSetPRParam (joint[0],dParamLoStop, -0.5);
  332. dJointSetPRParam (joint[0],dParamHiStop, 1.5);
  333. break;
  334. case ROTOIDE_ONLY:
  335. dJointSetPRAnchor (joint[0],
  336. 0.0,
  337. 0.0,
  338. BOX2_HEIGHT/2.0);
  339. dJointSetPRParam (joint[0],dParamLoStop, 0.0);
  340. dJointSetPRParam (joint[0],dParamHiStop, 0.0);
  341. break;
  342. default:
  343. dJointSetPRAnchor (joint[0],
  344. 1.1,
  345. 0.0,
  346. BOX2_HEIGHT/2.0);
  347. dJointSetPRParam (joint[0],dParamLoStop, -0.5);
  348. dJointSetPRParam (joint[0],dParamHiStop, 1.5);
  349. break;
  350. }
  351. dJointSetPRAxis1(joint[0],1,0,0);
  352. dJointSetPRAxis2(joint[0],0,0,1);
  353. // We position the 2 body
  354. // The position of the rotoide joint is on the second body so it can rotate on itself
  355. // and move along the X axis.
  356. // With this anchor
  357. // - A force in X will move only the body 2 inside the low and hi limit
  358. // of the prismatic
  359. // - A force in Y will make the 2 bodies to rotate around on the plane
  360. box1_space = dSimpleSpaceCreate (space);
  361. dSpaceSetCleanup (box1_space,0);
  362. dSpaceAdd(box1_space,box1[0]);
  363. // run simulation
  364. dsSimulationLoop (argc,argv,400,300,&fn);
  365. dJointGroupDestroy (contactgroup);
  366. dSpaceDestroy (space);
  367. dWorldDestroy (world);
  368. dCloseODE();
  369. return 0;
  370. }