test_ray.cpp 13 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  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 <dRay.h>
  24. #include <drawstuff/drawstuff.h>
  25. #ifdef _MSC_VER
  26. #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
  27. #endif
  28. // select correct drawing functions
  29. #ifdef dDOUBLE
  30. #define dsDrawBox dsDrawBoxD
  31. #define dsDrawSphere dsDrawSphereD
  32. #define dsDrawCylinder dsDrawCylinderD
  33. #define dsDrawCappedCylinder dsDrawCappedCylinderD
  34. #endif
  35. // some constants
  36. #define NUM 20 // max number of objects
  37. #define DENSITY (5.0) // density of all objects
  38. #define GPB 3 // maximum number of geometries per body
  39. // dynamics and collision objects
  40. struct MyObject {
  41. dBodyID body; // the body
  42. dGeomID geom[GPB]; // geometries representing this body
  43. };
  44. static int num=0; // number of objects in simulation
  45. static int nextobj=0; // next object to recycle if num==NUM
  46. static dWorldID world;
  47. static dSpaceID space;
  48. static MyObject obj[NUM];
  49. static dJointGroupID contactgroup;
  50. static int selected = -1; // selected object
  51. static dGeomID* Rays;
  52. static int RayCount;
  53. // this is called by dSpaceCollide when two objects in space are
  54. // potentially colliding.
  55. static void nearCallback (void *data, dGeomID o1, dGeomID o2)
  56. {
  57. int i;
  58. // if (o1->body && o2->body) return;
  59. // exit without doing anything if the two bodies are connected by a joint
  60. dBodyID b1 = dGeomGetBody(o1);
  61. dBodyID b2 = dGeomGetBody(o2);
  62. if (b1 && b2 && dAreConnected (b1,b2)) return;
  63. dContact contact[32]; // up to 3 contacts per box
  64. for (i=0; i<32; i++) {
  65. contact[i].surface.mode = dContactBounce; //dContactMu2;
  66. contact[i].surface.mu = dInfinity;
  67. contact[i].surface.mu2 = 0;
  68. contact[i].surface.bounce = 0.5;
  69. contact[i].surface.bounce_vel = 0.1;
  70. }
  71. if (int numc = dCollide (o1,o2,3,&contact[0].geom,sizeof(dContact))) {
  72. dMatrix3 RI;
  73. dRSetIdentity (RI);
  74. const dReal ss[3] = {0.02,0.02,0.02};
  75. for (i=0; i<numc; i++) {
  76. if (dGeomGetClass(o1) == dRayClass || dGeomGetClass(o2) == dRayClass){
  77. dMatrix3 Rotation;
  78. dRSetIdentity(Rotation);
  79. dsDrawSphere(contact[i].geom.pos, Rotation, REAL(0.01));
  80. continue;
  81. }
  82. dJointID c = dJointCreateContact (world,contactgroup,contact+i);
  83. dJointAttach (c,b1,b2);
  84. //dsDrawBox (contact[i].geom.pos,RI,ss);
  85. }
  86. }
  87. }
  88. // start simulation - set viewpoint
  89. static void start()
  90. {
  91. static float xyz[3] = {2.1640f,-1.3079f,1.7600f};
  92. static float hpr[3] = {125.5000f,-17.0000f,0.0000f};
  93. dsSetViewpoint (xyz,hpr);
  94. printf ("To drop another object, press:\n");
  95. printf (" b for box.\n");
  96. printf (" s for sphere.\n");
  97. printf (" c for cylinder.\n");
  98. printf (" x for a composite object.\n");
  99. printf ("To select an object, press space.\n");
  100. printf ("To disable the selected object, press d.\n");
  101. printf ("To enable the selected object, press e.\n");
  102. }
  103. char locase (char c)
  104. {
  105. if (c >= 'A' && c <= 'Z') return c - ('a'-'A');
  106. else return c;
  107. }
  108. // called when a key pressed
  109. static void command (int cmd)
  110. {
  111. int i,j,k;
  112. dReal sides[3];
  113. dMass m;
  114. cmd = locase (cmd);
  115. if (cmd == 'b' || cmd == 's' || cmd == 'c' || cmd == 'x') {
  116. if (num < NUM) {
  117. i = num;
  118. num++;
  119. }
  120. else {
  121. i = nextobj;
  122. nextobj++;
  123. if (nextobj >= num) nextobj = 0;
  124. // destroy the body and geoms for slot i
  125. dBodyDestroy (obj[i].body);
  126. for (k=0; k < GPB; k++) {
  127. if (obj[i].geom[k]) dGeomDestroy (obj[i].geom[k]);
  128. }
  129. memset (&obj[i],0,sizeof(obj[i]));
  130. }
  131. obj[i].body = dBodyCreate (world);
  132. for (k=0; k<3; k++) sides[k] = dRandReal()*0.5+0.1;
  133. dBodySetPosition (obj[i].body,
  134. dRandReal()*2-1,dRandReal()*2-1,dRandReal()+1);
  135. dMatrix3 R;
  136. dRFromAxisAndAngle (R,dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
  137. dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
  138. dBodySetRotation (obj[i].body,R);
  139. dBodySetData (obj[i].body,(void*) i);
  140. if (cmd == 'b') {
  141. dMassSetBox (&m,DENSITY,sides[0],sides[1],sides[2]);
  142. obj[i].geom[0] = dCreateBox (space,sides[0],sides[1],sides[2]);
  143. }
  144. else if (cmd == 'c') {
  145. sides[0] *= 0.5;
  146. dMassSetCappedCylinder (&m,DENSITY,3,sides[0],sides[1]);
  147. obj[i].geom[0] = dCreateCCylinder (space,sides[0],sides[1]);
  148. }
  149. else if (cmd == 's') {
  150. sides[0] *= 0.5;
  151. dMassSetSphere (&m,DENSITY,sides[0]);
  152. obj[i].geom[0] = dCreateSphere (space,sides[0]);
  153. }
  154. else if (cmd == 'x') {
  155. dGeomID g2[GPB]; // encapsulated geometries
  156. dReal dpos[GPB][3]; // delta-positions for encapsulated geometries
  157. // start accumulating masses for the encapsulated geometries
  158. dMass m2;
  159. dMassSetZero (&m);
  160. // set random delta positions
  161. for (j=0; j<GPB; j++) {
  162. for (k=0; k<3; k++) dpos[j][k] = dRandReal()*0.3-0.15;
  163. }
  164. for (k=0; k<3; k++) {
  165. obj[i].geom[k] = dCreateGeomTransform (space);
  166. dGeomTransformSetCleanup (obj[i].geom[k],1);
  167. if (k==0) {
  168. dReal radius = dRandReal()*0.25+0.05;
  169. g2[k] = dCreateSphere (0,radius);
  170. dMassSetSphere (&m2,DENSITY,radius);
  171. }
  172. else if (k==1) {
  173. g2[k] = dCreateBox (0,sides[0],sides[1],sides[2]);
  174. dMassSetBox (&m2,DENSITY,sides[0],sides[1],sides[2]);
  175. }
  176. else {
  177. dReal radius = dRandReal()*0.1+0.05;
  178. dReal length = dRandReal()*1.0+0.1;
  179. g2[k] = dCreateCCylinder (0,radius,length);
  180. dMassSetCappedCylinder (&m2,DENSITY,3,radius,length);
  181. }
  182. dGeomTransformSetGeom (obj[i].geom[k],g2[k]);
  183. // set the transformation (adjust the mass too)
  184. dGeomSetPosition (g2[k],dpos[k][0],dpos[k][1],dpos[k][2]);
  185. dMassTranslate (&m2,dpos[k][0],dpos[k][1],dpos[k][2]);
  186. dMatrix3 Rtx;
  187. dRFromAxisAndAngle (Rtx,dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
  188. dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
  189. dGeomSetRotation (g2[k],Rtx);
  190. dMassRotate (&m2,Rtx);
  191. // add to the total mass
  192. dMassAdd (&m,&m2);
  193. }
  194. // move all encapsulated objects so that the center of mass is (0,0,0)
  195. for (k=0; k<2; k++) {
  196. dGeomSetPosition (g2[k],
  197. dpos[k][0]-m.c[0],
  198. dpos[k][1]-m.c[1],
  199. dpos[k][2]-m.c[2]);
  200. }
  201. dMassTranslate (&m,-m.c[0],-m.c[1],-m.c[2]);
  202. }
  203. for (k=0; k < GPB; k++) {
  204. if (obj[i].geom[k]) dGeomSetBody (obj[i].geom[k],obj[i].body);
  205. }
  206. dBodySetMass (obj[i].body,&m);
  207. }
  208. if (cmd == ' ') {
  209. selected++;
  210. if (selected >= num) selected = 0;
  211. if (selected < 0) selected = 0;
  212. }
  213. else if (cmd == 'd' && selected >= 0 && selected < num) {
  214. dBodyDisable (obj[selected].body);
  215. }
  216. else if (cmd == 'e' && selected >= 0 && selected < num) {
  217. dBodyEnable (obj[selected].body);
  218. }
  219. }
  220. // draw a geom
  221. void drawGeom (dGeomID g, const dReal *pos, const dReal *R)
  222. {
  223. if (!g) return;
  224. if (!pos) pos = dGeomGetPosition (g);
  225. if (!R) R = dGeomGetRotation (g);
  226. int type = dGeomGetClass (g);
  227. if (type == dBoxClass) {
  228. dVector3 sides;
  229. dGeomBoxGetLengths (g,sides);
  230. dsDrawBox (pos,R,sides);
  231. }
  232. else if (type == dSphereClass) {
  233. dsDrawSphere (pos,R,dGeomSphereGetRadius (g));
  234. }
  235. else if (type == dCCylinderClass) {
  236. dReal radius,length;
  237. dGeomCCylinderGetParams (g,&radius,&length);
  238. dsDrawCappedCylinder (pos,R,length,radius);
  239. }
  240. else if (type == dGeomTransformClass) {
  241. dGeomID g2 = dGeomTransformGetGeom (g);
  242. const dReal *pos2 = dGeomGetPosition (g2);
  243. const dReal *R2 = dGeomGetRotation (g2);
  244. dVector3 actual_pos;
  245. dMatrix3 actual_R;
  246. dMULTIPLY0_331 (actual_pos,R,pos2);
  247. actual_pos[0] += pos[0];
  248. actual_pos[1] += pos[1];
  249. actual_pos[2] += pos[2];
  250. dMULTIPLY0_333 (actual_R,R,R2);
  251. drawGeom (g2,actual_pos,actual_R);
  252. }
  253. }
  254. // simulation loop
  255. static void simLoop (int pause)
  256. {
  257. dsSetColor (0,0,2);
  258. dSpaceCollide (space,0,&nearCallback);
  259. if (!pause) dWorldStep (world,0.05);
  260. // remove all contact joints
  261. dJointGroupEmpty (contactgroup);
  262. dsSetColor (1,1,0);
  263. dsSetTexture (DS_WOOD);
  264. for (int i=0; i<num; i++) {
  265. int color_changed = 0;
  266. if (i==selected) {
  267. dsSetColor (0,0.7,1);
  268. color_changed = 1;
  269. }
  270. else if (! dBodyIsEnabled (obj[i].body)) {
  271. dsSetColor (1,0,0);
  272. color_changed = 1;
  273. }
  274. for (int j=0; j < GPB; j++) drawGeom (obj[i].geom[j],0,0);
  275. if (color_changed) dsSetColor (1,1,0);
  276. }
  277. {for (int i = 0; i < RayCount; i++){
  278. dVector3 Origin, Direction;
  279. dGeomRayGet(Rays[i], Origin, Direction);
  280. dReal Length = dGeomRayGetLength(Rays[i]);
  281. dVector3 End;
  282. End[0] = Origin[0] + (Direction[0] * Length);
  283. End[1] = Origin[1] + (Direction[1] * Length);
  284. End[2] = Origin[2] + (Direction[2] * Length);
  285. End[3] = Origin[3] + (Direction[3] * Length);
  286. dsDrawLine(Origin, End);
  287. }}
  288. }
  289. int main (int argc, char **argv)
  290. {
  291. // setup pointers to drawstuff callback functions
  292. dsFunctions fn;
  293. fn.version = DS_VERSION;
  294. fn.start = &start;
  295. fn.step = &simLoop;
  296. fn.command = &command;
  297. fn.stop = 0;
  298. fn.path_to_textures = "../../drawstuff/textures";
  299. if(argc==2)
  300. {
  301. fn.path_to_textures = argv[1];
  302. }
  303. // create world
  304. world = dWorldCreate();
  305. space = dHashSpaceCreate();
  306. contactgroup = dJointGroupCreate (0);
  307. dWorldSetGravity (world,0,0,-0.5);
  308. dWorldSetCFM (world,1e-5);
  309. dCreatePlane (space,0,0,1,0);
  310. memset (obj,0,sizeof(obj));
  311. dVector3 Origin, Direction;
  312. RayCount = 5;
  313. Rays = new dGeomID[RayCount];
  314. /* Ray 0 */
  315. Origin[0] = 1;
  316. Origin[1] = 1;
  317. Origin[2] = 1.5;
  318. Origin[3] = 0;
  319. Direction[0] = 0.0f;
  320. Direction[1] = 0.0f;
  321. Direction[2] = -1;
  322. Direction[3] = 0;
  323. dNormalize3(Direction);
  324. Rays[0] = dGeomCreateRay(space, 5.0f);
  325. dGeomRaySet(Rays[0], Origin, Direction);
  326. /* Ray 1 */
  327. Origin[0] = 0;
  328. Origin[1] = 10;
  329. Origin[2] = 0.25;
  330. Origin[3] = 0;
  331. Direction[0] = 0.0f;
  332. Direction[1] = -1.0f;
  333. Direction[2] = 0.0f;
  334. Direction[3] = 0;
  335. dNormalize3(Direction);
  336. Rays[1] = dGeomCreateRay(space, 20.0f);
  337. dGeomRaySet(Rays[1], Origin, Direction);
  338. /* Ray 2 */
  339. Origin[0] = -10;
  340. Origin[1] = 0;
  341. Origin[2] = 0.20;
  342. Origin[3] = 0;
  343. Direction[0] = 1.0f;
  344. Direction[1] = 0.0f;
  345. Direction[2] = 0.0f;
  346. Direction[3] = 0;
  347. dNormalize3(Direction);
  348. Rays[2] = dGeomCreateRay(space, 20.0f);
  349. dGeomRaySet(Rays[2], Origin, Direction);
  350. /* Ray 3 */
  351. Origin[0] = -9;
  352. Origin[1] = 11;
  353. Origin[2] = 0.15;
  354. Origin[3] = 0;
  355. Direction[0] = 1.0f;
  356. Direction[1] = -1.0f;
  357. Direction[2] = 0.0f;
  358. Direction[3] = 0;
  359. dNormalize3(Direction);
  360. Rays[3] = dGeomCreateRay(space, 20.0f);
  361. dGeomRaySet(Rays[3], Origin, Direction);
  362. /* Ray 4 */
  363. Origin[0] = -0.1;
  364. Origin[1] = 0.3;
  365. Origin[2] = 0.30;
  366. Origin[3] = 0;
  367. Direction[0] = 0.3f;
  368. Direction[1] = 0.5f;
  369. Direction[2] = 1.0f;
  370. Direction[3] = 0;
  371. Rays[4] = dGeomCreateRay(space, 5.0f);
  372. dGeomRaySet(Rays[4], Origin, Direction);
  373. // run simulation
  374. dsSimulationLoop (argc,argv,352,288,&fn);
  375. dJointGroupDestroy (contactgroup);
  376. dSpaceDestroy (space);
  377. dWorldDestroy (world);
  378. return 0;
  379. }