demo_jointPU.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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 program demonstrates how the PU joint works.
  24. A PU joint is a combination of a Universal joint and a Slider joint.
  25. It is a universal joint with a slider between the anchor point and
  26. body 1.
  27. The upper yellow body is fixed to the world
  28. The lower yellow body is attached to the upper body by a PU joint
  29. The green object is one aprt of the slider.
  30. The purple object is the second part of the slider.
  31. The red object represent the axis1 of the universal part.
  32. The blue object represent the axis2 of the universal part.
  33. The gray object represent the anchor2 of the PU joint.
  34. */
  35. #include <ode/ode.h>
  36. #include <drawstuff/drawstuff.h>
  37. #include <iostream>
  38. #include <math.h>
  39. #include "texturepath.h"
  40. #ifdef _MSC_VER
  41. #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
  42. #endif
  43. // select correct drawing functions
  44. #ifdef dDOUBLE
  45. #define dsDrawBox dsDrawBoxD
  46. #define dsDrawCylinder dsDrawCylinderD
  47. #define dsDrawCapsule dsDrawCapsuleD
  48. #endif
  49. enum IDX_CYL_DIM
  50. {
  51. RADIUS,
  52. LENGTH,
  53. NUM_CYL_DIM
  54. };
  55. const dVector3 boxDim = {1,1,1};
  56. const dVector3 extDim = {0.2,0.2,1.2};
  57. const dVector3 ancDim = {0.2,0.2,0.5};
  58. const dReal axDim[NUM_CYL_DIM] = {0.1,1.0};
  59. int type = dJointTypePU;
  60. const dReal VEL_INC = 0.01; // Velocity increment
  61. // physics parameters
  62. const dReal PI = 3.14159265358979323846264338327950288419716939937510;
  63. const dReal INT_EXT_RATIO = 0.8;
  64. #define X 0
  65. #define Y 1
  66. #define Z 2
  67. enum INDEX
  68. {
  69. W = 0,
  70. D,
  71. EXT,
  72. INT,
  73. AXIS1,
  74. AXIS2,
  75. ANCHOR,
  76. GROUND,
  77. NUM_PARTS,
  78. ALL = NUM_PARTS,
  79. // INDEX for catBits
  80. JOINT,
  81. LAST_INDEX_CNT
  82. };
  83. const int catBits[LAST_INDEX_CNT] =
  84. {
  85. 0x0001, ///< W Cylinder category
  86. 0x0002, ///< D Cylinder category
  87. 0x0004, ///< EXT sliderr category
  88. 0x0008, ///< INT slider category
  89. 0x0010, ///< AXIS1 universal category
  90. 0x0020, ///< AXIS2 universal category
  91. 0x0040, ///< ANCHOR category
  92. 0x0080, ///< Ground category
  93. ~0L, ///< All categories
  94. 0x0004 | 0x0008 | 0x0010 | 0x0020 ///< JOINT category
  95. };
  96. #define Mass1 10
  97. #define Mass2 8
  98. //camera view
  99. static float xyz[3] = {6.0f,0.0f,6.0000f};
  100. static float hpr[3] = {-180.000f,-25.5000f,0.0000f};
  101. //world,space,body & geom
  102. static dWorldID world;
  103. static dSpaceID space;
  104. static dJointGroupID contactgroup;
  105. static dBodyID body[NUM_PARTS];
  106. static dGeomID geom[NUM_PARTS];
  107. static dJoint *joint;
  108. const dReal BOX_SIDES[3] = {1.0,1.0,1.0};
  109. const dReal OBS_SIDES[3] = {0.4,0.4,0.4};
  110. const dReal RECT_SIDES[3] = {0.3, 0.1, 0.2};
  111. //collision detection
  112. static void nearCallback (void *data, dGeomID o1, dGeomID o2)
  113. {
  114. int i,n;
  115. dBodyID b1 = dGeomGetBody (o1);
  116. dBodyID b2 = dGeomGetBody (o2);
  117. //if (b1 && b2 && dAreConnectedExcluding (b1,b2,dJointTypeContact) ) return;
  118. const int N = 10;
  119. dContact contact[N];
  120. n = dCollide (o1,o2,N,&contact[0].geom,sizeof (dContact) );
  121. if (n > 0) {
  122. for (i=0; i<n; i++) {
  123. contact[i].surface.mode = (dContactSlip1 | dContactSlip2 |
  124. dContactSoftERP | dContactSoftCFM |
  125. dContactApprox1);
  126. contact[i].surface.mu = 0.1;
  127. contact[i].surface.slip1 = 0.02;
  128. contact[i].surface.slip2 = 0.02;
  129. contact[i].surface.soft_erp = 0.1;
  130. contact[i].surface.soft_cfm = 0.0001;
  131. dJointID c = dJointCreateContact (world,contactgroup,&contact[i]);
  132. dJointAttach (c,dGeomGetBody (contact[i].geom.g1),dGeomGetBody (contact[i].geom.g2) );
  133. }
  134. }
  135. }
  136. static void printKeyBoardShortCut()
  137. {
  138. printf ("Press 'h' for this help.\n");
  139. printf ("Press 'q' to add force on BLUE body along positive x direction.\n");
  140. printf ("Press 'w' to add force on BLUE body along negative x direction.\n");
  141. printf ("Press 'a' to add force on BLUE body along positive y direction.\n");
  142. printf ("Press 's' to add force on BLUE body along negative y direction.\n");
  143. printf ("Press 'z' to add force on BLUE body along positive z direction.\n");
  144. printf ("Press 'x' to add force on BLUE body along negative z direction.\n");
  145. printf ("Press 'e' to add torque on BLUE body around positive x direction \n");
  146. printf ("Press 'r' to add torque on BLUE body around negative x direction \n");
  147. printf ("Press 'd' to add torque on BLUE body around positive y direction \n");
  148. printf ("Press 'f' to add torque on BLUE body around negative y direction \n");
  149. printf ("Press 'c' to add torque on BLUE body around positive z direction \n");
  150. printf ("Press 'v' to add torque on BLUE body around negative z direction \n");
  151. printf ("Press '.' to increase joint velocity along the prismatic direction.\n");
  152. printf ("Press ',' to decrease joint velocity along the prismatic direction.\n");
  153. printf ("Press 'l' Toggle ON/OFF the limits on all the axis\n");
  154. printf ("Press 'g' Toggle ON/OFF the gravity\n");
  155. printf ("Press 'p' to print the position, angle and rates of the joint.\n");
  156. }
  157. // start simulation - set viewpoint
  158. static void start()
  159. {
  160. dAllocateODEDataForThread(dAllocateMaskAll);
  161. dsSetViewpoint (xyz,hpr);
  162. printf ("This program demonstrates how the PU joint works.\n");
  163. printf ("A PU joint is a combination of a Universal joint and a Slider joint.\n");
  164. printf ("It is a universal joint with a slider between the anchor point and \n");
  165. printf ("body 1.\n\n");
  166. printf ("The upper yellow body is fixed to the world\n");
  167. printf ("The lower yellow body is attached to the upper body by a PU joint\n");
  168. printf ("The green object is one aprt of the slider.\n");
  169. printf ("The purple object is the second part of the slider.\n");
  170. printf ("The red object represent the axis1 of the universal part. \n");
  171. printf ("The blue object represent the axis2 of the universal part. \n");
  172. printf ("The gray object represent the anchor2 of the PU joint. \n");
  173. printKeyBoardShortCut();
  174. }
  175. // function to update camera position at each step.
  176. void update()
  177. {
  178. // static FILE *file = fopen("x:/sim/src/libode/tstsrcSF/export.dat", "w");
  179. // static int cnt = 0;
  180. // char str[24];
  181. // sprintf(str, "%06d",cnt++);
  182. // dWorldExportDIF(world, file, str);
  183. }
  184. // called when a key pressed
  185. static void command (int cmd)
  186. {
  187. switch (cmd) {
  188. case 'h' : case 'H' : case '?' :
  189. printKeyBoardShortCut();
  190. break;
  191. // Force
  192. case 'q' : case 'Q' :
  193. dBodyAddForce(body[D],40,0,0);
  194. break;
  195. case 'w' : case 'W' :
  196. dBodyAddForce(body[D],-40,0,0);
  197. break;
  198. case 'a' : case 'A' :
  199. dBodyAddForce(body[D],0,40,0);
  200. break;
  201. case 's' : case 'S' :
  202. dBodyAddForce(body[D],0,-40,0);
  203. break;
  204. case 'z' : case 'Z' :
  205. dBodyAddForce(body[D],0,0,40);
  206. break;
  207. case 'x' : case 'X' :
  208. dBodyAddForce(body[D],0,0,-40);
  209. break;
  210. // Torque
  211. case 'e': case 'E':
  212. dBodyAddTorque(body[D],0.1,0,0);
  213. break;
  214. case 'r': case 'R':
  215. dBodyAddTorque(body[D],-0.1,0,0);
  216. break;
  217. case 'd': case 'D':
  218. dBodyAddTorque(body[D],0, 0.1,0);
  219. break;
  220. case 'f': case 'F':
  221. dBodyAddTorque(body[D],0,-0.1,0);
  222. break;
  223. case 'c': case 'C':
  224. dBodyAddTorque(body[D],0,0,0.1);
  225. break;
  226. case 'v': case 'V':
  227. dBodyAddTorque(body[D],0,0,0.1);
  228. break;
  229. // Velocity of joint
  230. case ',': case '<' : {
  231. dReal vel = joint->getParam (dParamVel3) - VEL_INC;
  232. joint->setParam (dParamVel3, vel);
  233. joint->setParam (dParamFMax3, 2);
  234. std::cout<<"Velocity = "<<vel<<" FMax = 2"<<'\n';
  235. }
  236. break;
  237. case '.': case '>' : {
  238. dReal vel = joint->getParam (dParamVel3) + VEL_INC;
  239. joint->setParam (dParamVel3, vel);
  240. joint->setParam (dParamFMax3, 2);
  241. std::cout<<"Velocity = "<<vel<<" FMax = 2"<<'\n';
  242. }
  243. break;
  244. case 'l': case 'L' : {
  245. dReal aLimit, lLimit, fmax;
  246. if ( joint->getParam (dParamFMax) ) {
  247. aLimit = dInfinity;
  248. lLimit = dInfinity;
  249. fmax = 0;
  250. }
  251. else {
  252. aLimit = 0.25*PI;
  253. lLimit = 0.5*axDim[LENGTH];
  254. fmax = 0.02;
  255. }
  256. joint->setParam (dParamFMax1, fmax);
  257. joint->setParam (dParamFMax2, fmax);
  258. joint->setParam (dParamFMax3, fmax);
  259. switch (joint->getType() ) {
  260. case dJointTypePR : {
  261. dPRJoint *pr = reinterpret_cast<dPRJoint *> (joint);
  262. pr->setParam (dParamLoStop, -lLimit);
  263. pr->setParam (dParamHiStop, -lLimit);
  264. pr->setParam (dParamLoStop2, aLimit);
  265. pr->setParam (dParamHiStop2, -aLimit);
  266. }
  267. break;
  268. case dJointTypePU : {
  269. dPUJoint *pu = reinterpret_cast<dPUJoint *> (joint);
  270. pu->setParam (dParamLoStop1, -aLimit);
  271. pu->setParam (dParamHiStop1, aLimit);
  272. pu->setParam (dParamLoStop2, -aLimit);
  273. pu->setParam (dParamHiStop2, aLimit);
  274. pu->setParam (dParamLoStop3, -lLimit);
  275. pu->setParam (dParamHiStop3, lLimit);
  276. }
  277. break;
  278. default: {} // keep the compiler happy
  279. }
  280. }
  281. break;
  282. case 'g': case 'G' : {
  283. dVector3 g;
  284. dWorldGetGravity(world, g);
  285. if ( g[2]< -0.1 )
  286. dWorldSetGravity(world, 0, 0, 0);
  287. else
  288. dWorldSetGravity(world, 0, 0, -0.5);
  289. }
  290. case 'p' :case 'P' : {
  291. switch (joint->getType() ) {
  292. case dJointTypeSlider : {
  293. dSliderJoint *sj = reinterpret_cast<dSliderJoint *> (joint);
  294. std::cout<<"Position ="<<sj->getPosition() <<"\n";
  295. }
  296. break;
  297. case dJointTypePU : {
  298. dPUJoint *pu = reinterpret_cast<dPUJoint *> (joint);
  299. std::cout<<"Position ="<<pu->getPosition() <<"\n";
  300. std::cout<<"Position Rate="<<pu->getPositionRate() <<"\n";
  301. std::cout<<"Angle1 ="<<pu->getAngle1() <<"\n";
  302. std::cout<<"Angle1 Rate="<<pu->getAngle1Rate() <<"\n";
  303. std::cout<<"Angle2 ="<<pu->getAngle2() <<"\n";
  304. std::cout<<"Angle2 Rate="<<pu->getAngle2Rate() <<"\n";
  305. }
  306. break;
  307. default: {} // keep the compiler happy
  308. }
  309. }
  310. break;
  311. }
  312. }
  313. static void drawBox (dGeomID id, int R, int G, int B)
  314. {
  315. if (!id)
  316. return;
  317. const dReal *pos = dGeomGetPosition (id);
  318. const dReal *rot = dGeomGetRotation (id);
  319. dsSetColor (R,G,B);
  320. dVector3 l;
  321. dGeomBoxGetLengths (id, l);
  322. dsDrawBox (pos, rot, l);
  323. }
  324. // simulation loop
  325. static void simLoop (int pause)
  326. {
  327. static bool todo = false;
  328. if ( todo ) { // DEBUG
  329. static int cnt = 0;
  330. ++cnt;
  331. if (cnt == 5)
  332. command ( 'q' );
  333. if (cnt == 10)
  334. dsStop();
  335. }
  336. if (!pause) {
  337. double simstep = 0.01; // 10ms simulation steps
  338. double dt = dsElapsedTime();
  339. int nrofsteps = (int) ceilf (dt/simstep);
  340. if (!nrofsteps)
  341. nrofsteps = 1;
  342. for (int i=0; i<nrofsteps && !pause; i++) {
  343. dSpaceCollide (space,0,&nearCallback);
  344. dWorldStep (world, simstep);
  345. dJointGroupEmpty (contactgroup);
  346. }
  347. update();
  348. dReal radius, length;
  349. dsSetTexture (DS_WOOD);
  350. drawBox (geom[W], 1,1,0);
  351. drawBox (geom[EXT], 0,1,0);
  352. dVector3 anchorPos;
  353. dReal ang1 = 0;
  354. dReal ang2 = 0;
  355. dVector3 axisP, axisR1, axisR2;
  356. if ( dJointTypePU == type ) {
  357. dPUJoint *pu = dynamic_cast<dPUJoint *> (joint);
  358. ang1 = pu->getAngle1();
  359. ang2 = pu->getAngle2();
  360. pu->getAxis1 (axisR1);
  361. pu->getAxis2 (axisR2);
  362. pu->getAxisP (axisP);
  363. dJointGetPUAnchor (pu->id(), anchorPos);
  364. }
  365. else if ( dJointTypePR == type ) {
  366. dPRJoint *pr = dynamic_cast<dPRJoint *> (joint);
  367. pr->getAxis1 (axisP);
  368. pr->getAxis2 (axisR1);
  369. dJointGetPRAnchor (pr->id(), anchorPos);
  370. }
  371. // Draw the axisR
  372. if ( geom[INT] ) {
  373. dsSetColor (1,0,1);
  374. dVector3 l;
  375. dGeomBoxGetLengths (geom[INT], l);
  376. const dReal *rotBox = dGeomGetRotation (geom[W]);
  377. dVector3 pos;
  378. for (int i=0; i<3; ++i)
  379. pos[i] = anchorPos[i] - 0.5*extDim[Z]*axisP[i];
  380. dsDrawBox (pos, rotBox, l);
  381. }
  382. dsSetTexture (DS_CHECKERED);
  383. if ( geom[AXIS1] ) {
  384. dQuaternion q, qAng;
  385. dQFromAxisAndAngle (qAng,axisR1[X], axisR1[Y], axisR1[Z], ang1);
  386. dGeomGetQuaternion (geom[AXIS1], q);
  387. dQuaternion qq;
  388. dQMultiply1 (qq, qAng, q);
  389. dMatrix3 R;
  390. dQtoR (qq,R);
  391. dGeomCylinderGetParams (dGeomTransformGetGeom (geom[AXIS1]), &radius, &length);
  392. dsSetColor (1,0,0);
  393. dsDrawCylinder (anchorPos, R, length, radius);
  394. }
  395. if ( dJointTypePU == type && geom[AXIS2] ) {
  396. //dPUJoint *pu = dynamic_cast<dPUJoint *> (joint);
  397. dQuaternion q, qAng, qq, qq1;
  398. dGeomGetQuaternion (geom[AXIS2], q);
  399. dQFromAxisAndAngle (qAng, 0, 1, 0, ang2);
  400. dQMultiply1 (qq, qAng, q);
  401. dQFromAxisAndAngle (qAng,axisR1[X], axisR1[Y], axisR1[Z], ang1);
  402. dQMultiply1 (qq1, qAng, qq);
  403. dMatrix3 R;
  404. dQtoR (qq1,R);
  405. dGeomCylinderGetParams (dGeomTransformGetGeom (geom[AXIS2]), &radius, &length);
  406. dsSetColor (0,0,1);
  407. dsDrawCylinder (anchorPos, R, length, radius);
  408. }
  409. dsSetTexture (DS_WOOD);
  410. // Draw the anchor
  411. if ( geom[ANCHOR] ) {
  412. dsSetColor (1,1,1);
  413. dVector3 l;
  414. dGeomBoxGetLengths (geom[ANCHOR], l);
  415. const dReal *rotBox = dGeomGetRotation (geom[D]);
  416. const dReal *posBox = dGeomGetPosition (geom[D]);
  417. dVector3 e;
  418. for (int i=0; i<3; ++i)
  419. e[i] = posBox[i] - anchorPos[i];
  420. dNormalize3 (e);
  421. dVector3 pos;
  422. for (int i=0; i<3; ++i)
  423. pos[i] = anchorPos[i] + 0.5 * l[Z]*e[i];
  424. dsDrawBox (pos, rotBox, l);
  425. }
  426. drawBox (geom[D], 1,1,0);
  427. }
  428. }
  429. void Help (char **argv)
  430. {
  431. printf ("%s ", argv[0]);
  432. printf (" -h | --help : print this help\n");
  433. printf (" -p | --PRJoint : Use a PR joint instead of PU joint\n");
  434. printf (" -t | --texture-path path : Path to the texture.\n");
  435. printf (" Default = %s\n", DRAWSTUFF_TEXTURE_PATH);
  436. printf ("--------------------------------------------------\n");
  437. printf ("Hit any key to continue:");
  438. getchar();
  439. exit (0);
  440. }
  441. int main (int argc, char **argv)
  442. {
  443. // setup pointers to drawstuff callback functions
  444. dsFunctions fn;
  445. fn.version = DS_VERSION;
  446. fn.start = &start;
  447. fn.step = &simLoop;
  448. fn.command = &command;
  449. fn.stop = 0;
  450. fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
  451. if (argc >= 2 ) {
  452. for (int i=1; i < argc; ++i) {
  453. if ( 0 == strcmp ("-h", argv[i]) || 0 == strcmp ("--help", argv[i]) )
  454. Help (argv);
  455. if ( 0 == strcmp ("-p", argv[i]) || 0 == strcmp ("--PRJoint", argv[i]) )
  456. type = dJointTypePR;
  457. if (0 == strcmp ("-t", argv[i]) || 0 == strcmp ("--texture-path", argv[i]) ) {
  458. int j = i+1;
  459. if ( j+1 > argc || // Check if we have enough arguments
  460. argv[j] == '\0' || // We should have a path here
  461. argv[j][0] == '-' ) // We should have a path not a command line
  462. Help (argv);
  463. else
  464. fn.path_to_textures = argv[++i]; // Increase i since we use this argument
  465. }
  466. }
  467. }
  468. dInitODE2(0);
  469. world = dWorldCreate();
  470. dWorldSetERP (world, 0.8);
  471. space = dSimpleSpaceCreate (0);
  472. contactgroup = dJointGroupCreate (0);
  473. geom[GROUND] = dCreatePlane (space, 0,0,1,0);
  474. dGeomSetCategoryBits (geom[GROUND], catBits[GROUND]);
  475. dGeomSetCollideBits (geom[GROUND], catBits[ALL]);
  476. dMass m;
  477. // Create the body attached to the World
  478. body[W] = dBodyCreate (world);
  479. // Main axis of cylinder is along X=1
  480. m.setBox (1, boxDim[X], boxDim[Y], boxDim[Z]);
  481. m.adjust (Mass1);
  482. geom[W] = dCreateBox (space, boxDim[X], boxDim[Y], boxDim[Z]);
  483. dGeomSetBody (geom[W], body[W]);
  484. dGeomSetCategoryBits (geom[W], catBits[W]);
  485. dGeomSetCollideBits (geom[W], catBits[ALL] & (~catBits[W]) & (~catBits[JOINT]) );
  486. dBodySetMass (body[W], &m);
  487. // Create the dandling body
  488. body[D] = dBodyCreate (world);
  489. // Main axis of capsule is along X=1
  490. m.setBox (1, boxDim[X], boxDim[Y], boxDim[Z]);
  491. m.adjust (Mass1);
  492. geom[D] = dCreateBox (space, boxDim[X], boxDim[Y], boxDim[Z]);
  493. dGeomSetBody (geom[D], body[D]);
  494. dGeomSetCategoryBits (geom[D], catBits[D]);
  495. dGeomSetCollideBits (geom[D], catBits[ALL] & (~catBits[D]) & (~catBits[JOINT]) );
  496. dBodySetMass (body[D], &m);
  497. // Create the external part of the slider joint
  498. geom[EXT] = dCreateBox (0, extDim[X], extDim[Y], extDim[Z]);
  499. dGeomSetCategoryBits (geom[EXT], catBits[EXT]);
  500. dGeomSetCollideBits (geom[EXT],
  501. catBits[ALL] & (~catBits[JOINT]) & (~catBits[W]) & (~catBits[D]) );
  502. // Create the internal part of the slider joint
  503. geom[INT] = dCreateBox (0, INT_EXT_RATIO*extDim[X],
  504. INT_EXT_RATIO*extDim[Y],
  505. INT_EXT_RATIO*extDim[Z]);
  506. dGeomSetCategoryBits (geom[INT], catBits[INT]);
  507. dGeomSetCollideBits (geom[INT],
  508. catBits[ALL] & (~catBits[JOINT]) & (~catBits[W]) & (~catBits[D]) );
  509. dMatrix3 R;
  510. // Create the first axis of the universal joi9nt
  511. geom[AXIS1] = dCreateGeomTransform (0);
  512. //Rotation of 90deg around y
  513. dRFromAxisAndAngle (R, 0,1,0, 0.5*PI);
  514. dGeomSetRotation (geom[AXIS1], R);
  515. dGeomSetCategoryBits (geom[AXIS1], catBits[AXIS1]);
  516. dGeomSetCollideBits (geom[AXIS1],
  517. catBits[ALL] & ~catBits[JOINT] & ~catBits[W] & ~catBits[D]);
  518. dGeomTransformSetGeom (geom[AXIS1], dCreateCylinder (0, axDim[RADIUS], axDim[LENGTH]) );
  519. // Create the second axis of the universal joint
  520. geom[AXIS2] = dCreateGeomTransform (0);
  521. //Rotation of 90deg around y
  522. dRFromAxisAndAngle (R, 1,0,0, 0.5*PI);
  523. dGeomSetRotation (geom[AXIS2], R);
  524. dGeomSetCategoryBits (geom[AXIS2], catBits[AXIS2]);
  525. dGeomSetCollideBits (geom[AXIS2],
  526. catBits[ALL] & ~catBits[JOINT] & ~catBits[W] & ~catBits[D]);
  527. dGeomTransformSetGeom (geom[AXIS2], dCreateCylinder (0, axDim[RADIUS], axDim[LENGTH]) );
  528. // Create the anchor
  529. geom[ANCHOR] = dCreateBox (0, ancDim[X], ancDim[Y], ancDim[Z]);
  530. dGeomSetCategoryBits (geom[ANCHOR], catBits[ANCHOR]);
  531. dGeomSetCollideBits (geom[ANCHOR],
  532. catBits[ALL] & (~catBits[JOINT]) & (~catBits[W]) & (~catBits[D]) );
  533. if (body[W]) {
  534. dBodySetPosition(body[W], 0, 0, 5);
  535. }
  536. if (geom[EXT]) {
  537. dGeomSetPosition (geom[EXT], 0,0,3.8);
  538. }
  539. if (geom[INT]) {
  540. dGeomSetPosition (geom[INT], 0,0,2.6);
  541. }
  542. if (geom[AXIS1]) {
  543. dGeomSetPosition (geom[AXIS1], 0,0,2.5);
  544. }
  545. if (geom[AXIS2]) {
  546. dGeomSetPosition (geom[AXIS2], 0,0,2.5);
  547. }
  548. if (geom[ANCHOR]) {
  549. dGeomSetPosition (geom[ANCHOR], 0,0,2.25);
  550. }
  551. if (body[D]) {
  552. dBodySetPosition (body[D], 0,0,1.5);
  553. }
  554. // Attache the upper box to the world
  555. dJointID fixed = dJointCreateFixed (world,0);
  556. dJointAttach (fixed , NULL, body[W]);
  557. dJointSetFixed (fixed );
  558. if (type == dJointTypePR) {
  559. dPRJoint *pr = new dPRJoint (world, 0);
  560. pr->attach (body[W], body[D]);
  561. pr->setAxis1 (0, 0, -1);
  562. pr->setAxis2 (1, 0, 0);
  563. joint = pr;
  564. dJointSetPRAnchor (pr->id(), 0, 0, 2.5);
  565. }
  566. else {
  567. dPUJoint *pu = new dPUJoint (world, 0);
  568. pu->attach (body[W], body[D]);
  569. pu->setAxis1 (1, 0, 0);
  570. pu->setAxis2 (0, 1, 0);
  571. pu->setAxisP (0, 0, -1);
  572. joint = pu;
  573. dJointSetPUAnchor (pu->id(), 0, 0, 2.5);
  574. }
  575. // run simulation
  576. dsSimulationLoop (argc,argv,400,300,&fn);
  577. delete joint;
  578. dJointGroupDestroy (contactgroup);
  579. dSpaceDestroy (space);
  580. dWorldDestroy (world);
  581. dCloseODE();
  582. return 0;
  583. }