Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ode/ode-0.9/contrib/dRay/Test/test_ray.cpp @ 216

Last change on this file since 216 was 216, checked in by mathiask, 17 years ago

[Physik] add ode-0.9

File size: 13.0 KB
Line 
1/*************************************************************************
2
3
4 *                                                                       *
5
6
7 * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith.       *
8
9
10 * All rights reserved.  Email: russ@q12.org   Web: www.q12.org          *
11
12
13 *                                                                       *
14
15
16 * This library is free software; you can redistribute it and/or         *
17
18
19 * modify it under the terms of EITHER:                                  *
20
21
22 *   (1) The GNU Lesser General Public License as published by the Free  *
23
24
25 *       Software Foundation; either version 2.1 of the License, or (at  *
26
27
28 *       your option) any later version. The text of the GNU Lesser      *
29
30
31 *       General Public License is included with this library in the     *
32
33
34 *       file LICENSE.TXT.                                               *
35
36
37 *   (2) The BSD-style license that is included with this library in     *
38
39
40 *       the file LICENSE-BSD.TXT.                                       *
41
42
43 *                                                                       *
44
45
46 * This library is distributed in the hope that it will be useful,       *
47
48
49 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
50
51
52 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
53
54
55 * LICENSE.TXT and LICENSE-BSD.TXT for more details.                     *
56
57
58 *                                                                       *
59
60
61 *************************************************************************/
62
63
64
65
66
67#include <ode/ode.h>
68
69
70#include <dRay.h>
71
72
73#include <drawstuff/drawstuff.h>
74
75
76
77
78
79#ifdef _MSC_VER
80
81
82#pragma warning(disable:4244 4305)  // for VC++, no precision loss complaints
83
84
85#endif
86
87
88
89
90
91// select correct drawing functions
92
93
94
95
96
97#ifdef dDOUBLE
98
99
100#define dsDrawBox dsDrawBoxD
101
102
103#define dsDrawSphere dsDrawSphereD
104
105
106#define dsDrawCylinder dsDrawCylinderD
107
108
109#define dsDrawCappedCylinder dsDrawCappedCylinderD
110
111
112#endif
113
114
115
116
117
118
119
120
121// some constants
122
123
124
125
126
127#define NUM 20                  // max number of objects
128
129
130#define DENSITY (5.0)           // density of all objects
131
132
133#define GPB 3                   // maximum number of geometries per body
134
135
136
137
138
139
140
141
142// dynamics and collision objects
143
144
145
146
147
148struct MyObject {
149
150
151  dBodyID body;                 // the body
152
153
154  dGeomID geom[GPB];            // geometries representing this body
155
156
157};
158
159
160
161
162
163static int num=0;               // number of objects in simulation
164
165
166static int nextobj=0;           // next object to recycle if num==NUM
167
168
169static dWorldID world;
170
171
172static dSpaceID space;
173
174
175static MyObject obj[NUM];
176
177
178static dJointGroupID contactgroup;
179
180
181static int selected = -1;       // selected object
182
183
184
185
186
187static dGeomID* Rays;
188
189
190static int RayCount;
191
192
193
194
195
196// this is called by dSpaceCollide when two objects in space are
197
198
199// potentially colliding.
200
201
202
203
204
205static void nearCallback (void *data, dGeomID o1, dGeomID o2)
206
207
208{
209
210
211  int i;
212
213
214  // if (o1->body && o2->body) return;
215
216
217
218
219
220  // exit without doing anything if the two bodies are connected by a joint
221
222
223  dBodyID b1 = dGeomGetBody(o1);
224
225
226  dBodyID b2 = dGeomGetBody(o2);
227
228
229  if (b1 && b2 && dAreConnected (b1,b2)) return;
230
231
232
233
234
235  dContact contact[32];                 // up to 3 contacts per box
236
237
238  for (i=0; i<32; i++) {
239
240
241    contact[i].surface.mode = dContactBounce; //dContactMu2;
242
243
244    contact[i].surface.mu = dInfinity;
245
246
247    contact[i].surface.mu2 = 0;
248
249
250    contact[i].surface.bounce = 0.5;
251
252
253    contact[i].surface.bounce_vel = 0.1;
254
255
256  }
257
258
259  if (int numc = dCollide (o1,o2,3,&contact[0].geom,sizeof(dContact))) {
260
261
262     dMatrix3 RI;
263
264
265     dRSetIdentity (RI);
266
267
268     const dReal ss[3] = {0.02,0.02,0.02};
269
270
271    for (i=0; i<numc; i++) {
272
273
274                if (dGeomGetClass(o1) == dRayClass || dGeomGetClass(o2) == dRayClass){
275
276
277                        dMatrix3 Rotation;
278
279
280                        dRSetIdentity(Rotation);
281
282
283                        dsDrawSphere(contact[i].geom.pos, Rotation, REAL(0.01));
284
285
286                        continue;
287
288
289                }
290
291
292
293
294
295      dJointID c = dJointCreateContact (world,contactgroup,contact+i);
296
297
298      dJointAttach (c,b1,b2);
299
300
301       //dsDrawBox (contact[i].geom.pos,RI,ss);
302
303
304
305
306
307         
308
309
310    }
311
312
313  }
314
315
316}
317
318
319
320
321
322// start simulation - set viewpoint
323
324
325
326
327
328static void start()
329
330
331{
332
333
334  static float xyz[3] = {2.1640f,-1.3079f,1.7600f};
335
336
337  static float hpr[3] = {125.5000f,-17.0000f,0.0000f};
338
339
340  dsSetViewpoint (xyz,hpr);
341
342
343  printf ("To drop another object, press:\n");
344
345
346  printf ("   b for box.\n");
347
348
349  printf ("   s for sphere.\n");
350
351
352  printf ("   c for cylinder.\n");
353
354
355  printf ("   x for a composite object.\n");
356
357
358  printf ("To select an object, press space.\n");
359
360
361  printf ("To disable the selected object, press d.\n");
362
363
364  printf ("To enable the selected object, press e.\n");
365
366
367}
368
369
370
371
372
373
374
375
376char locase (char c)
377
378
379{
380
381
382  if (c >= 'A' && c <= 'Z') return c - ('a'-'A');
383
384
385  else return c;
386
387
388}
389
390
391
392
393
394
395
396
397// called when a key pressed
398
399
400
401
402
403static void command (int cmd)
404
405
406{
407
408
409  int i,j,k;
410
411
412  dReal sides[3];
413
414
415  dMass m;
416
417
418
419
420
421  cmd = locase (cmd);
422
423
424  if (cmd == 'b' || cmd == 's' || cmd == 'c' || cmd == 'x') {
425
426
427    if (num < NUM) {
428
429
430      i = num;
431
432
433      num++;
434
435
436    }
437
438
439    else {
440
441
442      i = nextobj;
443
444
445      nextobj++;
446
447
448      if (nextobj >= num) nextobj = 0;
449
450
451
452
453
454      // destroy the body and geoms for slot i
455
456
457      dBodyDestroy (obj[i].body);
458
459
460      for (k=0; k < GPB; k++) {
461
462
463        if (obj[i].geom[k]) dGeomDestroy (obj[i].geom[k]);
464
465
466      }
467
468
469      memset (&obj[i],0,sizeof(obj[i]));
470
471
472    }
473
474
475
476
477
478    obj[i].body = dBodyCreate (world);
479
480
481    for (k=0; k<3; k++) sides[k] = dRandReal()*0.5+0.1;
482
483
484
485
486
487    dBodySetPosition (obj[i].body,
488
489
490                      dRandReal()*2-1,dRandReal()*2-1,dRandReal()+1);
491
492
493    dMatrix3 R;
494
495
496    dRFromAxisAndAngle (R,dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
497
498
499                        dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
500
501
502    dBodySetRotation (obj[i].body,R);
503
504
505    dBodySetData (obj[i].body,(void*) i);
506
507
508
509
510
511    if (cmd == 'b') {
512
513
514      dMassSetBox (&m,DENSITY,sides[0],sides[1],sides[2]);
515
516
517      obj[i].geom[0] = dCreateBox (space,sides[0],sides[1],sides[2]);
518
519
520    }
521
522
523    else if (cmd == 'c') {
524
525
526      sides[0] *= 0.5;
527
528
529      dMassSetCappedCylinder (&m,DENSITY,3,sides[0],sides[1]);
530
531
532      obj[i].geom[0] = dCreateCCylinder (space,sides[0],sides[1]);
533
534
535    }
536
537
538    else if (cmd == 's') {
539
540
541      sides[0] *= 0.5;
542
543
544      dMassSetSphere (&m,DENSITY,sides[0]);
545
546
547      obj[i].geom[0] = dCreateSphere (space,sides[0]);
548
549
550    }
551
552
553    else if (cmd == 'x') {
554
555
556      dGeomID g2[GPB];          // encapsulated geometries
557
558
559      dReal dpos[GPB][3];       // delta-positions for encapsulated geometries
560
561
562
563
564
565      // start accumulating masses for the encapsulated geometries
566
567
568      dMass m2;
569
570
571      dMassSetZero (&m);
572
573
574
575
576
577      // set random delta positions
578
579
580      for (j=0; j<GPB; j++) {
581
582
583        for (k=0; k<3; k++) dpos[j][k] = dRandReal()*0.3-0.15;
584
585
586      }
587
588
589
590
591
592      for (k=0; k<3; k++) {
593
594
595        obj[i].geom[k] = dCreateGeomTransform (space);
596
597
598        dGeomTransformSetCleanup (obj[i].geom[k],1);
599
600
601        if (k==0) {
602
603
604          dReal radius = dRandReal()*0.25+0.05;
605
606
607          g2[k] = dCreateSphere (0,radius);
608
609
610          dMassSetSphere (&m2,DENSITY,radius);
611
612
613        }
614
615
616        else if (k==1) {
617
618
619          g2[k] = dCreateBox (0,sides[0],sides[1],sides[2]);
620
621
622          dMassSetBox (&m2,DENSITY,sides[0],sides[1],sides[2]);
623
624
625        }
626
627
628        else {
629
630
631          dReal radius = dRandReal()*0.1+0.05;
632
633
634          dReal length = dRandReal()*1.0+0.1;
635
636
637          g2[k] = dCreateCCylinder (0,radius,length);
638
639
640          dMassSetCappedCylinder (&m2,DENSITY,3,radius,length);
641
642
643        }
644
645
646        dGeomTransformSetGeom (obj[i].geom[k],g2[k]);
647
648
649
650
651
652        // set the transformation (adjust the mass too)
653
654
655        dGeomSetPosition (g2[k],dpos[k][0],dpos[k][1],dpos[k][2]);
656
657
658        dMassTranslate (&m2,dpos[k][0],dpos[k][1],dpos[k][2]);
659
660
661        dMatrix3 Rtx;
662
663
664        dRFromAxisAndAngle (Rtx,dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
665
666
667                            dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
668
669
670        dGeomSetRotation (g2[k],Rtx);
671
672
673        dMassRotate (&m2,Rtx);
674
675
676
677
678
679        // add to the total mass
680
681
682        dMassAdd (&m,&m2);
683
684
685      }
686
687
688
689
690
691      // move all encapsulated objects so that the center of mass is (0,0,0)
692
693
694      for (k=0; k<2; k++) {
695
696
697        dGeomSetPosition (g2[k],
698
699
700                          dpos[k][0]-m.c[0],
701
702
703                          dpos[k][1]-m.c[1],
704
705
706                          dpos[k][2]-m.c[2]);
707
708
709      }
710
711
712      dMassTranslate (&m,-m.c[0],-m.c[1],-m.c[2]);
713
714
715    }
716
717
718
719
720
721    for (k=0; k < GPB; k++) {
722
723
724      if (obj[i].geom[k]) dGeomSetBody (obj[i].geom[k],obj[i].body);
725
726
727    }
728
729
730
731
732
733    dBodySetMass (obj[i].body,&m);
734
735
736  }
737
738
739
740
741
742  if (cmd == ' ') {
743
744
745    selected++;
746
747
748    if (selected >= num) selected = 0;
749
750
751    if (selected < 0) selected = 0;
752
753
754  }
755
756
757  else if (cmd == 'd' && selected >= 0 && selected < num) {
758
759
760    dBodyDisable (obj[selected].body);
761
762
763  }
764
765
766  else if (cmd == 'e' && selected >= 0 && selected < num) {
767
768
769    dBodyEnable (obj[selected].body);
770
771
772  }
773
774
775}
776
777
778
779
780
781
782
783
784// draw a geom
785
786
787
788
789
790void drawGeom (dGeomID g, const dReal *pos, const dReal *R)
791
792
793{
794
795
796  if (!g) return;
797
798
799  if (!pos) pos = dGeomGetPosition (g);
800
801
802  if (!R) R = dGeomGetRotation (g);
803
804
805
806
807
808  int type = dGeomGetClass (g);
809
810
811  if (type == dBoxClass) {
812
813
814    dVector3 sides;
815
816
817    dGeomBoxGetLengths (g,sides);
818
819
820    dsDrawBox (pos,R,sides);
821
822
823  }
824
825
826  else if (type == dSphereClass) {
827
828
829    dsDrawSphere (pos,R,dGeomSphereGetRadius (g));
830
831
832  }
833
834
835  else if (type == dCCylinderClass) {
836
837
838    dReal radius,length;
839
840
841    dGeomCCylinderGetParams (g,&radius,&length);
842
843
844    dsDrawCappedCylinder (pos,R,length,radius);
845
846
847  }
848
849
850  else if (type == dGeomTransformClass) {
851
852
853    dGeomID g2 = dGeomTransformGetGeom (g);
854
855
856    const dReal *pos2 = dGeomGetPosition (g2);
857
858
859    const dReal *R2 = dGeomGetRotation (g2);
860
861
862    dVector3 actual_pos;
863
864
865    dMatrix3 actual_R;
866
867
868    dMULTIPLY0_331 (actual_pos,R,pos2);
869
870
871    actual_pos[0] += pos[0];
872
873
874    actual_pos[1] += pos[1];
875
876
877    actual_pos[2] += pos[2];
878
879
880    dMULTIPLY0_333 (actual_R,R,R2);
881
882
883    drawGeom (g2,actual_pos,actual_R);
884
885
886  }
887
888
889}
890
891
892
893
894
895
896
897
898// simulation loop
899
900
901
902
903
904static void simLoop (int pause)
905
906
907{
908
909
910  dsSetColor (0,0,2);
911
912
913  dSpaceCollide (space,0,&nearCallback);
914
915
916  if (!pause) dWorldStep (world,0.05);
917
918
919
920
921
922  // remove all contact joints
923
924
925  dJointGroupEmpty (contactgroup);
926
927
928
929
930
931  dsSetColor (1,1,0);
932
933
934  dsSetTexture (DS_WOOD);
935
936
937  for (int i=0; i<num; i++) {
938
939
940    int color_changed = 0;
941
942
943    if (i==selected) {
944
945
946      dsSetColor (0,0.7,1);
947
948
949      color_changed = 1;
950
951
952    }
953
954
955    else if (! dBodyIsEnabled (obj[i].body)) {
956
957
958      dsSetColor (1,0,0);
959
960
961      color_changed = 1;
962
963
964    }
965
966
967    for (int j=0; j < GPB; j++) drawGeom (obj[i].geom[j],0,0);
968
969
970    if (color_changed) dsSetColor (1,1,0);
971
972
973  }
974
975
976
977
978
979  {for (int i = 0; i < RayCount; i++){
980
981
982          dVector3 Origin, Direction;
983
984
985          dGeomRayGet(Rays[i], Origin, Direction);
986
987
988 
989
990
991          dReal Length = dGeomRayGetLength(Rays[i]);
992
993
994
995
996
997          dVector3 End;
998
999
1000          End[0] = Origin[0] + (Direction[0] * Length);
1001
1002
1003          End[1] = Origin[1] + (Direction[1] * Length);
1004
1005
1006          End[2] = Origin[2] + (Direction[2] * Length);
1007
1008
1009          End[3] = Origin[3] + (Direction[3] * Length);
1010
1011
1012 
1013
1014
1015          dsDrawLine(Origin, End);
1016
1017
1018  }}
1019
1020
1021}
1022
1023
1024
1025
1026
1027
1028
1029
1030int main (int argc, char **argv)
1031
1032
1033{
1034
1035
1036  // setup pointers to drawstuff callback functions
1037
1038
1039  dsFunctions fn;
1040
1041
1042  fn.version = DS_VERSION;
1043
1044
1045  fn.start = &start;
1046
1047
1048  fn.step = &simLoop;
1049
1050
1051  fn.command = &command;
1052
1053
1054  fn.stop = 0;
1055
1056
1057  fn.path_to_textures = "../../drawstuff/textures";
1058
1059
1060  if(argc==2)
1061    {
1062        fn.path_to_textures = argv[1];
1063    }
1064
1065
1066
1067  // create world
1068
1069
1070
1071
1072
1073  world = dWorldCreate();
1074
1075
1076  space = dHashSpaceCreate();
1077
1078
1079  contactgroup = dJointGroupCreate (0);
1080
1081
1082  dWorldSetGravity (world,0,0,-0.5);
1083
1084
1085  dWorldSetCFM (world,1e-5);
1086
1087
1088  dCreatePlane (space,0,0,1,0);
1089
1090
1091  memset (obj,0,sizeof(obj));
1092
1093
1094
1095
1096
1097  dVector3 Origin, Direction;
1098
1099
1100 
1101
1102
1103  RayCount = 5;
1104
1105
1106  Rays = new dGeomID[RayCount];
1107
1108
1109
1110
1111
1112  /* Ray 0 */
1113
1114
1115  Origin[0] = 1;
1116
1117
1118  Origin[1] = 1;
1119
1120
1121  Origin[2] = 1.5;
1122
1123
1124  Origin[3] = 0;
1125
1126
1127
1128
1129
1130  Direction[0] = 0.0f;
1131
1132
1133  Direction[1] = 0.0f;
1134
1135
1136  Direction[2] = -1;
1137
1138
1139  Direction[3] = 0;
1140
1141
1142
1143
1144
1145  dNormalize3(Direction);
1146
1147
1148
1149
1150
1151  Rays[0] = dGeomCreateRay(space, 5.0f);
1152
1153
1154  dGeomRaySet(Rays[0], Origin, Direction);
1155
1156
1157
1158
1159
1160  /* Ray 1 */
1161
1162
1163  Origin[0] = 0;
1164
1165
1166  Origin[1] = 10;
1167
1168
1169  Origin[2] = 0.25;
1170
1171
1172  Origin[3] = 0;
1173
1174
1175
1176
1177
1178  Direction[0] = 0.0f;
1179
1180
1181  Direction[1] = -1.0f;
1182
1183
1184  Direction[2] = 0.0f;
1185
1186
1187  Direction[3] = 0;
1188
1189
1190
1191
1192
1193  dNormalize3(Direction);
1194
1195
1196
1197
1198
1199  Rays[1] = dGeomCreateRay(space, 20.0f);
1200
1201
1202  dGeomRaySet(Rays[1], Origin, Direction);
1203
1204
1205
1206
1207
1208  /* Ray 2 */
1209
1210
1211  Origin[0] = -10;
1212
1213
1214  Origin[1] = 0;
1215
1216
1217  Origin[2] = 0.20;
1218
1219
1220  Origin[3] = 0;
1221
1222
1223
1224
1225
1226  Direction[0] = 1.0f;
1227
1228
1229  Direction[1] = 0.0f;
1230
1231
1232  Direction[2] = 0.0f;
1233
1234
1235  Direction[3] = 0;
1236
1237
1238
1239
1240
1241  dNormalize3(Direction);
1242
1243
1244
1245
1246
1247  Rays[2] = dGeomCreateRay(space, 20.0f);
1248
1249
1250  dGeomRaySet(Rays[2], Origin, Direction);
1251
1252
1253
1254
1255
1256  /* Ray 3 */
1257
1258
1259  Origin[0] = -9;
1260
1261
1262  Origin[1] = 11;
1263
1264
1265  Origin[2] = 0.15;
1266
1267
1268  Origin[3] = 0;
1269
1270
1271
1272
1273
1274  Direction[0] = 1.0f;
1275
1276
1277  Direction[1] = -1.0f;
1278
1279
1280  Direction[2] = 0.0f;
1281
1282
1283  Direction[3] = 0;
1284
1285
1286
1287
1288
1289  dNormalize3(Direction);
1290
1291
1292
1293
1294
1295  Rays[3] = dGeomCreateRay(space, 20.0f);
1296
1297
1298  dGeomRaySet(Rays[3], Origin, Direction);
1299
1300
1301
1302
1303
1304  /* Ray 4 */
1305
1306
1307  Origin[0] = -0.1;
1308
1309
1310  Origin[1] = 0.3;
1311
1312
1313  Origin[2] = 0.30;
1314
1315
1316  Origin[3] = 0;
1317
1318
1319
1320
1321
1322  Direction[0] = 0.3f;
1323
1324
1325  Direction[1] = 0.5f;
1326
1327
1328  Direction[2] = 1.0f;
1329
1330
1331  Direction[3] = 0;
1332
1333
1334
1335
1336
1337  Rays[4] = dGeomCreateRay(space, 5.0f);
1338
1339
1340  dGeomRaySet(Rays[4], Origin, Direction);
1341
1342
1343
1344
1345
1346  // run simulation
1347
1348
1349  dsSimulationLoop (argc,argv,352,288,&fn);
1350
1351
1352
1353
1354
1355  dJointGroupDestroy (contactgroup);
1356
1357
1358  dSpaceDestroy (space);
1359
1360
1361  dWorldDestroy (world);
1362
1363
1364
1365
1366
1367  return 0;
1368
1369
1370}
1371
1372
Note: See TracBrowser for help on using the repository browser.