demo_jointPU.cpp 20 KB

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