test_buggy.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. buggy with suspension.
  24. this also shows you how to use geom groups.
  25. */
  26. #include <ode/ode.h>
  27. #include <drawstuff/drawstuff.h>
  28. #ifdef _MSC_VER
  29. #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
  30. #endif
  31. // select correct drawing functions
  32. #ifdef dDOUBLE
  33. #define dsDrawBox dsDrawBoxD
  34. #define dsDrawSphere dsDrawSphereD
  35. #define dsDrawCylinder dsDrawCylinderD
  36. #define dsDrawCappedCylinder dsDrawCappedCylinderD
  37. #endif
  38. // some constants
  39. #define LENGTH 0.7 // chassis length
  40. #define WIDTH 0.5 // chassis width
  41. #define HEIGHT 0.2 // chassis height
  42. #define RADIUS 0.18 // wheel radius
  43. #define STARTZ 0.5 // starting height of chassis
  44. #define CMASS 1 // chassis mass
  45. #define WMASS 0.2 // wheel mass
  46. // dynamics and collision objects (chassis, 3 wheels, environment)
  47. static dWorldID world;
  48. static dSpaceID space;
  49. static dBodyID body[4];
  50. static dJointID joint[3]; // joint[0] is the front wheel
  51. static dJointGroupID contactgroup;
  52. static dGeomID ground;
  53. static dSpaceID car_space;
  54. static dGeomID box[1];
  55. static dGeomID sphere[3];
  56. static dGeomID ground_box;
  57. // things that the user controls
  58. static dReal speed=0,steer=0; // user commands
  59. // this is called by dSpaceCollide when two objects in space are
  60. // potentially colliding.
  61. static void nearCallback (void *data, dGeomID o1, dGeomID o2)
  62. {
  63. int i,n;
  64. // only collide things with the ground
  65. int g1 = (o1 == ground || o1 == ground_box);
  66. int g2 = (o2 == ground || o2 == ground_box);
  67. if (!(g1 ^ g2)) return;
  68. const int N = 10;
  69. dContact contact[N];
  70. n = dCollide (o1,o2,N,&contact[0].geom,sizeof(dContact));
  71. if (n > 0) {
  72. for (i=0; i<n; i++) {
  73. contact[i].surface.mode = dContactSlip1 | dContactSlip2 |
  74. dContactSoftERP | dContactSoftCFM | dContactApprox1;
  75. contact[i].surface.mu = dInfinity;
  76. contact[i].surface.slip1 = 0.1;
  77. contact[i].surface.slip2 = 0.1;
  78. contact[i].surface.soft_erp = 0.5;
  79. contact[i].surface.soft_cfm = 0.3;
  80. dJointID c = dJointCreateContact (world,contactgroup,&contact[i]);
  81. dJointAttach (c,
  82. dGeomGetBody(contact[i].geom.g1),
  83. dGeomGetBody(contact[i].geom.g2));
  84. }
  85. }
  86. }
  87. // start simulation - set viewpoint
  88. static void start()
  89. {
  90. static float xyz[3] = {0.8317f,-0.9817f,0.8000f};
  91. static float hpr[3] = {121.0000f,-27.5000f,0.0000f};
  92. dsSetViewpoint (xyz,hpr);
  93. printf ("Press:\t'a' to increase speed.\n"
  94. "\t'z' to decrease speed.\n"
  95. "\t',' to steer left.\n"
  96. "\t'.' to steer right.\n"
  97. "\t' ' to reset speed and steering.\n");
  98. }
  99. // called when a key pressed
  100. static void command (int cmd)
  101. {
  102. switch (cmd) {
  103. case 'a': case 'A':
  104. speed += 0.3;
  105. break;
  106. case 'z': case 'Z':
  107. speed -= 0.3;
  108. break;
  109. case ',':
  110. steer -= 0.5;
  111. break;
  112. case '.':
  113. steer += 0.5;
  114. break;
  115. case ' ':
  116. speed = 0;
  117. steer = 0;
  118. break;
  119. }
  120. }
  121. // simulation loop
  122. static void simLoop (int pause)
  123. {
  124. int i;
  125. if (!pause) {
  126. // motor
  127. dJointSetHinge2Param (joint[0],dParamVel2,-speed);
  128. dJointSetHinge2Param (joint[0],dParamFMax2,0.1);
  129. // steering
  130. dReal v = steer - dJointGetHinge2Angle1 (joint[0]);
  131. if (v > 0.1) v = 0.1;
  132. if (v < -0.1) v = -0.1;
  133. v *= 10.0;
  134. dJointSetHinge2Param (joint[0],dParamVel,v);
  135. dJointSetHinge2Param (joint[0],dParamFMax,0.2);
  136. dJointSetHinge2Param (joint[0],dParamLoStop,-0.75);
  137. dJointSetHinge2Param (joint[0],dParamHiStop,0.75);
  138. dJointSetHinge2Param (joint[0],dParamFudgeFactor,0.1);
  139. dSpaceCollide (space,0,&nearCallback);
  140. dWorldStep (world,0.05);
  141. // remove all contact joints
  142. dJointGroupEmpty (contactgroup);
  143. }
  144. dsSetColor (0,1,1);
  145. dsSetTexture (DS_WOOD);
  146. dReal sides[3] = {LENGTH,WIDTH,HEIGHT};
  147. dsDrawBox (dBodyGetPosition(body[0]),dBodyGetRotation(body[0]),sides);
  148. dsSetColor (1,1,1);
  149. for (i=1; i<=3; i++) dsDrawCylinder (dBodyGetPosition(body[i]),
  150. dBodyGetRotation(body[i]),0.02f,RADIUS);
  151. dVector3 ss;
  152. dGeomBoxGetLengths (ground_box,ss);
  153. dsDrawBox (dGeomGetPosition(ground_box),dGeomGetRotation(ground_box),ss);
  154. /*
  155. printf ("%.10f %.10f %.10f %.10f\n",
  156. dJointGetHingeAngle (joint[1]),
  157. dJointGetHingeAngle (joint[2]),
  158. dJointGetHingeAngleRate (joint[1]),
  159. dJointGetHingeAngleRate (joint[2]));
  160. */
  161. }
  162. int main (int argc, char **argv)
  163. {
  164. int i;
  165. dMass m;
  166. // setup pointers to drawstuff callback functions
  167. dsFunctions fn;
  168. fn.version = DS_VERSION;
  169. fn.start = &start;
  170. fn.step = &simLoop;
  171. fn.command = &command;
  172. fn.stop = 0;
  173. fn.path_to_textures = "../../drawstuff/textures";
  174. if(argc==2)
  175. {
  176. fn.path_to_textures = argv[1];
  177. }
  178. // create world
  179. world = dWorldCreate();
  180. space = dHashSpaceCreate (0);
  181. contactgroup = dJointGroupCreate (0);
  182. dWorldSetGravity (world,0,0,-0.5);
  183. ground = dCreatePlane (space,0,0,1,0);
  184. // chassis body
  185. body[0] = dBodyCreate (world);
  186. dBodySetPosition (body[0],0,0,STARTZ);
  187. dMassSetBox (&m,1,LENGTH,WIDTH,HEIGHT);
  188. dMassAdjust (&m,CMASS);
  189. dBodySetMass (body[0],&m);
  190. box[0] = dCreateBox (0,LENGTH,WIDTH,HEIGHT);
  191. dGeomSetBody (box[0],body[0]);
  192. // wheel bodies
  193. for (i=1; i<=3; i++) {
  194. body[i] = dBodyCreate (world);
  195. dQuaternion q;
  196. dQFromAxisAndAngle (q,1,0,0,M_PI*0.5);
  197. dBodySetQuaternion (body[i],q);
  198. dMassSetSphere (&m,1,RADIUS);
  199. dMassAdjust (&m,WMASS);
  200. dBodySetMass (body[i],&m);
  201. sphere[i-1] = dCreateSphere (0,RADIUS);
  202. dGeomSetBody (sphere[i-1],body[i]);
  203. }
  204. dBodySetPosition (body[1],0.5*LENGTH,0,STARTZ-HEIGHT*0.5);
  205. dBodySetPosition (body[2],-0.5*LENGTH, WIDTH*0.5,STARTZ-HEIGHT*0.5);
  206. dBodySetPosition (body[3],-0.5*LENGTH,-WIDTH*0.5,STARTZ-HEIGHT*0.5);
  207. // front wheel hinge
  208. /*
  209. joint[0] = dJointCreateHinge2 (world,0);
  210. dJointAttach (joint[0],body[0],body[1]);
  211. const dReal *a = dBodyGetPosition (body[1]);
  212. dJointSetHinge2Anchor (joint[0],a[0],a[1],a[2]);
  213. dJointSetHinge2Axis1 (joint[0],0,0,1);
  214. dJointSetHinge2Axis2 (joint[0],0,1,0);
  215. */
  216. // front and back wheel hinges
  217. for (i=0; i<3; i++) {
  218. joint[i] = dJointCreateHinge2 (world,0);
  219. dJointAttach (joint[i],body[0],body[i+1]);
  220. const dReal *a = dBodyGetPosition (body[i+1]);
  221. dJointSetHinge2Anchor (joint[i],a[0],a[1],a[2]);
  222. dJointSetHinge2Axis1 (joint[i],0,0,1);
  223. dJointSetHinge2Axis2 (joint[i],0,1,0);
  224. // breakable joints contribution
  225. // the wheels can break
  226. dJointSetBreakable (joint[i], 1);
  227. // the wheels wil break at a specific force
  228. dJointSetBreakMode (joint[i], dJOINT_BREAK_AT_B1_FORCE|dJOINT_BREAK_AT_B2_FORCE);
  229. // specify the force for the first body connected to the joint ...
  230. dJointSetBreakForce (joint[i], 0, 1.5, 1.5, 1.5);
  231. // and for the second body
  232. dJointSetBreakForce (joint[i], 1, 1.5, 1.5, 1.5);
  233. }
  234. // set joint suspension
  235. for (i=0; i<3; i++) {
  236. dJointSetHinge2Param (joint[i],dParamSuspensionERP,0.4);
  237. dJointSetHinge2Param (joint[i],dParamSuspensionCFM,0.8);
  238. }
  239. // lock back wheels along the steering axis
  240. for (i=1; i<3; i++) {
  241. // set stops to make sure wheels always stay in alignment
  242. dJointSetHinge2Param (joint[i],dParamLoStop,0);
  243. dJointSetHinge2Param (joint[i],dParamHiStop,0);
  244. // the following alternative method is no good as the wheels may get out
  245. // of alignment:
  246. // dJointSetHinge2Param (joint[i],dParamVel,0);
  247. // dJointSetHinge2Param (joint[i],dParamFMax,dInfinity);
  248. }
  249. // create car space and add it to the top level space
  250. car_space = dSimpleSpaceCreate (space);
  251. dSpaceSetCleanup (car_space,0);
  252. dSpaceAdd (car_space,box[0]);
  253. dSpaceAdd (car_space,sphere[0]);
  254. dSpaceAdd (car_space,sphere[1]);
  255. dSpaceAdd (car_space,sphere[2]);
  256. // environment
  257. ground_box = dCreateBox (space,2,1.5,1);
  258. dMatrix3 R;
  259. dRFromAxisAndAngle (R,0,1,0,-0.15);
  260. dGeomSetPosition (ground_box,2,0,-0.34);
  261. dGeomSetRotation (ground_box,R);
  262. // run simulation
  263. dsSimulationLoop (argc,argv,352,288,&fn);
  264. dJointGroupDestroy (contactgroup);
  265. dSpaceDestroy (space);
  266. dWorldDestroy (world);
  267. dGeomDestroy (box[0]);
  268. dGeomDestroy (sphere[0]);
  269. dGeomDestroy (sphere[1]);
  270. dGeomDestroy (sphere[2]);
  271. return 0;
  272. }