demo_dhinge.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 dDOUBLE
  26. #define dsDrawSphere dsDrawSphereD
  27. #define dsDrawBox dsDrawBoxD
  28. #define dsDrawLine dsDrawLineD
  29. #endif
  30. dWorldID world;
  31. dSpaceID space;
  32. dBodyID body1;
  33. dBodyID body2;
  34. dJointID joint1, joint2;
  35. bool applyForce = false;
  36. void start()
  37. {
  38. world = dWorldCreate();
  39. dWorldSetGravity (world,0,0,-9.8);
  40. dWorldSetDamping(world, 1e-4, 1e-5);
  41. // dWorldSetERP(world, 1);
  42. space = dSimpleSpaceCreate (0);
  43. body1 = dBodyCreate(world);
  44. body2 = dBodyCreate(world);
  45. dBodySetPosition(body1, 0, 0, 3);
  46. dBodySetPosition(body2, 0, 0, 1);
  47. dGeomID g;
  48. dMass mass;
  49. g = dCreateBox(space, 0.2, 0.2, 1);
  50. dGeomSetBody(g, body1);
  51. dMassSetBox(&mass, 1, 0.2, 0.2, 1);
  52. dBodySetMass(body1, &mass);
  53. g = dCreateBox(space, 0.2, 0.2, 1);
  54. dGeomSetBody(g, body2);
  55. dMassSetBox(&mass, 1, 0.2, 0.2, 1);
  56. dBodySetMass(body2, &mass);
  57. #if 1
  58. joint1 = dJointCreateDHinge(world, 0);
  59. dJointAttach(joint1, body1, 0);
  60. dJointSetDHingeAxis(joint1, 0, 1, 0);
  61. dJointSetDHingeAnchor1(joint1, 0, 0, 3.5);
  62. dJointSetDHingeAnchor2(joint1, 0, 0, 4.5);
  63. #endif
  64. #if 1
  65. joint2 = dJointCreateDHinge(world, 0);
  66. dJointAttach(joint2, body1, body2);
  67. dJointSetDHingeAxis(joint2, 1, 0, 0);
  68. dJointSetDHingeAnchor1(joint2, 0, 0, 2.5);
  69. dJointSetDHingeAnchor2(joint2, 0, 0, 1.5);
  70. #else
  71. joint2 = dJointCreateDBall(world, 0);
  72. dJointAttach(joint2, body1, body2);
  73. dJointSetDBallAnchor1(joint2, 0, 0, 2.5);
  74. dJointSetDBallAnchor2(joint2, 0, 0, 1.5);
  75. #endif
  76. //dBodyAddForce(body1, 20, 0, 0);
  77. // initial camera position
  78. static float xyz[3] = {3.8966, -2.0614, 4.0300};
  79. static float hpr[3] = {153.5, -16.5, 0};
  80. dsSetViewpoint (xyz,hpr);
  81. }
  82. void stop()
  83. {
  84. dSpaceDestroy(space);
  85. dWorldDestroy(world);
  86. }
  87. void drawGeom(dGeomID g)
  88. {
  89. int gclass = dGeomGetClass(g);
  90. const dReal *pos = dGeomGetPosition(g);
  91. const dReal *rot = dGeomGetRotation(g);
  92. switch (gclass) {
  93. case dBoxClass:
  94. {
  95. dVector3 lengths;
  96. if (applyForce)
  97. dsSetColor(1, .5, 0);
  98. else
  99. dsSetColor(1, 1, 0);
  100. dsSetTexture (DS_WOOD);
  101. dGeomBoxGetLengths(g, lengths);
  102. dsDrawBox(pos, rot, lengths);
  103. break;
  104. }
  105. default:
  106. {}
  107. }
  108. }
  109. void simLoop(int pause)
  110. {
  111. if (!pause) {
  112. static dReal t = 0;
  113. const dReal step = 0.005;
  114. const unsigned nsteps = 2;
  115. for (unsigned i=0; i<nsteps; ++i) {
  116. applyForce = fmodf(t, 3.) > 2.;
  117. if (applyForce) {
  118. dReal f = 0.3 * sin(t*1.2);
  119. dBodyAddForceAtRelPos(body1,
  120. f, 0, 0,
  121. 0, 0, -0.5); // at the lower end
  122. dReal g = 0.3 * sin(t*0.7);
  123. dBodyAddForceAtRelPos(body2,
  124. 0, g, 0,
  125. 0, 0, -0.5); // at the lower end
  126. }
  127. t += step;
  128. if (t > 20.)
  129. t = 0.;
  130. dWorldQuickStep(world, step);
  131. }
  132. }
  133. // now we draw everything
  134. unsigned ngeoms = dSpaceGetNumGeoms(space);
  135. for (unsigned i=0; i<ngeoms; ++i) {
  136. dGeomID g = dSpaceGetGeom(space, i);
  137. drawGeom(g);
  138. }
  139. #if 1
  140. dVector3 a11, a12;
  141. dJointGetDHingeAnchor1(joint1, a11);
  142. dJointGetDHingeAnchor2(joint1, a12);
  143. dsSetColor(1, 0, 0);
  144. dsDrawLine(a11, a12);
  145. //printf("Error 1: %f\n", fabs(dJointGetDHingeDistance(joint1) - dCalcPointsDistance3(a11, a12)));
  146. #endif
  147. #if 1
  148. dVector3 a21, a22;
  149. dJointGetDHingeAnchor1(joint2, a21);
  150. dJointGetDHingeAnchor2(joint2, a22);
  151. dsSetColor(0, 1, 0);
  152. dsDrawLine(a21, a22);
  153. //printf("Error 2: %f\n", fabs(dJointGetDHingeDistance(joint2) - dCalcPointsDistance3(a21, a22)));
  154. #endif
  155. }
  156. int main(int argc, char **argv)
  157. {
  158. // setup pointers to drawstuff callback functions
  159. dsFunctions fn;
  160. fn.version = DS_VERSION;
  161. fn.start = &start;
  162. fn.step = &simLoop;
  163. fn.command = 0;
  164. fn.stop = stop;
  165. fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
  166. // create world
  167. dInitODE();
  168. // run demo
  169. dsSimulationLoop (argc, argv, 800, 600, &fn);
  170. dCloseODE();
  171. return 0;
  172. }