demo_dball.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #include <ode/ode.h>
  23. #include <drawstuff/drawstuff.h>
  24. #include "texturepath.h"
  25. #ifdef dDOUBLE
  26. #define dsDrawSphere dsDrawSphereD
  27. #define dsDrawBox dsDrawBoxD
  28. #define dsDrawLine dsDrawLineD
  29. #endif
  30. dWorldID world;
  31. dSpaceID space;
  32. dBodyID body1;
  33. dBodyID body2;
  34. dJointID joint1, joint2;
  35. void start()
  36. {
  37. world = dWorldCreate();
  38. dWorldSetGravity (world,0,0,-9.8);
  39. dWorldSetDamping(world, 1e-4, 1e-5);
  40. // dWorldSetERP(world, 1);
  41. space = dSimpleSpaceCreate (0);
  42. body1 = dBodyCreate(world);
  43. body2 = dBodyCreate(world);
  44. dBodySetPosition(body1, 0, 0, 3);
  45. dBodySetPosition(body2, 0, 0, 1);
  46. dGeomID g;
  47. dMass mass;
  48. g = dCreateBox(space, 0.2, 0.2, 1);
  49. dGeomSetBody(g, body1);
  50. dMassSetBox(&mass, 1, 0.2, 0.2, 1);
  51. dBodySetMass(body1, &mass);
  52. g = dCreateBox(space, 0.2, 0.2, 1);
  53. dGeomSetBody(g, body2);
  54. dMassSetBox(&mass, 1, 0.2, 0.2, 1);
  55. dBodySetMass(body2, &mass);
  56. joint1 = dJointCreateDBall(world, 0);
  57. dJointAttach(joint1, body1, 0);
  58. dJointSetDBallAnchor1(joint1, 0, 0, 3.5);
  59. dJointSetDBallAnchor2(joint1, 0, 0, 4.5);
  60. joint2 = dJointCreateDBall(world, 0);
  61. dJointAttach(joint2, body1, body2);
  62. dJointSetDBallAnchor1(joint2, 0, 0, 2.5);
  63. dJointSetDBallAnchor2(joint2, 0, 0, 1.5);
  64. // initial camera position
  65. static float xyz[3] = {3.8966, -2.0614, 4.0300};
  66. static float hpr[3] = {153.5, -16.5, 0};
  67. dsSetViewpoint (xyz,hpr);
  68. }
  69. void stop()
  70. {
  71. dSpaceDestroy(space);
  72. dWorldDestroy(world);
  73. }
  74. void drawGeom(dGeomID g)
  75. {
  76. int gclass = dGeomGetClass(g);
  77. const dReal *pos = dGeomGetPosition(g);
  78. const dReal *rot = dGeomGetRotation(g);
  79. switch (gclass) {
  80. case dSphereClass:
  81. dsSetColorAlpha(0, 0.75, 0.5, 1);
  82. dsSetTexture (DS_CHECKERED);
  83. dsDrawSphere(pos, rot, dGeomSphereGetRadius(g));
  84. break;
  85. case dBoxClass:
  86. {
  87. dVector3 lengths;
  88. dsSetColorAlpha(1, 1, 0, 1);
  89. dsSetTexture (DS_WOOD);
  90. dGeomBoxGetLengths(g, lengths);
  91. dsDrawBox(pos, rot, lengths);
  92. break;
  93. }
  94. default:
  95. {}
  96. }
  97. }
  98. void simLoop(int pause)
  99. {
  100. if (!pause) {
  101. static dReal t = 0;
  102. const dReal step = 0.005;
  103. const unsigned nsteps = 4;
  104. for (unsigned i=0; i<nsteps; ++i) {
  105. dReal f = sin(t*1.2)*0.8;
  106. dBodyAddForceAtRelPos(body1,
  107. f, 0, 0,
  108. 0, 0, -0.5); // at the lower end
  109. dReal g = sin(t*0.7)*0.8;
  110. dBodyAddForceAtRelPos(body2,
  111. 0, g, 0,
  112. 0, 0, -0.5); // at the lower end
  113. t += step;
  114. dWorldQuickStep(world, step);
  115. }
  116. }
  117. // now we draw everything
  118. unsigned ngeoms = dSpaceGetNumGeoms(space);
  119. for (unsigned i=0; i<ngeoms; ++i) {
  120. dGeomID g = dSpaceGetGeom(space, i);
  121. drawGeom(g);
  122. }
  123. dVector3 a11, a12;
  124. dJointGetDBallAnchor1(joint1, a11);
  125. dJointGetDBallAnchor2(joint1, a12);
  126. dsSetColor(1, 0, 0);
  127. dsDrawLine(a11, a12);
  128. //printf("Error 1: %f\n", fabs(dJointGetDBallDistance(joint1) - dCalcPointsDistance3(a11, a12)));
  129. dVector3 a21, a22;
  130. dJointGetDBallAnchor1(joint2, a21);
  131. dJointGetDBallAnchor2(joint2, a22);
  132. dsSetColor(0, 1, 0);
  133. dsDrawLine(a21, a22);
  134. //printf("Error 2: %f\n", fabs(dJointGetDBallDistance(joint2) - dCalcPointsDistance3(a21, a22)));
  135. }
  136. int main(int argc, char **argv)
  137. {
  138. // setup pointers to drawstuff callback functions
  139. dsFunctions fn;
  140. fn.version = DS_VERSION;
  141. fn.start = &start;
  142. fn.step = &simLoop;
  143. fn.command = 0;
  144. fn.stop = stop;
  145. fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
  146. // create world
  147. dInitODE();
  148. // run demo
  149. dsSimulationLoop (argc, argv, 800, 600, &fn);
  150. dCloseODE();
  151. return 0;
  152. }