demo_friction.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 the Coulomb friction approximation.
  24. a 10x10 array of boxes is made, each of which rests on the ground.
  25. a horizantal force is applied to each box to try and get it to slide.
  26. box[i][j] has a mass (i+1)*MASS and a force (j+1)*FORCE. by the Coloumb
  27. friction model, the box should only slide if the force is greater than MU
  28. times the contact normal force, i.e.
  29. f > MU * body_mass * GRAVITY
  30. (j+1)*FORCE > MU * (i+1)*MASS * GRAVITY
  31. (j+1) > (i+1) * (MU*MASS*GRAVITY/FORCE)
  32. (j+1) > (i+1) * k
  33. this should be independent of the number of contact points, as N contact
  34. points will each have 1/N'th the normal force but the pushing force will
  35. have to overcome N contacts. the constants are chosen so that k=1.
  36. thus you should see a triangle made of half the bodies in the array start to
  37. slide.
  38. */
  39. #include <ode/ode.h>
  40. #include <drawstuff/drawstuff.h>
  41. #include "texturepath.h"
  42. #ifdef _MSC_VER
  43. #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
  44. #endif
  45. // select correct drawing functions
  46. #ifdef dDOUBLE
  47. #define dsDrawBox dsDrawBoxD
  48. #define dsDrawSphere dsDrawSphereD
  49. #define dsDrawCylinder dsDrawCylinderD
  50. #define dsDrawCapsule dsDrawCapsuleD
  51. #endif
  52. // some constants
  53. #define LENGTH 0.2 // box length & width
  54. #define HEIGHT 0.05 // box height
  55. #define MASS 0.2 // mass of box[i][j] = (i+1) * MASS
  56. #define FORCE 0.05 // force applied to box[i][j] = (j+1) * FORCE
  57. #define MU 0.5 // the global mu to use
  58. #define GRAVITY 0.5 // the global gravity to use
  59. #define N1 10 // number of different forces to try
  60. #define N2 10 // number of different masses to try
  61. // dynamics and collision objects
  62. static dWorldID world;
  63. static dSpaceID space;
  64. static dBodyID body[N1][N2];
  65. static dJointGroupID contactgroup;
  66. static dGeomID ground;
  67. static dGeomID box[N1][N2];
  68. // this is called by dSpaceCollide when two objects in space are
  69. // potentially colliding.
  70. static void nearCallback (void *, dGeomID o1, dGeomID o2)
  71. {
  72. int i;
  73. // only collide things with the ground
  74. int g1 = (o1 == ground);
  75. int g2 = (o2 == ground);
  76. if (!(g1 ^ g2)) return;
  77. dBodyID b1 = dGeomGetBody(o1);
  78. dBodyID b2 = dGeomGetBody(o2);
  79. dContact contact[3]; // up to 3 contacts per box
  80. for (i=0; i<3; i++) {
  81. contact[i].surface.mode = dContactSoftCFM | dContactApprox1;
  82. contact[i].surface.mu = MU;
  83. contact[i].surface.soft_cfm = 0.01;
  84. }
  85. if (int numc = dCollide (o1,o2,3,&contact[0].geom,sizeof(dContact))) {
  86. for (i=0; i<numc; i++) {
  87. dJointID c = dJointCreateContact (world,contactgroup,contact+i);
  88. dJointAttach (c,b1,b2);
  89. }
  90. }
  91. }
  92. // start simulation - set viewpoint
  93. static void start()
  94. {
  95. dAllocateODEDataForThread(dAllocateMaskAll);
  96. static float xyz[3] = {1.7772,-0.7924,2.7600};
  97. static float hpr[3] = {90.0000,-54.0000,0.0000};
  98. dsSetViewpoint (xyz,hpr);
  99. }
  100. // simulation loop
  101. static void simLoop (int pause)
  102. {
  103. int i;
  104. if (!pause) {
  105. // apply forces to all bodies
  106. for (i=0; i<N1; i++) {
  107. for (int j=0; j<N2; j++) {
  108. dBodyAddForce (body[i][j],FORCE*(i+1),0,0);
  109. }
  110. }
  111. dSpaceCollide (space,0,&nearCallback);
  112. dWorldStep (world,0.05);
  113. // remove all contact joints
  114. dJointGroupEmpty (contactgroup);
  115. }
  116. dsSetColor (1,0,1);
  117. dReal sides[3] = {LENGTH,LENGTH,HEIGHT};
  118. for (i=0; i<N1; i++) {
  119. for (int j=0; j<N2; j++) {
  120. dsDrawBox (dGeomGetPosition(box[i][j]),dGeomGetRotation(box[i][j]),
  121. sides);
  122. }
  123. }
  124. }
  125. int main (int argc, char **argv)
  126. {
  127. int i,j;
  128. dMass m;
  129. // setup pointers to drawstuff callback functions
  130. dsFunctions fn;
  131. fn.version = DS_VERSION;
  132. fn.start = &start;
  133. fn.step = &simLoop;
  134. fn.command = 0;
  135. fn.stop = 0;
  136. fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
  137. // create world
  138. dInitODE2(0);
  139. world = dWorldCreate();
  140. space = dHashSpaceCreate (0);
  141. contactgroup = dJointGroupCreate (0);
  142. dWorldSetGravity (world,0,0,-GRAVITY);
  143. ground = dCreatePlane (space,0,0,1,0);
  144. // bodies
  145. for (i=0; i<N1; i++) {
  146. for (j=0; j<N2; j++) {
  147. body[i][j] = dBodyCreate (world);
  148. dMassSetBox (&m,1,LENGTH,LENGTH,HEIGHT);
  149. dMassAdjust (&m,MASS*(j+1));
  150. dBodySetMass (body[i][j],&m);
  151. dBodySetPosition (body[i][j],i*2*LENGTH,j*2*LENGTH,HEIGHT*0.5);
  152. box[i][j] = dCreateBox (space,LENGTH,LENGTH,HEIGHT);
  153. dGeomSetBody (box[i][j],body[i][j]);
  154. }
  155. }
  156. // run simulation
  157. dsSimulationLoop (argc,argv,352,288,&fn);
  158. dJointGroupDestroy (contactgroup);
  159. dSpaceDestroy (space);
  160. dWorldDestroy (world);
  161. dCloseODE();
  162. return 0;
  163. }