demo_step.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // test the step function by comparing the output of the fast and the slow
  23. // version, for various systems. currently you have to define COMPARE_METHODS
  24. // in step.cpp for this to work properly.
  25. //
  26. // @@@ report MAX error
  27. #include <time.h>
  28. #include <ode/ode.h>
  29. #include <drawstuff/drawstuff.h>
  30. #include "texturepath.h"
  31. #ifdef _MSC_VER
  32. #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
  33. #endif
  34. // select correct drawing functions
  35. #ifdef dDOUBLE
  36. #define dsDrawBox dsDrawBoxD
  37. #define dsDrawSphere dsDrawSphereD
  38. #define dsDrawCylinder dsDrawCylinderD
  39. #define dsDrawCapsule dsDrawCapsuleD
  40. #endif
  41. // some constants
  42. #define NUM 10 // number of bodies
  43. #define NUMJ 9 // number of joints
  44. #define SIDE (0.2) // side length of a box
  45. #define MASS (1.0) // mass of a box
  46. #define RADIUS (0.1732f) // sphere radius
  47. // dynamics and collision objects
  48. static dWorldID world=0;
  49. static dBodyID body[NUM];
  50. static dJointID joint[NUMJ];
  51. // create the test system
  52. void createTest()
  53. {
  54. int i,j;
  55. if (world) dWorldDestroy (world);
  56. world = dWorldCreate();
  57. // create random bodies
  58. for (i=0; i<NUM; i++) {
  59. // create bodies at random position and orientation
  60. body[i] = dBodyCreate (world);
  61. dBodySetPosition (body[i],dRandReal()*2-1,dRandReal()*2-1,
  62. dRandReal()*2+RADIUS);
  63. dReal q[4];
  64. for (j=0; j<4; j++) q[j] = dRandReal()*2-1;
  65. dBodySetQuaternion (body[i],q);
  66. // set random velocity
  67. dBodySetLinearVel (body[i], dRandReal()*2-1,dRandReal()*2-1,
  68. dRandReal()*2-1);
  69. dBodySetAngularVel (body[i], dRandReal()*2-1,dRandReal()*2-1,
  70. dRandReal()*2-1);
  71. // set random mass (random diagonal mass rotated by a random amount)
  72. dMass m;
  73. dMatrix3 R;
  74. dMassSetBox (&m,1,dRandReal()+0.1,dRandReal()+0.1,dRandReal()+0.1);
  75. dMassAdjust (&m,dRandReal()+1);
  76. for (j=0; j<4; j++) q[j] = dRandReal()*2-1;
  77. dQtoR (q,R);
  78. dMassRotate (&m,R);
  79. dBodySetMass (body[i],&m);
  80. }
  81. // create ball-n-socket joints at random positions, linking random bodies
  82. // (but make sure not to link the same pair of bodies twice)
  83. char linked[NUM*NUM];
  84. for (i=0; i<NUM*NUM; i++) linked[i] = 0;
  85. for (i=0; i<NUMJ; i++) {
  86. int b1,b2;
  87. do {
  88. b1 = dRandInt (NUM);
  89. b2 = dRandInt (NUM);
  90. } while (linked[b1*NUM + b2] || b1==b2);
  91. linked[b1*NUM + b2] = 1;
  92. linked[b2*NUM + b1] = 1;
  93. joint[i] = dJointCreateBall (world,0);
  94. dJointAttach (joint[i],body[b1],body[b2]);
  95. dJointSetBallAnchor (joint[i],dRandReal()*2-1,
  96. dRandReal()*2-1,dRandReal()*2+RADIUS);
  97. }
  98. for (i=0; i<NUM; i++) {
  99. // move bodies a bit to get some joint error
  100. const dReal *pos = dBodyGetPosition (body[i]);
  101. dBodySetPosition (body[i],pos[0]+dRandReal()*0.2-0.1,
  102. pos[1]+dRandReal()*0.2-0.1,pos[2]+dRandReal()*0.2-0.1);
  103. }
  104. }
  105. // start simulation - set viewpoint
  106. static void start()
  107. {
  108. dAllocateODEDataForThread(dAllocateMaskAll);
  109. float xyz[3] = {2.6117f,-1.4433f,2.3700f};
  110. float hpr[3] = {151.5000f,-25.5000f,0.0000f};
  111. dsSetViewpoint (xyz,hpr);
  112. }
  113. // simulation loop
  114. static void simLoop (int pause)
  115. {
  116. if (!pause) {
  117. // add random forces and torques to all bodies
  118. int i;
  119. const dReal scale1 = 5;
  120. const dReal scale2 = 5;
  121. for (i=0; i<NUM; i++) {
  122. dBodyAddForce (body[i],
  123. scale1*(dRandReal()*2-1),
  124. scale1*(dRandReal()*2-1),
  125. scale1*(dRandReal()*2-1));
  126. dBodyAddTorque (body[i],
  127. scale2*(dRandReal()*2-1),
  128. scale2*(dRandReal()*2-1),
  129. scale2*(dRandReal()*2-1));
  130. }
  131. dWorldStep (world,0.05);
  132. createTest();
  133. }
  134. // float sides[3] = {SIDE,SIDE,SIDE};
  135. dsSetColor (1,1,0);
  136. for (int i=0; i<NUM; i++)
  137. dsDrawSphere (dBodyGetPosition(body[i]), dBodyGetRotation(body[i]),RADIUS);
  138. }
  139. int main (int argc, char **argv)
  140. {
  141. // setup pointers to drawstuff callback functions
  142. dsFunctions fn;
  143. fn.version = DS_VERSION;
  144. fn.start = &start;
  145. fn.step = &simLoop;
  146. fn.command = 0;
  147. fn.stop = 0;
  148. fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
  149. dInitODE2(0);
  150. dRandSetSeed (time(0));
  151. createTest();
  152. // run simulation
  153. dsSimulationLoop (argc, argv, DS_SIMULATION_DEFAULT_WIDTH, DS_SIMULATION_DEFAULT_HEIGHT, &fn);
  154. dWorldDestroy (world);
  155. dCloseODE();
  156. return 0;
  157. }