demo_motor.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #include <ode/ode.h>
  23. #include <drawstuff/drawstuff.h>
  24. #include "texturepath.h"
  25. #ifdef _MSC_VER
  26. #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
  27. #endif
  28. // select correct drawing functions
  29. #ifdef dDOUBLE
  30. #define dsDrawBox dsDrawBoxD
  31. #endif
  32. // some constants
  33. #define SIDE (0.5f) // side length of a box
  34. #define MASS (1.0) // mass of a box
  35. // dynamics and collision objects
  36. static dWorldID world;
  37. static dBodyID body[2];
  38. static dGeomID geom[2];
  39. static dJointID lmotor[2];
  40. static dJointID amotor[2];
  41. static dSpaceID space;
  42. static dJointGroupID contactgroup;
  43. // start simulation - set viewpoint
  44. static void start()
  45. {
  46. dAllocateODEDataForThread(dAllocateMaskAll);
  47. static float xyz[3] = {1.0382f,-1.0811f,1.4700f};
  48. static float hpr[3] = {135.0000f,-19.5000f,0.0000f};
  49. dsSetViewpoint (xyz,hpr);
  50. printf ("Press 'q,a,z' to control one axis of lmotor connectiong two bodies. (q is +,a is 0, z is -)\n");
  51. printf ("Press 'w,e,r' to control one axis of lmotor connectiong first body with world. (w is +,e is 0, r is -)\n");
  52. }
  53. // called when a key pressed
  54. static void command (int cmd)
  55. {
  56. if (cmd == 'q' || cmd == 'Q') {
  57. dJointSetLMotorParam(lmotor[0],dParamVel,0);
  58. dJointSetLMotorParam(lmotor[0],dParamVel2,0);
  59. dJointSetLMotorParam(lmotor[0],dParamVel3,0.1);
  60. } else if (cmd == 'a' || cmd == 'A') {
  61. dJointSetLMotorParam(lmotor[0],dParamVel,0);
  62. dJointSetLMotorParam(lmotor[0],dParamVel2,0);
  63. dJointSetLMotorParam(lmotor[0],dParamVel3,0);
  64. } else if (cmd == 'z' || cmd == 'Z') {
  65. dJointSetLMotorParam(lmotor[0],dParamVel,0);
  66. dJointSetLMotorParam(lmotor[0],dParamVel2,0);
  67. dJointSetLMotorParam(lmotor[0],dParamVel3,-0.1);
  68. } else if (cmd == 'w' || cmd == 'W') {
  69. dJointSetLMotorParam(lmotor[1],dParamVel,0.1);
  70. dJointSetLMotorParam(lmotor[1],dParamVel2,0);
  71. dJointSetLMotorParam(lmotor[1],dParamVel3,0);
  72. } else if (cmd == 'e' || cmd == 'E') {
  73. dJointSetLMotorParam(lmotor[1],dParamVel,0);
  74. dJointSetLMotorParam(lmotor[1],dParamVel2,0);
  75. dJointSetLMotorParam(lmotor[1],dParamVel3,0);
  76. } else if (cmd == 'r' || cmd == 'R') {
  77. dJointSetLMotorParam(lmotor[1],dParamVel,-0.1);
  78. dJointSetLMotorParam(lmotor[1],dParamVel2,0);
  79. dJointSetLMotorParam(lmotor[1],dParamVel3,0);
  80. }
  81. }
  82. static void nearCallback (void *, dGeomID o1, dGeomID o2)
  83. {
  84. // exit without doing anything if the two bodies are connected by a joint
  85. dBodyID b1 = dGeomGetBody(o1);
  86. dBodyID b2 = dGeomGetBody(o2);
  87. dContact contact;
  88. contact.surface.mode = 0;
  89. contact.surface.mu = dInfinity;
  90. if (dCollide (o1,o2,1,&contact.geom,sizeof(dContactGeom))) {
  91. dJointID c = dJointCreateContact (world,contactgroup,&contact);
  92. dJointAttach (c,b1,b2);
  93. }
  94. }
  95. // simulation loop
  96. static void simLoop (int pause)
  97. {
  98. if (!pause) {
  99. dSpaceCollide(space,0,&nearCallback);
  100. dWorldQuickStep (world,0.05);
  101. dJointGroupEmpty(contactgroup);
  102. }
  103. dReal sides1[3];
  104. dGeomBoxGetLengths(geom[0], sides1);
  105. dReal sides2[3];
  106. dGeomBoxGetLengths(geom[1], sides2);
  107. dsSetTexture (DS_WOOD);
  108. dsSetColor (1,1,0);
  109. dsDrawBox (dBodyGetPosition(body[0]),dBodyGetRotation(body[0]),sides1);
  110. dsSetColor (0,1,1);
  111. dsDrawBox (dBodyGetPosition(body[1]),dBodyGetRotation(body[1]),sides2);
  112. }
  113. int main (int argc, char **argv)
  114. {
  115. // setup pointers to drawstuff callback functions
  116. dsFunctions fn;
  117. fn.version = DS_VERSION;
  118. fn.start = &start;
  119. fn.step = &simLoop;
  120. fn.command = &command;
  121. fn.stop = 0;
  122. fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
  123. // create world
  124. dInitODE2(0);
  125. contactgroup = dJointGroupCreate(0);
  126. world = dWorldCreate();
  127. space = dSimpleSpaceCreate(0);
  128. dMass m;
  129. dMassSetBox (&m,1,SIDE,SIDE,SIDE);
  130. dMassAdjust (&m,MASS);
  131. body[0] = dBodyCreate (world);
  132. dBodySetMass (body[0],&m);
  133. dBodySetPosition (body[0],0,0,1);
  134. geom[0] = dCreateBox(space,SIDE,SIDE,SIDE);
  135. body[1] = dBodyCreate (world);
  136. dBodySetMass (body[1],&m);
  137. dBodySetPosition (body[1],0,0,2);
  138. geom[1] = dCreateBox(space,SIDE,SIDE,SIDE);
  139. dGeomSetBody(geom[0],body[0]);
  140. dGeomSetBody(geom[1],body[1]);
  141. lmotor[0] = dJointCreateLMotor (world,0);
  142. dJointAttach (lmotor[0],body[0],body[1]);
  143. lmotor[1] = dJointCreateLMotor (world,0);
  144. dJointAttach (lmotor[1],body[0],0);
  145. amotor[0] = dJointCreateAMotor(world,0);
  146. dJointAttach(amotor[0], body[0],body[1]);
  147. amotor[1] = dJointCreateAMotor(world,0);
  148. dJointAttach(amotor[1], body[0], 0);
  149. for (int i=0; i<2; i++) {
  150. dJointSetAMotorNumAxes(amotor[i], 3);
  151. dJointSetAMotorAxis(amotor[i],0,1,1,0,0);
  152. dJointSetAMotorAxis(amotor[i],1,1,0,1,0);
  153. dJointSetAMotorAxis(amotor[i],2,1,0,0,1);
  154. dJointSetAMotorParam(amotor[i],dParamFMax,0.00001);
  155. dJointSetAMotorParam(amotor[i],dParamFMax2,0.00001);
  156. dJointSetAMotorParam(amotor[i],dParamFMax3,0.00001);
  157. dJointSetAMotorParam(amotor[i],dParamVel,0);
  158. dJointSetAMotorParam(amotor[i],dParamVel2,0);
  159. dJointSetAMotorParam(amotor[i],dParamVel3,0);
  160. dJointSetLMotorNumAxes(lmotor[i],3);
  161. dJointSetLMotorAxis(lmotor[i],0,1,1,0,0);
  162. dJointSetLMotorAxis(lmotor[i],1,1,0,1,0);
  163. dJointSetLMotorAxis(lmotor[i],2,1,0,0,1);
  164. dJointSetLMotorParam(lmotor[i],dParamFMax,0.0001);
  165. dJointSetLMotorParam(lmotor[i],dParamFMax2,0.0001);
  166. dJointSetLMotorParam(lmotor[i],dParamFMax3,0.0001);
  167. }
  168. // run simulation
  169. dsSimulationLoop (argc,argv,352,288,&fn);
  170. dJointGroupDestroy(contactgroup);
  171. dSpaceDestroy (space);
  172. dWorldDestroy (world);
  173. dCloseODE();
  174. return 0;
  175. }