test_breakable.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 <stdlib.h>
  27. #include <ode/ode.h>
  28. #include <drawstuff/drawstuff.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 dsDrawCappedCylinder dsDrawCappedCylinderD
  38. #endif
  39. // some constants
  40. #define LENGTH 0.7 // chassis length
  41. #define WIDTH 0.4 // chassis width
  42. #define HEIGHT 0.2 // chassis height
  43. #define RADIUS 0.22 // wheel radius
  44. #define STARTZ 0.4 // starting height of chassis
  45. #define CMASS 1 // chassis mass
  46. #define WMASS 0.2 // wheel mass
  47. // dynamics and collision objects (chassis, 4 wheels, environment, obstacles, chain)
  48. static dWorldID world;
  49. static dSpaceID space;
  50. // chain stuff
  51. static const float chain_radius = 0.1;
  52. static const float chain_mass = 0.1;
  53. static const int chain_num = 10;
  54. static dBodyID chain_body[chain_num];
  55. static dGeomID chain_geom[chain_num];
  56. static dJointID chain_joint[chain_num-1];
  57. // 1 chasses, 4 wheels
  58. static dBodyID body[5];
  59. // joint[0] is left front wheel, joint[1] is right front wheel
  60. static dJointID joint[4];
  61. static int joint_exists[4];
  62. static dJointGroupID contactgroup;
  63. static dGeomID ground;
  64. static dSpaceID car_space;
  65. static dGeomID box[1];
  66. static dGeomID sphere[4];
  67. static dGeomID ground_box;
  68. static const int obstacle_num = 25;
  69. static dGeomID obstacle[obstacle_num];
  70. // things that the user controls
  71. static dReal speed=0,steer=0; // user commands
  72. // this is called by dSpaceCollide when two objects in space are
  73. // potentially colliding.
  74. static void nearCallback (void *data, dGeomID o1, dGeomID o2)
  75. {
  76. int i,n;
  77. // // do not collide objects that are connected
  78. // dBodyID b1 = dGeomGetBody (o1),
  79. // b2 = dGeomGetBody (o2);
  80. // if (b1 && b2 && dAreConnected(b1, b2)) return;
  81. const int N = 10;
  82. dContact contact[N];
  83. n = dCollide (o1,o2,N,&contact[0].geom,sizeof(dContact));
  84. if (n > 0) {
  85. for (i=0; i<n; i++) {
  86. contact[i].surface.mode = dContactSlip1 | dContactSlip2 |
  87. dContactSoftERP | dContactSoftCFM | dContactApprox1;
  88. contact[i].surface.mu = dInfinity;
  89. contact[i].surface.slip1 = 0.1;
  90. contact[i].surface.slip2 = 0.1;
  91. contact[i].surface.soft_erp = 0.5;
  92. contact[i].surface.soft_cfm = 0.3;
  93. dJointID c = dJointCreateContact (world,contactgroup,&contact[i]);
  94. dJointAttach (c,
  95. dGeomGetBody(contact[i].geom.g1),
  96. dGeomGetBody(contact[i].geom.g2));
  97. }
  98. }
  99. }
  100. // callback function for joints that break
  101. static void jointBreakCallback (dJointID j)
  102. {
  103. if (j == joint[0]) joint_exists[0] = 0;
  104. else if (j == joint[1]) joint_exists[1] = 0;
  105. else if (j == joint[2]) joint_exists[2] = 0;
  106. else if (j == joint[3]) joint_exists[3] = 0;
  107. printf ("A joint just broke\n");
  108. }
  109. // start simulation - set viewpoint
  110. static void start()
  111. {
  112. static float xyz[3] = {0.8317f,-0.9817f,0.8000f};
  113. static float hpr[3] = {121.0000f,-27.5000f,0.0000f};
  114. dsSetViewpoint (xyz,hpr);
  115. printf ("Press:\t'a' to increase speed.\n"
  116. "\t'z' to decrease speed.\n"
  117. "\t',' to steer left.\n"
  118. "\t'.' to steer right.\n"
  119. "\t' ' to reset speed and steering.\n");
  120. }
  121. // called when a key pressed
  122. static void command (int cmd)
  123. {
  124. switch (cmd) {
  125. case 'a': case 'A':
  126. speed += 0.3;
  127. break;
  128. case 'z': case 'Z':
  129. speed -= 0.3;
  130. break;
  131. case ',':
  132. steer -= 0.5;
  133. break;
  134. case '.':
  135. steer += 0.5;
  136. break;
  137. case ' ':
  138. speed = 0;
  139. steer = 0;
  140. break;
  141. }
  142. }
  143. // simulation loop
  144. static void simLoop (int pause)
  145. {
  146. int i;
  147. if (!pause) {
  148. for (i=0; i<2; i++) {
  149. if (joint_exists[i]) {
  150. // motor
  151. dJointSetHinge2Param (joint[i],dParamVel2,-speed);
  152. dJointSetHinge2Param (joint[i],dParamFMax2,0.1);
  153. // steering
  154. dReal v = steer - dJointGetHinge2Angle1 (joint[i]);
  155. if (v > 0.1) v = 0.1;
  156. if (v < -0.1) v = -0.1;
  157. v *= 10.0;
  158. dJointSetHinge2Param (joint[i],dParamVel,v);
  159. dJointSetHinge2Param (joint[i],dParamFMax,0.2);
  160. dJointSetHinge2Param (joint[i],dParamLoStop,-0.75);
  161. dJointSetHinge2Param (joint[i],dParamHiStop,0.75);
  162. dJointSetHinge2Param (joint[i],dParamFudgeFactor,0.1);
  163. }
  164. }
  165. dSpaceCollide (space,0,&nearCallback);
  166. //dWorldStep (world,0.05);
  167. dWorldStepFast1 (world,0.05,5);
  168. // remove all contact joints
  169. dJointGroupEmpty (contactgroup);
  170. }
  171. dsSetColor (0,1,1);
  172. dsSetTexture (DS_WOOD);
  173. dReal sides[3] = {LENGTH,WIDTH,HEIGHT};
  174. dsDrawBox (dBodyGetPosition(body[0]),dBodyGetRotation(body[0]),sides);
  175. dsSetColor (1,1,1);
  176. for (i=1; i<=4; i++)
  177. dsDrawCylinder (dBodyGetPosition(body[i]),
  178. dBodyGetRotation(body[i]),
  179. 0.2,
  180. RADIUS);
  181. dVector3 ss;
  182. dGeomBoxGetLengths (ground_box,ss);
  183. dsDrawBox (dGeomGetPosition(ground_box),dGeomGetRotation(ground_box),ss);
  184. dsSetColor (1,0,0);
  185. for (i=0; i<obstacle_num; i++) {
  186. dVector3 ss;
  187. dGeomBoxGetLengths (obstacle[i],ss);
  188. dsDrawBox (dGeomGetPosition(obstacle[i]),dGeomGetRotation(obstacle[i]),ss);
  189. }
  190. dsSetColor (1,1,0);
  191. for (i=0; i<chain_num; i++) {
  192. dsDrawSphere (dGeomGetPosition(chain_geom[i]),dGeomGetRotation(chain_geom[i]),chain_radius);
  193. }
  194. /*
  195. printf ("%.10f %.10f %.10f %.10f\n",
  196. dJointGetHingeAngle (joint[1]),
  197. dJointGetHingeAngle (joint[2]),
  198. dJointGetHingeAngleRate (joint[1]),
  199. dJointGetHingeAngleRate (joint[2]));
  200. */
  201. }
  202. int main (int argc, char **argv)
  203. {
  204. int i;
  205. dMass m;
  206. // setup pointers to drawstuff callback functions
  207. dsFunctions fn;
  208. fn.version = DS_VERSION;
  209. fn.start = &start;
  210. fn.step = &simLoop;
  211. fn.command = &command;
  212. fn.stop = 0;
  213. fn.path_to_textures = "../../drawstuff/textures";
  214. if(argc==2)
  215. {
  216. fn.path_to_textures = argv[1];
  217. }
  218. // create world
  219. world = dWorldCreate();
  220. space = dHashSpaceCreate (0);
  221. contactgroup = dJointGroupCreate (0);
  222. dWorldSetGravity (world,0,0,-0.5);
  223. ground = dCreatePlane (space,0,0,1,0);
  224. // chassis body
  225. body[0] = dBodyCreate (world);
  226. dBodySetPosition (body[0],0,0,STARTZ);
  227. dMassSetBox (&m,1,LENGTH,WIDTH,HEIGHT);
  228. dMassAdjust (&m,CMASS);
  229. dBodySetMass (body[0],&m);
  230. box[0] = dCreateBox (0,LENGTH,WIDTH,HEIGHT);
  231. dGeomSetBody (box[0],body[0]);
  232. // a chain
  233. for (i=0; i<chain_num; i++) {
  234. chain_body[i] = dBodyCreate (world);
  235. dBodySetPosition (chain_body[i],-LENGTH-(i*2*chain_radius),0,STARTZ-HEIGHT*0.5);
  236. dMassSetSphere (&m,1,chain_radius);
  237. dMassAdjust (&m,chain_mass);
  238. dBodySetMass (chain_body[i],&m);
  239. chain_geom[i] = dCreateSphere (space,chain_radius);
  240. dGeomSetBody (chain_geom[i],chain_body[i]);
  241. }
  242. // wheel bodies
  243. for (i=1; i<=4; i++) {
  244. body[i] = dBodyCreate (world);
  245. dQuaternion q;
  246. dQFromAxisAndAngle (q,1,0,0,M_PI*0.5);
  247. dBodySetQuaternion (body[i],q);
  248. dMassSetSphere (&m,1,RADIUS);
  249. dMassAdjust (&m,WMASS);
  250. dBodySetMass (body[i],&m);
  251. sphere[i-1] = dCreateSphere (0,RADIUS);
  252. dGeomSetBody (sphere[i-1],body[i]);
  253. }
  254. dBodySetPosition (body[1], 0.5*LENGTH, WIDTH*1.0, STARTZ-HEIGHT*0.5);
  255. dBodySetPosition (body[2], 0.5*LENGTH, -WIDTH*1.0, STARTZ-HEIGHT*0.5);
  256. dBodySetPosition (body[3], -0.5*LENGTH, WIDTH*1.0, STARTZ-HEIGHT*0.5);
  257. dBodySetPosition (body[4], -0.5*LENGTH, -WIDTH*1.0, STARTZ-HEIGHT*0.5);
  258. // front wheel hinge
  259. /*
  260. joint[0] = dJointCreateHinge2 (world,0);
  261. dJointAttach (joint[0],body[0],body[1]);
  262. const dReal *a = dBodyGetPosition (body[1]);
  263. dJointSetHinge2Anchor (joint[0],a[0],a[1],a[2]);
  264. dJointSetHinge2Axis1 (joint[0],0,0,1);
  265. dJointSetHinge2Axis2 (joint[0],0,1,0);
  266. */
  267. // front and back wheel hinges
  268. for (i=0; i<4; i++) {
  269. joint[i] = dJointCreateHinge2 (world,0);
  270. joint_exists[i] = 1;
  271. dJointAttach (joint[i],body[0],body[i+1]);
  272. const dReal *a = dBodyGetPosition (body[i+1]);
  273. dJointSetHinge2Anchor (joint[i],a[0],a[1],a[2]);
  274. dJointSetHinge2Axis1 (joint[i],0,0,1);
  275. dJointSetHinge2Axis2 (joint[i],0,1,0);
  276. // the wheels can break
  277. dJointSetBreakable (joint[i], 1);
  278. // the wheels wil break at a specific force
  279. dJointSetBreakMode (joint[i],
  280. dJOINT_BREAK_AT_B1_FORCE |
  281. dJOINT_BREAK_AT_B2_FORCE |
  282. dJOINT_DELETE_ON_BREAK);
  283. // specify the force for the first body connected to the joint ...
  284. dJointSetBreakForce (joint[i], 0, 2.5, 2.5, 2.5);
  285. // and for the second body
  286. dJointSetBreakForce (joint[i], 1, 2.5, 2.5, 2.5);
  287. // set the callback function
  288. dJointSetBreakCallback (joint[i], &jointBreakCallback);
  289. }
  290. // joints for the chain
  291. for (i=0; i<chain_num-1; i++) {
  292. chain_joint[i] = dJointCreateFixed (world,0);
  293. dJointAttach (chain_joint[i],chain_body[i+1],chain_body[i]);
  294. dJointSetFixed (chain_joint[i]);
  295. // the chain can break
  296. dJointSetBreakable (chain_joint[i], 1);
  297. // the chain wil break at a specific force
  298. dJointSetBreakMode (chain_joint[i],
  299. dJOINT_BREAK_AT_B1_FORCE |
  300. dJOINT_BREAK_AT_B2_FORCE |
  301. dJOINT_DELETE_ON_BREAK);
  302. // specify the force for the first body connected to the joint ...
  303. dJointSetBreakForce (chain_joint[i], 0, 0.5, 0.5, 0.5);
  304. // and for the second body
  305. dJointSetBreakForce (chain_joint[i], 1, 0.5, 0.5, 0.5);
  306. // set the callback function
  307. dJointSetBreakCallback (chain_joint[i], &jointBreakCallback);
  308. }
  309. // set joint suspension
  310. for (i=0; i<4; i++) {
  311. dJointSetHinge2Param (joint[i],dParamSuspensionERP,0.4);
  312. dJointSetHinge2Param (joint[i],dParamSuspensionCFM,0.1);
  313. }
  314. // lock back wheels along the steering axis
  315. for (i=1; i<4; i++) {
  316. // set stops to make sure wheels always stay in alignment
  317. dJointSetHinge2Param (joint[i],dParamLoStop,0);
  318. dJointSetHinge2Param (joint[i],dParamHiStop,0);
  319. // the following alternative method is no good as the wheels may get out
  320. // of alignment:
  321. // dJointSetHinge2Param (joint[i],dParamVel,0);
  322. // dJointSetHinge2Param (joint[i],dParamFMax,dInfinity);
  323. }
  324. // create car space and add it to the top level space
  325. car_space = dSimpleSpaceCreate (space);
  326. dSpaceSetCleanup (car_space,0);
  327. dSpaceAdd (car_space,box[0]);
  328. dSpaceAdd (car_space,sphere[0]);
  329. dSpaceAdd (car_space,sphere[1]);
  330. dSpaceAdd (car_space,sphere[2]);
  331. // environment
  332. ground_box = dCreateBox (space,2,1.5,1);
  333. dMatrix3 R;
  334. dRFromAxisAndAngle (R,0,1,0,-0.15);
  335. dGeomSetPosition (ground_box,2,0,-0.34);
  336. dGeomSetRotation (ground_box,R);
  337. // obstacles
  338. for (i=0; i<obstacle_num; i++) {
  339. dReal height = 0.1+(dReal(rand()%10)/10.0);
  340. obstacle[i] = dCreateBox (space,0.2,0.2,height);
  341. dGeomSetPosition (
  342. obstacle[i],
  343. (rand()%20)-10,
  344. (rand()%20)-10,
  345. height/2.0);
  346. }
  347. // run simulation
  348. dsSimulationLoop (argc,argv,352,288,&fn);
  349. dJointGroupDestroy (contactgroup);
  350. dSpaceDestroy (space);
  351. dWorldDestroy (world);
  352. dGeomDestroy (box[0]);
  353. for (i=0; i<4; i++)
  354. dGeomDestroy (sphere[i]);
  355. dCloseODE ();
  356. return 0;
  357. }