demo_buggy.cpp 8.8 KB

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