demo_chain2.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /* exercise the C++ interface */
  23. #include <ode/ode.h>
  24. #include <drawstuff/drawstuff.h>
  25. #include "texturepath.h"
  26. #ifdef _MSC_VER
  27. #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
  28. #endif
  29. // select correct drawing functions
  30. #ifdef dDOUBLE
  31. #define dsDrawBox dsDrawBoxD
  32. #define dsDrawSphere dsDrawSphereD
  33. #define dsDrawCylinder dsDrawCylinderD
  34. #define dsDrawCapsule dsDrawCapsuleD
  35. #endif
  36. // some constants
  37. #define NUM 10 // number of boxes
  38. #define SIDE (0.2) // side length of a box
  39. #define MASS (1.0) // mass of a box
  40. #define RADIUS (0.1732f) // sphere radius
  41. //using namespace ode;
  42. // dynamics and collision objects
  43. static dWorld world;
  44. static dSimpleSpace space (0);
  45. static dBody body[NUM];
  46. static dBallJoint joint[NUM-1];
  47. static dJointGroup contactgroup;
  48. static dBox box[NUM];
  49. // this is called by space.collide when two objects in space are
  50. // potentially colliding.
  51. static void nearCallback (void *, dGeomID o1, dGeomID o2)
  52. {
  53. // exit without doing anything if the two bodies are connected by a joint
  54. dBodyID b1 = dGeomGetBody(o1);
  55. dBodyID b2 = dGeomGetBody(o2);
  56. if (b1 && b2 && dAreConnected (b1,b2)) return;
  57. // @@@ it's still more convenient to use the C interface here.
  58. dContact contact;
  59. contact.surface.mode = 0;
  60. contact.surface.mu = dInfinity;
  61. if (dCollide (o1,o2,1,&contact.geom,sizeof(dContactGeom))) {
  62. dJointID c = dJointCreateContact (world.id(),contactgroup.id(),&contact);
  63. dJointAttach (c,b1,b2);
  64. }
  65. }
  66. // start simulation - set viewpoint
  67. static void start()
  68. {
  69. dAllocateODEDataForThread(dAllocateMaskAll);
  70. static float xyz[3] = {2.1640f,-1.3079f,1.7600f};
  71. static float hpr[3] = {125.5000f,-17.0000f,0.0000f};
  72. dsSetViewpoint (xyz,hpr);
  73. }
  74. // simulation loop
  75. static void simLoop (int pause)
  76. {
  77. if (!pause) {
  78. static double angle = 0;
  79. angle += 0.05;
  80. body[NUM-1].addForce (0,0,1.5*(sin(angle)+1.0));
  81. space.collide (0,&nearCallback);
  82. world.step (0.05);
  83. // remove all contact joints
  84. contactgroup.empty();
  85. }
  86. dReal sides[3] = {SIDE,SIDE,SIDE};
  87. dsSetColor (1,1,0);
  88. dsSetTexture (DS_WOOD);
  89. for (int i=0; i<NUM; i++)
  90. dsDrawBox (body[i].getPosition(),body[i].getRotation(),sides);
  91. }
  92. int main (int argc, char **argv)
  93. {
  94. // setup pointers to drawstuff callback functions
  95. dsFunctions fn;
  96. fn.version = DS_VERSION;
  97. fn.start = &start;
  98. fn.step = &simLoop;
  99. fn.command = 0;
  100. fn.stop = 0;
  101. fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
  102. // create world
  103. dInitODE2(0);
  104. int i;
  105. contactgroup.create ();
  106. world.setGravity (0,0,-0.5);
  107. dWorldSetCFM (world.id(),1e-5);
  108. dPlane plane (space,0,0,1,0);
  109. for (i=0; i<NUM; i++) {
  110. body[i].create (world);
  111. dReal k = i*SIDE;
  112. body[i].setPosition (k,k,k+0.4);
  113. dMass m;
  114. m.setBox (1,SIDE,SIDE,SIDE);
  115. m.adjust (MASS);
  116. body[i].setMass (&m);
  117. body[i].setData ((void*)(size_t)i);
  118. box[i].create (space,SIDE,SIDE,SIDE);
  119. box[i].setBody (body[i]);
  120. }
  121. for (i=0; i<(NUM-1); i++) {
  122. joint[i].create (world);
  123. joint[i].attach (body[i],body[i+1]);
  124. dReal k = (i+0.5)*SIDE;
  125. joint[i].setAnchor (k,k,k+0.4);
  126. }
  127. // run simulation
  128. dsSimulationLoop (argc,argv,352,288,&fn);
  129. dCloseODE();
  130. return 0;
  131. }