demo_hinge.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 dJointID hinge;
  39. // state set by keyboard commands
  40. static int occasional_error = 0;
  41. // start simulation - set viewpoint
  42. static void start()
  43. {
  44. dAllocateODEDataForThread(dAllocateMaskAll);
  45. static float xyz[3] = {1.0382f,-1.0811f,1.4700f};
  46. static float hpr[3] = {135.0000f,-19.5000f,0.0000f};
  47. dsSetViewpoint (xyz,hpr);
  48. printf ("Press 'e' to start/stop occasional error.\n");
  49. }
  50. // called when a key pressed
  51. static void command (int cmd)
  52. {
  53. if (cmd == 'e' || cmd == 'E') {
  54. occasional_error ^= 1;
  55. }
  56. }
  57. // simulation loop
  58. static void simLoop (int pause)
  59. {
  60. const dReal kd = -0.3; // angular damping constant
  61. if (!pause) {
  62. // add an oscillating torque to body 0, and also damp its rotational motion
  63. static dReal a=0;
  64. const dReal *w = dBodyGetAngularVel (body[0]);
  65. dBodyAddTorque (body[0],kd*w[0],kd*w[1]+0.1*cos(a),kd*w[2]+0.1*sin(a));
  66. dWorldStep (world,0.05);
  67. a += 0.01;
  68. // occasionally re-orient one of the bodies to create a deliberate error.
  69. if (occasional_error) {
  70. static int count = 0;
  71. if ((count % 20)==0) {
  72. // randomly adjust orientation of body[0]
  73. const dReal *R1;
  74. dMatrix3 R2,R3;
  75. R1 = dBodyGetRotation (body[0]);
  76. dRFromAxisAndAngle (R2,dRandReal()-0.5,dRandReal()-0.5,
  77. dRandReal()-0.5,dRandReal()-0.5);
  78. dMultiply0 (R3,R1,R2,3,3,3);
  79. dBodySetRotation (body[0],R3);
  80. // randomly adjust position of body[0]
  81. const dReal *pos = dBodyGetPosition (body[0]);
  82. dBodySetPosition (body[0],
  83. pos[0]+0.2*(dRandReal()-0.5),
  84. pos[1]+0.2*(dRandReal()-0.5),
  85. pos[2]+0.2*(dRandReal()-0.5));
  86. }
  87. count++;
  88. }
  89. }
  90. dReal sides1[3] = {SIDE,SIDE,SIDE};
  91. dReal sides2[3] = {SIDE,SIDE,SIDE*0.8f};
  92. dsSetTexture (DS_WOOD);
  93. dsSetColor (1,1,0);
  94. dsDrawBox (dBodyGetPosition(body[0]),dBodyGetRotation(body[0]),sides1);
  95. dsSetColor (0,1,1);
  96. dsDrawBox (dBodyGetPosition(body[1]),dBodyGetRotation(body[1]),sides2);
  97. }
  98. int main (int argc, char **argv)
  99. {
  100. // setup pointers to drawstuff callback functions
  101. dsFunctions fn;
  102. fn.version = DS_VERSION;
  103. fn.start = &start;
  104. fn.step = &simLoop;
  105. fn.command = &command;
  106. fn.stop = 0;
  107. fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
  108. if(argc==2)
  109. {
  110. fn.path_to_textures = argv[1];
  111. }
  112. // create world
  113. dInitODE2(0);
  114. world = dWorldCreate();
  115. dMass m;
  116. dMassSetBox (&m,1,SIDE,SIDE,SIDE);
  117. dMassAdjust (&m,MASS);
  118. dQuaternion q;
  119. dQFromAxisAndAngle (q,1,1,0,0.25*M_PI);
  120. body[0] = dBodyCreate (world);
  121. dBodySetMass (body[0],&m);
  122. dBodySetPosition (body[0],0.5*SIDE,0.5*SIDE,1);
  123. dBodySetQuaternion (body[0],q);
  124. body[1] = dBodyCreate (world);
  125. dBodySetMass (body[1],&m);
  126. dBodySetPosition (body[1],-0.5*SIDE,-0.5*SIDE,1);
  127. dBodySetQuaternion (body[1],q);
  128. hinge = dJointCreateHinge (world,0);
  129. dJointAttach (hinge,body[0],body[1]);
  130. dJointSetHingeAnchor (hinge,0,0,1);
  131. dJointSetHingeAxis (hinge,1,-1,1.41421356);
  132. // run simulation
  133. dsSimulationLoop (argc,argv,352,288,&fn);
  134. dWorldDestroy (world);
  135. dCloseODE();
  136. return 0;
  137. }