Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ode/ode-0.9/ode/demo/demo_trimesh.cpp @ 216

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

[Physik] add ode-0.9

File size: 14.9 KB
Line 
1/*************************************************************************
2 *                                                                       *
3 * Open Dynamics Engine, Copyright (C) 2001-2003 Russell L. Smith.       *
4 * All rights reserved.  Email: russ@q12.org   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
23// TriMesh test by Erwin de Vries
24
25#include <ode/ode.h>
26#include <drawstuff/drawstuff.h>
27
28#ifdef _MSC_VER
29#pragma warning(disable:4244 4305)  // for VC++, no precision loss complaints
30#endif
31
32// select correct drawing functions
33
34#ifdef dDOUBLE
35#define dsDrawBox dsDrawBoxD
36#define dsDrawSphere dsDrawSphereD
37#define dsDrawCylinder dsDrawCylinderD
38#define dsDrawCapsule dsDrawCapsuleD
39#define dsDrawLine dsDrawLineD
40#define dsDrawTriangle dsDrawTriangleD
41#endif
42
43
44// some constants
45
46#define NUM 200                 // max number of objects
47#define DENSITY (5.0)           // density of all objects
48#define GPB 3                   // maximum number of geometries per body
49#define MAX_CONTACTS 40         // maximum number of contact points per body
50
51
52// dynamics and collision objects
53
54struct MyObject {
55  dBodyID body;                 // the body
56  dGeomID geom[GPB];            // geometries representing this body
57};
58
59static int num=0;               // number of objects in simulation
60static int nextobj=0;           // next object to recycle if num==NUM
61static dWorldID world;
62static dSpaceID space;
63static MyObject obj[NUM];
64static dJointGroupID contactgroup;
65static int selected = -1;       // selected object
66static int show_aabb = 0;       // show geom AABBs?
67static int show_contacts = 0;   // show contact points?
68static int random_pos = 1;      // drop objects from random position?
69
70#define VertexCount 5
71#define IndexCount 12
72
73static dVector3 Size;
74static dVector3 Vertices[VertexCount];
75static int Indices[IndexCount];
76
77static dGeomID TriMesh;
78static dGeomID Ray;
79
80
81// this is called by dSpaceCollide when two objects in space are
82// potentially colliding.
83
84static void nearCallback (void *data, dGeomID o1, dGeomID o2)
85{
86  int i;
87  // if (o1->body && o2->body) return;
88
89  // exit without doing anything if the two bodies are connected by a joint
90  dBodyID b1 = dGeomGetBody(o1);
91  dBodyID b2 = dGeomGetBody(o2);
92  if (b1 && b2 && dAreConnectedExcluding (b1,b2,dJointTypeContact)) return;
93
94  dContact contact[MAX_CONTACTS];   // up to MAX_CONTACTS contacts per box-box
95  for (i=0; i<MAX_CONTACTS; i++) {
96    contact[i].surface.mode = dContactBounce | dContactSoftCFM;
97    contact[i].surface.mu = dInfinity;
98    contact[i].surface.mu2 = 0;
99    contact[i].surface.bounce = 0.1;
100    contact[i].surface.bounce_vel = 0.1;
101    contact[i].surface.soft_cfm = 0.01;
102  }
103  if (int numc = dCollide (o1,o2,MAX_CONTACTS,&contact[0].geom,
104                           sizeof(dContact))) {
105    dMatrix3 RI;
106    dRSetIdentity (RI);
107    const dReal ss[3] = {0.02,0.02,0.02};
108    for (i=0; i<numc; i++) {
109                if (dGeomGetClass(o1) == dRayClass || dGeomGetClass(o2) == dRayClass){
110                        dMatrix3 Rotation;
111                        dRSetIdentity(Rotation);
112                        dsDrawSphere(contact[i].geom.pos, Rotation, REAL(0.01));
113                       
114                        dVector3 End;
115                        End[0] = contact[i].geom.pos[0] + (contact[i].geom.normal[0] * contact[i].geom.depth);
116                        End[1] = contact[i].geom.pos[1] + (contact[i].geom.normal[1] * contact[i].geom.depth);
117                        End[2] = contact[i].geom.pos[2] + (contact[i].geom.normal[2] * contact[i].geom.depth);
118                        End[3] = contact[i].geom.pos[3] + (contact[i].geom.normal[3] * contact[i].geom.depth);
119                       
120                        dsDrawLine(contact[i].geom.pos, End);
121                        continue;
122                }
123               
124      dJointID c = dJointCreateContact (world,contactgroup,contact+i);
125      dJointAttach (c,b1,b2);
126      if (show_contacts) dsDrawBox (contact[i].geom.pos,RI,ss);
127    }
128  }
129}
130
131
132// start simulation - set viewpoint
133
134static void start()
135{
136  static float xyz[3] = {2.1640f,-1.3079f,1.7600f};
137  static float hpr[3] = {125.5000f,-17.0000f,0.0000f};
138  dsSetViewpoint (xyz,hpr);
139  printf ("To drop another object, press:\n");
140  printf ("   b for box.\n");
141  printf ("   s for sphere.\n");
142  printf ("   c for cylinder.\n");
143  printf ("   x for a composite object.\n");
144  printf ("To select an object, press space.\n");
145  printf ("To disable the selected object, press d.\n");
146  printf ("To enable the selected object, press e.\n");
147  printf ("To toggle showing the geom AABBs, press a.\n");
148  printf ("To toggle showing the contact points, press t.\n");
149  printf ("To toggle dropping from random position/orientation, press r.\n");
150}
151
152
153char locase (char c)
154{
155  if (c >= 'A' && c <= 'Z') return c - ('a'-'A');
156  else return c;
157}
158
159
160// called when a key pressed
161
162static void command (int cmd)
163{
164  int i,j,k;
165  dReal sides[3];
166  dMass m;
167
168  cmd = locase (cmd);
169  if (cmd == 'b' || cmd == 's' || cmd == 'c' || cmd == 'x'
170      /* || cmd == 'l' */) {
171    if (num < NUM) {
172      i = num;
173      num++;
174    }
175    else {
176      i = nextobj;
177      nextobj++;
178      if (nextobj >= num) nextobj = 0;
179
180      // destroy the body and geoms for slot i
181      dBodyDestroy (obj[i].body);
182      for (k=0; k < GPB; k++) {
183        if (obj[i].geom[k]) dGeomDestroy (obj[i].geom[k]);
184      }
185      memset (&obj[i],0,sizeof(obj[i]));
186    }
187
188    obj[i].body = dBodyCreate (world);
189    for (k=0; k<3; k++) sides[k] = dRandReal()*0.5+0.1;
190
191    dMatrix3 R;
192    if (random_pos) {
193      dBodySetPosition (obj[i].body,
194                        dRandReal()*2-1,dRandReal()*2-1,dRandReal()+1);
195      dRFromAxisAndAngle (R,dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
196                          dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
197    }
198    else {
199      dReal maxheight = 0;
200      for (k=0; k<num; k++) {
201        const dReal *pos = dBodyGetPosition (obj[k].body);
202        if (pos[2] > maxheight) maxheight = pos[2];
203      }
204      dBodySetPosition (obj[i].body, 0,0,maxheight+1);
205      dRFromAxisAndAngle (R,0,0,1,dRandReal()*10.0-5.0);
206    }
207    dBodySetRotation (obj[i].body,R);
208    dBodySetData (obj[i].body,(void*)(size_t)i);
209
210    if (cmd == 'b') {
211      dMassSetBox (&m,DENSITY,sides[0],sides[1],sides[2]);
212      obj[i].geom[0] = dCreateBox (space,sides[0],sides[1],sides[2]);
213    }
214    else if (cmd == 'c') {
215      sides[0] *= 0.5;
216      dMassSetCapsule (&m,DENSITY,3,sides[0],sides[1]);
217      obj[i].geom[0] = dCreateCapsule (space,sides[0],sides[1]);
218    }
219/*
220    // cylinder option not yet implemented
221    else if (cmd == 'l') {
222      sides[1] *= 0.5;
223      dMassSetCapsule (&m,DENSITY,3,sides[0],sides[1]);
224      obj[i].geom[0] = dCreateCylinder (space,sides[0],sides[1]);
225    }
226*/
227    else if (cmd == 's') {
228      sides[0] *= 0.5;
229      dMassSetSphere (&m,DENSITY,sides[0]);
230      obj[i].geom[0] = dCreateSphere (space,sides[0]);
231    }
232    else if (cmd == 'x') {
233      dGeomID g2[GPB];          // encapsulated geometries
234      dReal dpos[GPB][3];       // delta-positions for encapsulated geometries
235
236      // start accumulating masses for the encapsulated geometries
237      dMass m2;
238      dMassSetZero (&m);
239
240      // set random delta positions
241      for (j=0; j<GPB; j++) {
242        for (k=0; k<3; k++) dpos[j][k] = dRandReal()*0.3-0.15;
243      }
244
245      for (k=0; k<GPB; k++) {
246        obj[i].geom[k] = dCreateGeomTransform (space);
247        dGeomTransformSetCleanup (obj[i].geom[k],1);
248        if (k==0) {
249          dReal radius = dRandReal()*0.25+0.05;
250          g2[k] = dCreateSphere (0,radius);
251          dMassSetSphere (&m2,DENSITY,radius);
252        }
253        else if (k==1) {
254          g2[k] = dCreateBox (0,sides[0],sides[1],sides[2]);
255          dMassSetBox (&m2,DENSITY,sides[0],sides[1],sides[2]);
256        }
257        else {
258          dReal radius = dRandReal()*0.1+0.05;
259          dReal length = dRandReal()*1.0+0.1;
260          g2[k] = dCreateCapsule (0,radius,length);
261          dMassSetCapsule (&m2,DENSITY,3,radius,length);
262        }
263        dGeomTransformSetGeom (obj[i].geom[k],g2[k]);
264
265        // set the transformation (adjust the mass too)
266        dGeomSetPosition (g2[k],dpos[k][0],dpos[k][1],dpos[k][2]);
267        dMassTranslate (&m2,dpos[k][0],dpos[k][1],dpos[k][2]);
268        dMatrix3 Rtx;
269        dRFromAxisAndAngle (Rtx,dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
270                            dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
271        dGeomSetRotation (g2[k],Rtx);
272        dMassRotate (&m2,Rtx);
273
274        // add to the total mass
275        dMassAdd (&m,&m2);
276      }
277
278      // move all encapsulated objects so that the center of mass is (0,0,0)
279      for (k=0; k<2; k++) {
280        dGeomSetPosition (g2[k],
281                          dpos[k][0]-m.c[0],
282                          dpos[k][1]-m.c[1],
283                          dpos[k][2]-m.c[2]);
284      }
285      dMassTranslate (&m,-m.c[0],-m.c[1],-m.c[2]);
286    }
287
288    for (k=0; k < GPB; k++) {
289      if (obj[i].geom[k]) dGeomSetBody (obj[i].geom[k],obj[i].body);
290    }
291
292    dBodySetMass (obj[i].body,&m);
293  }
294
295  if (cmd == ' ') {
296    selected++;
297    if (selected >= num) selected = 0;
298    if (selected < 0) selected = 0;
299  }
300  else if (cmd == 'd' && selected >= 0 && selected < num) {
301    dBodyDisable (obj[selected].body);
302  }
303  else if (cmd == 'e' && selected >= 0 && selected < num) {
304    dBodyEnable (obj[selected].body);
305  }
306  else if (cmd == 'a') {
307    show_aabb ^= 1;
308  }
309  else if (cmd == 't') {
310    show_contacts ^= 1;
311  }
312  else if (cmd == 'r') {
313    random_pos ^= 1;
314  }
315}
316
317
318// draw a geom
319
320void drawGeom (dGeomID g, const dReal *pos, const dReal *R, int show_aabb)
321{
322  if (!g) return;
323  if (!pos) pos = dGeomGetPosition (g);
324  if (!R) R = dGeomGetRotation (g);
325
326  int type = dGeomGetClass (g);
327  if (type == dBoxClass) {
328    dVector3 sides;
329    dGeomBoxGetLengths (g,sides);
330    dsDrawBox (pos,R,sides);
331  }
332  else if (type == dSphereClass) {
333    dsDrawSphere (pos,R,dGeomSphereGetRadius (g));
334  }
335  else if (type == dCapsuleClass) {
336    dReal radius,length;
337    dGeomCapsuleGetParams (g,&radius,&length);
338    dsDrawCapsule (pos,R,length,radius);
339  }
340/*
341  // cylinder option not yet implemented
342  else if (type == dCylinderClass) {
343    dReal radius,length;
344    dGeomCylinderGetParams (g,&radius,&length);
345    dsDrawCylinder (pos,R,length,radius);
346  }
347*/
348  else if (type == dGeomTransformClass) {
349    dGeomID g2 = dGeomTransformGetGeom (g);
350    const dReal *pos2 = dGeomGetPosition (g2);
351    const dReal *R2 = dGeomGetRotation (g2);
352    dVector3 actual_pos;
353    dMatrix3 actual_R;
354    dMULTIPLY0_331 (actual_pos,R,pos2);
355    actual_pos[0] += pos[0];
356    actual_pos[1] += pos[1];
357    actual_pos[2] += pos[2];
358    dMULTIPLY0_333 (actual_R,R,R2);
359    drawGeom (g2,actual_pos,actual_R,0);
360  }
361
362  if (show_aabb) {
363    // draw the bounding box for this geom
364    dReal aabb[6];
365    dGeomGetAABB (g,aabb);
366    dVector3 bbpos;
367    for (int i=0; i<3; i++) bbpos[i] = 0.5*(aabb[i*2] + aabb[i*2+1]);
368    dVector3 bbsides;
369    for (int j=0; j<3; j++) bbsides[j] = aabb[j*2+1] - aabb[j*2];
370    dMatrix3 RI;
371    dRSetIdentity (RI);
372    dsSetColorAlpha (1,0,0,0.5);
373    dsDrawBox (bbpos,RI,bbsides);
374  }
375}
376
377
378// simulation loop
379
380static void simLoop (int pause)
381{
382  dsSetColor (0,0,2);
383  dSpaceCollide (space,0,&nearCallback);
384  if (!pause) dWorldStep (world,0.05);
385  //if (!pause) dWorldStepFast (world,0.05, 1);
386
387  // remove all contact joints
388  dJointGroupEmpty (contactgroup);
389
390  dsSetColor (1,1,0);
391  dsSetTexture (DS_WOOD);
392  for (int i=0; i<num; i++) {
393    for (int j=0; j < GPB; j++) {
394      if (i==selected) {
395        dsSetColor (0,0.7,1);
396      }
397      else if (! dBodyIsEnabled (obj[i].body)) {
398        dsSetColor (1,0,0);
399      }
400      else {
401        dsSetColor (1,1,0);
402      }
403      drawGeom (obj[i].geom[j],0,0,show_aabb);
404    }
405  }
406
407  /*{
408    for (int i = 1; i < IndexCount; i++) {
409      dsDrawLine(Vertices[Indices[i - 1]], Vertices[Indices[i]]);
410    }
411  }*/
412
413  {const dReal* Pos = dGeomGetPosition(TriMesh);
414  const dReal* Rot = dGeomGetRotation(TriMesh);
415
416  {for (int i = 0; i < IndexCount / 3; i++){
417    const dVector3& v0 = Vertices[Indices[i * 3 + 0]];
418        const dVector3& v1 = Vertices[Indices[i * 3 + 1]];
419        const dVector3& v2 = Vertices[Indices[i * 3 + 2]];
420        dsDrawTriangle(Pos, Rot, (dReal*)&v0, (dReal*)&v1, (dReal*)&v2, 0);
421  }}}
422
423  if (Ray){
424          dVector3 Origin, Direction;
425          dGeomRayGet(Ray, Origin, Direction);
426         
427          dReal Length = dGeomRayGetLength(Ray);
428         
429          dVector3 End;
430          End[0] = Origin[0] + (Direction[0] * Length);
431          End[1] = Origin[1] + (Direction[1] * Length);
432          End[2] = Origin[2] + (Direction[2] * Length);
433          End[3] = Origin[3] + (Direction[3] * Length);
434         
435          dsDrawLine(Origin, End);
436  }
437}
438
439
440int main (int argc, char **argv)
441{
442  // setup pointers to drawstuff callback functions
443  dsFunctions fn;
444  fn.version = DS_VERSION;
445  fn.start = &start;
446  fn.step = &simLoop;
447  fn.command = &command;
448  fn.stop = 0;
449  fn.path_to_textures = "../../drawstuff/textures";
450  if(argc==2)
451    {
452        fn.path_to_textures = argv[1];
453    }
454
455  // create world
456  dInitODE();
457  world = dWorldCreate();
458
459  space = dSimpleSpaceCreate(0);
460  contactgroup = dJointGroupCreate (0);
461  dWorldSetGravity (world,0,0,-0.5);
462  dWorldSetCFM (world,1e-5);
463  //dCreatePlane (space,0,0,1,0);
464  memset (obj,0,sizeof(obj));
465 
466  Size[0] = 5.0f;
467  Size[1] = 5.0f;
468  Size[2] = 2.5f;
469 
470  Vertices[0][0] = -Size[0];
471  Vertices[0][1] = -Size[1];
472  Vertices[0][2] = Size[2];
473 
474  Vertices[1][0] = Size[0];
475  Vertices[1][1] = -Size[1];
476  Vertices[1][2] = Size[2];
477 
478  Vertices[2][0] = Size[0];
479  Vertices[2][1] = Size[1];
480  Vertices[2][2] = Size[2];
481 
482  Vertices[3][0] = -Size[0];
483  Vertices[3][1] = Size[1];
484  Vertices[3][2] = Size[2];
485 
486  Vertices[4][0] = 0;
487  Vertices[4][1] = 0;
488  Vertices[4][2] = 0;
489 
490  Indices[0] = 0;
491  Indices[1] = 1;
492  Indices[2] = 4;
493 
494  Indices[3] = 1;
495  Indices[4] = 2;
496  Indices[5] = 4;
497 
498  Indices[6] = 2;
499  Indices[7] = 3;
500  Indices[8] = 4;
501 
502  Indices[9] = 3;
503  Indices[10] = 0;
504  Indices[11] = 4;
505
506  dTriMeshDataID Data = dGeomTriMeshDataCreate();
507
508  dGeomTriMeshDataBuildSimple(Data, (dReal*)Vertices, VertexCount, Indices, IndexCount);
509 
510  TriMesh = dCreateTriMesh(space, Data, 0, 0, 0);
511
512  //dGeomSetPosition(TriMesh, 0, 0, 1.0);
513 
514  Ray = dCreateRay(space, 0.9);
515  dVector3 Origin, Direction;
516  Origin[0] = 0.0;
517  Origin[1] = 0;
518  Origin[2] = 0.5;
519  Origin[3] = 0;
520 
521  Direction[0] = 0;
522  Direction[1] = 1.1f;
523  Direction[2] = -1;
524  Direction[3] = 0;
525  dNormalize3(Direction);
526 
527  dGeomRaySet(Ray, Origin[0], Origin[1], Origin[2], Direction[0], Direction[1], Direction[2]);
528 
529  // run simulation
530  dsSimulationLoop (argc,argv,352,288,&fn);
531
532  dJointGroupDestroy (contactgroup);
533  dSpaceDestroy (space);
534  dWorldDestroy (world);
535  dCloseODE();
536  return 0;
537}
Note: See TracBrowser for help on using the repository browser.