demo_slider.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 slider;
  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. float xyz[3] = {1.0382f,-1.0811f,1.4700f};
  46. 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. const dReal ks = 0.5; // spring constant
  62. if (!pause) {
  63. // add an oscillating torque to body 0, and also damp its rotational motion
  64. static dReal a=0;
  65. const dReal *w = dBodyGetAngularVel (body[0]);
  66. dBodyAddTorque (body[0],kd*w[0],kd*w[1]+0.1*cos(a),kd*w[2]+0.1*sin(a));
  67. a += 0.01;
  68. // add a spring force to keep the bodies together, otherwise they will
  69. // fly apart along the slider axis.
  70. const dReal *p1 = dBodyGetPosition (body[0]);
  71. const dReal *p2 = dBodyGetPosition (body[1]);
  72. dBodyAddForce (body[0],ks*(p2[0]-p1[0]),ks*(p2[1]-p1[1]),
  73. ks*(p2[2]-p1[2]));
  74. dBodyAddForce (body[1],ks*(p1[0]-p2[0]),ks*(p1[1]-p2[1]),
  75. ks*(p1[2]-p2[2]));
  76. // occasionally re-orient one of the bodies to create a deliberate error.
  77. if (occasional_error) {
  78. static int count = 0;
  79. if ((count % 20)==0) {
  80. // randomly adjust orientation of body[0]
  81. const dReal *R1;
  82. dMatrix3 R2,R3;
  83. R1 = dBodyGetRotation (body[0]);
  84. dRFromAxisAndAngle (R2,dRandReal()-0.5,dRandReal()-0.5,
  85. dRandReal()-0.5,dRandReal()-0.5);
  86. dMultiply0 (R3,R1,R2,3,3,3);
  87. dBodySetRotation (body[0],R3);
  88. // randomly adjust position of body[0]
  89. const dReal *pos = dBodyGetPosition (body[0]);
  90. dBodySetPosition (body[0],
  91. pos[0]+0.2*(dRandReal()-0.5),
  92. pos[1]+0.2*(dRandReal()-0.5),
  93. pos[2]+0.2*(dRandReal()-0.5));
  94. }
  95. count++;
  96. }
  97. dWorldStep (world,0.05);
  98. }
  99. dReal sides1[3] = {SIDE,SIDE,SIDE};
  100. dReal sides2[3] = {SIDE*0.8f,SIDE*0.8f,SIDE*2.0f};
  101. dsSetTexture (DS_WOOD);
  102. dsSetColor (1,1,0);
  103. dsDrawBox (dBodyGetPosition(body[0]),dBodyGetRotation(body[0]),sides1);
  104. dsSetColor (0,1,1);
  105. dsDrawBox (dBodyGetPosition(body[1]),dBodyGetRotation(body[1]),sides2);
  106. }
  107. int main (int argc, char **argv)
  108. {
  109. // setup pointers to drawstuff callback functions
  110. dsFunctions fn;
  111. fn.version = DS_VERSION;
  112. fn.start = &start;
  113. fn.step = &simLoop;
  114. fn.command = &command;
  115. fn.stop = 0;
  116. fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
  117. // create world
  118. dInitODE2(0);
  119. world = dWorldCreate();
  120. dMass m;
  121. dMassSetBox (&m,1,SIDE,SIDE,SIDE);
  122. dMassAdjust (&m,MASS);
  123. body[0] = dBodyCreate (world);
  124. dBodySetMass (body[0],&m);
  125. dBodySetPosition (body[0],0,0,1);
  126. body[1] = dBodyCreate (world);
  127. dBodySetMass (body[1],&m);
  128. dQuaternion q;
  129. dQFromAxisAndAngle (q,-1,1,0,0.25*M_PI);
  130. dBodySetPosition (body[1],0.2,0.2,1.2);
  131. dBodySetQuaternion (body[1],q);
  132. slider = dJointCreateSlider (world,0);
  133. dJointAttach (slider,body[0],body[1]);
  134. dJointSetSliderAxis (slider,1,1,1);
  135. // run simulation
  136. dsSimulationLoop (argc, argv, DS_SIMULATION_DEFAULT_WIDTH, DS_SIMULATION_DEFAULT_HEIGHT, &fn);
  137. dWorldDestroy (world);
  138. dCloseODE();
  139. return 0;
  140. }