demo_I.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. /*
  23. test that the rotational physics is correct.
  24. an "anchor body" has a number of other randomly positioned bodies
  25. ("particles") attached to it by ball-and-socket joints, giving it some
  26. random effective inertia tensor. the effective inertia matrix is calculated,
  27. and then this inertia is assigned to another "test" body. a random torque is
  28. applied to both bodies and the difference in angular velocity and orientation
  29. is observed after a number of iterations.
  30. typical errors for each test cycle are about 1e-5 ... 1e-4.
  31. */
  32. #include <time.h>
  33. #include <ode/ode.h>
  34. #include <drawstuff/drawstuff.h>
  35. #include "texturepath.h"
  36. #ifdef _MSC_VER
  37. #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
  38. #endif
  39. // select correct drawing functions
  40. #ifdef dDOUBLE
  41. #define dsDrawBox dsDrawBoxD
  42. #define dsDrawSphere dsDrawSphereD
  43. #define dsDrawCylinder dsDrawCylinderD
  44. #define dsDrawCapsule dsDrawCapsuleD
  45. #endif
  46. // some constants
  47. #define NUM 10 // number of particles
  48. #define SIDE 0.1 // visual size of the particles
  49. // dynamics objects an globals
  50. static dWorldID world=0;
  51. static dBodyID anchor_body,particle[NUM],test_body;
  52. static dJointID particle_joint[NUM];
  53. static dReal torque[3];
  54. static int iteration;
  55. // start simulation - set viewpoint
  56. static void start()
  57. {
  58. dAllocateODEDataForThread(dAllocateMaskAll);
  59. static float xyz[3] = {1.5572f,-1.8886f,1.5700f};
  60. static float hpr[3] = {118.5000f,-17.0000f,0.0000f};
  61. dsSetViewpoint (xyz,hpr);
  62. }
  63. // compute the mass parameters of a particle set. q = particle positions,
  64. // pm = particle masses
  65. #define _I(i,j) I[(i)*4+(j)]
  66. void computeMassParams (dMass *m, dReal q[NUM][3], dReal pm[NUM])
  67. {
  68. int i,j;
  69. dMassSetZero (m);
  70. for (i=0; i<NUM; i++) {
  71. m->mass += pm[i];
  72. for (j=0; j<3; j++) m->c[j] += pm[i]*q[i][j];
  73. m->_I(0,0) += pm[i]*(q[i][1]*q[i][1] + q[i][2]*q[i][2]);
  74. m->_I(1,1) += pm[i]*(q[i][0]*q[i][0] + q[i][2]*q[i][2]);
  75. m->_I(2,2) += pm[i]*(q[i][0]*q[i][0] + q[i][1]*q[i][1]);
  76. m->_I(0,1) -= pm[i]*(q[i][0]*q[i][1]);
  77. m->_I(0,2) -= pm[i]*(q[i][0]*q[i][2]);
  78. m->_I(1,2) -= pm[i]*(q[i][1]*q[i][2]);
  79. }
  80. for (j=0; j<3; j++) m->c[j] /= m->mass;
  81. m->_I(1,0) = m->_I(0,1);
  82. m->_I(2,0) = m->_I(0,2);
  83. m->_I(2,1) = m->_I(1,2);
  84. }
  85. void reset_test()
  86. {
  87. int i;
  88. dMass m,anchor_m;
  89. dReal q[NUM][3], pm[NUM]; // particle positions and masses
  90. dReal pos1[3] = {1,0,1}; // point of reference (POR)
  91. dReal pos2[3] = {-1,0,1}; // point of reference (POR)
  92. // make random particle positions (relative to POR) and masses
  93. for (i=0; i<NUM; i++) {
  94. pm[i] = dRandReal()+0.1;
  95. q[i][0] = dRandReal()-0.5;
  96. q[i][1] = dRandReal()-0.5;
  97. q[i][2] = dRandReal()-0.5;
  98. }
  99. // adjust particle positions so centor of mass = POR
  100. computeMassParams (&m,q,pm);
  101. for (i=0; i<NUM; i++) {
  102. q[i][0] -= m.c[0];
  103. q[i][1] -= m.c[1];
  104. q[i][2] -= m.c[2];
  105. }
  106. if (world) dWorldDestroy (world);
  107. world = dWorldCreate();
  108. anchor_body = dBodyCreate (world);
  109. dBodySetPosition (anchor_body,pos1[0],pos1[1],pos1[2]);
  110. dMassSetBox (&anchor_m,1,SIDE,SIDE,SIDE);
  111. dMassAdjust (&anchor_m,0.1);
  112. dBodySetMass (anchor_body,&anchor_m);
  113. for (i=0; i<NUM; i++) {
  114. particle[i] = dBodyCreate (world);
  115. dBodySetPosition (particle[i],
  116. pos1[0]+q[i][0],pos1[1]+q[i][1],pos1[2]+q[i][2]);
  117. dMassSetBox (&m,1,SIDE,SIDE,SIDE);
  118. dMassAdjust (&m,pm[i]);
  119. dBodySetMass (particle[i],&m);
  120. }
  121. for (i=0; i < NUM; i++) {
  122. particle_joint[i] = dJointCreateBall (world,0);
  123. dJointAttach (particle_joint[i],anchor_body,particle[i]);
  124. const dReal *p = dBodyGetPosition (particle[i]);
  125. dJointSetBallAnchor (particle_joint[i],p[0],p[1],p[2]);
  126. }
  127. // make test_body with the same mass and inertia of the anchor_body plus
  128. // all the particles
  129. test_body = dBodyCreate (world);
  130. dBodySetPosition (test_body,pos2[0],pos2[1],pos2[2]);
  131. computeMassParams (&m,q,pm);
  132. m.mass += anchor_m.mass;
  133. for (i=0; i<12; i++) m.I[i] = m.I[i] + anchor_m.I[i];
  134. dBodySetMass (test_body,&m);
  135. // rotate the test and anchor bodies by a random amount
  136. dQuaternion qrot;
  137. for (i=0; i<4; i++) qrot[i] = dRandReal()-0.5;
  138. dNormalize4 (qrot);
  139. dBodySetQuaternion (anchor_body,qrot);
  140. dBodySetQuaternion (test_body,qrot);
  141. dMatrix3 R;
  142. dQtoR (qrot,R);
  143. for (i=0; i<NUM; i++) {
  144. dVector3 v;
  145. dMultiply0 (v,R,&q[i][0],3,3,1);
  146. dBodySetPosition (particle[i],pos1[0]+v[0],pos1[1]+v[1],pos1[2]+v[2]);
  147. }
  148. // set random torque
  149. for (i=0; i<3; i++) torque[i] = (dRandReal()-0.5) * 0.1;
  150. iteration=0;
  151. }
  152. // simulation loop
  153. static void simLoop (int pause)
  154. {
  155. if (!pause) {
  156. dBodyAddTorque (anchor_body,torque[0],torque[1],torque[2]);
  157. dBodyAddTorque (test_body,torque[0],torque[1],torque[2]);
  158. dWorldStep (world,0.03);
  159. iteration++;
  160. if (iteration >= 100) {
  161. // measure the difference between the anchor and test bodies
  162. const dReal *w1 = dBodyGetAngularVel (anchor_body);
  163. const dReal *w2 = dBodyGetAngularVel (test_body);
  164. const dReal *q1 = dBodyGetQuaternion (anchor_body);
  165. const dReal *q2 = dBodyGetQuaternion (test_body);
  166. dReal maxdiff = dMaxDifference (w1,w2,1,3);
  167. printf ("w-error = %.4e (%.2f,%.2f,%.2f) and (%.2f,%.2f,%.2f)\n",
  168. maxdiff,w1[0],w1[1],w1[2],w2[0],w2[1],w2[2]);
  169. maxdiff = dMaxDifference (q1,q2,1,4);
  170. printf ("q-error = %.4e\n",maxdiff);
  171. reset_test();
  172. }
  173. }
  174. dReal sides[3] = {SIDE,SIDE,SIDE};
  175. dReal sides2[3] = {6*SIDE,6*SIDE,6*SIDE};
  176. dReal sides3[3] = {3*SIDE,3*SIDE,3*SIDE};
  177. dsSetColor (1,1,1);
  178. dsDrawBox (dBodyGetPosition(anchor_body), dBodyGetRotation(anchor_body),
  179. sides3);
  180. dsSetColor (1,0,0);
  181. dsDrawBox (dBodyGetPosition(test_body), dBodyGetRotation(test_body), sides2);
  182. dsSetColor (1,1,0);
  183. for (int i=0; i<NUM; i++)
  184. dsDrawBox (dBodyGetPosition (particle[i]),
  185. dBodyGetRotation (particle[i]), sides);
  186. }
  187. int main (int argc, char **argv)
  188. {
  189. // setup pointers to drawstuff callback functions
  190. dsFunctions fn;
  191. fn.version = DS_VERSION;
  192. fn.start = &start;
  193. fn.step = &simLoop;
  194. fn.command = 0;
  195. fn.stop = 0;
  196. fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
  197. dInitODE2(0);
  198. dRandSetSeed (time(0));
  199. reset_test();
  200. // run simulation
  201. dsSimulationLoop (argc,argv,352,288,&fn);
  202. dWorldDestroy (world);
  203. dCloseODE();
  204. return 0;
  205. }