demo_buggy.cpp 9.2 KB

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