demo_cylvssphere.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. // Test for cylinder vs sphere, by Bram Stolk
  23. #include <ode/odeconfig.h>
  24. #include <assert.h>
  25. #ifdef HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #endif
  28. #include <ode/ode.h>
  29. #include <drawstuff/drawstuff.h>
  30. #include "texturepath.h"
  31. #ifdef _MSC_VER
  32. #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
  33. #endif
  34. // dynamics and collision objects (chassis, 3 wheels, environment)
  35. static dWorldID world;
  36. static dSpaceID space;
  37. static dBodyID cylbody;
  38. static dGeomID cylgeom;
  39. static dBodyID sphbody;
  40. static dGeomID sphgeom;
  41. static dJointGroupID contactgroup;
  42. static bool show_contacts = true;
  43. #define CYLRADIUS 0.6
  44. #define CYLLENGTH 2.0
  45. #define SPHERERADIUS 0.5
  46. #ifdef dDOUBLE
  47. #define dsDrawBox dsDrawBoxD
  48. #define dsDrawLine dsDrawLineD
  49. #endif
  50. // this is called by dSpaceCollide when two objects in space are
  51. // potentially colliding.
  52. static void nearCallback (void *data, dGeomID o1, dGeomID o2)
  53. {
  54. assert(o1);
  55. assert(o2);
  56. if (dGeomIsSpace(o1) || dGeomIsSpace(o2))
  57. {
  58. fprintf(stderr,"testing space %p %p\n", (void*)o1, (void*)o2);
  59. // colliding a space with something
  60. dSpaceCollide2(o1,o2,data,&nearCallback);
  61. // Note we do not want to test intersections within a space,
  62. // only between spaces.
  63. return;
  64. }
  65. const int N = 32;
  66. dContact contact[N];
  67. int n = dCollide (o1,o2,N,&(contact[0].geom),sizeof(dContact));
  68. if (n > 0)
  69. {
  70. for (int i=0; i<n; i++)
  71. {
  72. contact[i].surface.mode = 0;
  73. contact[i].surface.mu = 50.0; // was: dInfinity
  74. dJointID c = dJointCreateContact (world,contactgroup,&contact[i]);
  75. dJointAttach (c, dGeomGetBody(contact[i].geom.g1), dGeomGetBody(contact[i].geom.g2));
  76. if (show_contacts)
  77. {
  78. dMatrix3 RI;
  79. dRSetIdentity (RI);
  80. const dReal ss[3] = {0.12,0.12,0.12};
  81. dsSetColorAlpha (0,0,1,0.5);
  82. dsDrawBox (contact[i].geom.pos,RI,ss);
  83. dReal *pos = contact[i].geom.pos;
  84. dReal depth = contact[i].geom.depth;
  85. dReal *norm = contact[i].geom.normal;
  86. dReal endp[3] = {pos[0]+depth*norm[0], pos[1]+depth*norm[1], pos[2]+depth*norm[2]};
  87. dsSetColorAlpha (1,1,1,1);
  88. dsDrawLine (contact[i].geom.pos, endp);
  89. }
  90. }
  91. }
  92. }
  93. // start simulation - set viewpoint
  94. static void start()
  95. {
  96. dAllocateODEDataForThread(dAllocateMaskAll);
  97. static float xyz[3] = {-8,-9,3};
  98. static float hpr[3] = {45.0000f,-27.5000f,0.0000f};
  99. dsSetViewpoint (xyz,hpr);
  100. }
  101. // called when a key pressed
  102. static void command (int cmd)
  103. {
  104. switch (cmd)
  105. {
  106. case ' ':
  107. break;
  108. }
  109. }
  110. // simulation loop
  111. static void simLoop (int pause)
  112. {
  113. dSpaceCollide (space,0,&nearCallback);
  114. if (!pause)
  115. {
  116. dWorldQuickStep (world, 0.01); // 100 Hz
  117. }
  118. dJointGroupEmpty (contactgroup);
  119. dsSetColorAlpha (1,1,0,0.5);
  120. const dReal *CPos = dBodyGetPosition(cylbody);
  121. const dReal *CRot = dBodyGetRotation(cylbody);
  122. float cpos[3] = {CPos[0], CPos[1], CPos[2]};
  123. float crot[12] = { CRot[0], CRot[1], CRot[2], CRot[3], CRot[4], CRot[5], CRot[6], CRot[7], CRot[8], CRot[9], CRot[10], CRot[11] };
  124. dsDrawCylinder
  125. (
  126. cpos,
  127. crot,
  128. CYLLENGTH,
  129. CYLRADIUS
  130. ); // single precision
  131. const dReal *SPos = dBodyGetPosition(sphbody);
  132. const dReal *SRot = dBodyGetRotation(sphbody);
  133. float spos[3] = {SPos[0], SPos[1], SPos[2]};
  134. float srot[12] = { SRot[0], SRot[1], SRot[2], SRot[3], SRot[4], SRot[5], SRot[6], SRot[7], SRot[8], SRot[9], SRot[10], SRot[11] };
  135. dsDrawSphere
  136. (
  137. spos,
  138. srot,
  139. SPHERERADIUS
  140. ); // single precision
  141. }
  142. int main (int argc, char **argv)
  143. {
  144. dMass m;
  145. // setup pointers to drawstuff callback functions
  146. dsFunctions fn;
  147. fn.version = DS_VERSION;
  148. fn.start = &start;
  149. fn.step = &simLoop;
  150. fn.command = &command;
  151. fn.stop = 0;
  152. fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
  153. // create world
  154. dInitODE2(0);
  155. world = dWorldCreate();
  156. space = dHashSpaceCreate (0);
  157. contactgroup = dJointGroupCreate (0);
  158. dWorldSetGravity (world,0,0,-9.8);
  159. dWorldSetQuickStepNumIterations (world, 32);
  160. dCreatePlane (space,0,0,1, 0.0);
  161. cylbody = dBodyCreate (world);
  162. dQuaternion q;
  163. #if 0
  164. dQFromAxisAndAngle (q,1,0,0,M_PI*0.5);
  165. #else
  166. // dQFromAxisAndAngle (q,1,0,0, M_PI * 1.0);
  167. dQFromAxisAndAngle (q,1,0,0, M_PI * -0.77);
  168. #endif
  169. dBodySetQuaternion (cylbody,q);
  170. dMassSetCylinder (&m,1.0,3,CYLRADIUS,CYLLENGTH);
  171. dBodySetMass (cylbody,&m);
  172. cylgeom = dCreateCylinder(0, CYLRADIUS, CYLLENGTH);
  173. dGeomSetBody (cylgeom,cylbody);
  174. dBodySetPosition (cylbody, 0, 0, 3);
  175. dSpaceAdd (space, cylgeom);
  176. sphbody = dBodyCreate (world);
  177. dMassSetSphere (&m,1,SPHERERADIUS);
  178. dBodySetMass (sphbody,&m);
  179. sphgeom = dCreateSphere(0, SPHERERADIUS);
  180. dGeomSetBody (sphgeom,sphbody);
  181. dBodySetPosition (sphbody, 0, 0, 5.5);
  182. dSpaceAdd (space, sphgeom);
  183. // run simulation
  184. dsSimulationLoop (argc,argv,352,288,&fn);
  185. dJointGroupEmpty (contactgroup);
  186. dJointGroupDestroy (contactgroup);
  187. dGeomDestroy(sphgeom);
  188. dGeomDestroy (cylgeom);
  189. dSpaceDestroy (space);
  190. dWorldDestroy (world);
  191. dCloseODE();
  192. return 0;
  193. }