| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372 |
- /*************************************************************************
- * *
- * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
- * All rights reserved. Email: [email protected] Web: www.q12.org *
- * *
- * This library is free software; you can redistribute it and/or *
- * modify it under the terms of EITHER: *
- * (1) The GNU Lesser General Public License as published by the Free *
- * Software Foundation; either version 2.1 of the License, or (at *
- * your option) any later version. The text of the GNU Lesser *
- * General Public License is included with this library in the *
- * file LICENSE.TXT. *
- * (2) The BSD-style license that is included with this library in *
- * the file LICENSE-BSD.TXT. *
- * *
- * This library is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
- * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
- * *
- *************************************************************************/
- #include <ode/ode.h>
- #include <dRay.h>
- #include <drawstuff/drawstuff.h>
- #ifdef _MSC_VER
- #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
- #endif
- // select correct drawing functions
- #ifdef dDOUBLE
- #define dsDrawBox dsDrawBoxD
- #define dsDrawSphere dsDrawSphereD
- #define dsDrawCylinder dsDrawCylinderD
- #define dsDrawCappedCylinder dsDrawCappedCylinderD
- #endif
- // some constants
- #define NUM 20 // max number of objects
- #define DENSITY (5.0) // density of all objects
- #define GPB 3 // maximum number of geometries per body
- // dynamics and collision objects
- struct MyObject {
- dBodyID body; // the body
- dGeomID geom[GPB]; // geometries representing this body
- };
- static int num=0; // number of objects in simulation
- static int nextobj=0; // next object to recycle if num==NUM
- static dWorldID world;
- static dSpaceID space;
- static MyObject obj[NUM];
- static dJointGroupID contactgroup;
- static int selected = -1; // selected object
- static dGeomID* Rays;
- static int RayCount;
- // this is called by dSpaceCollide when two objects in space are
- // potentially colliding.
- static void nearCallback (void *data, dGeomID o1, dGeomID o2)
- {
- int i;
- // if (o1->body && o2->body) return;
- // exit without doing anything if the two bodies are connected by a joint
- dBodyID b1 = dGeomGetBody(o1);
- dBodyID b2 = dGeomGetBody(o2);
- if (b1 && b2 && dAreConnected (b1,b2)) return;
- dContact contact[32]; // up to 3 contacts per box
- for (i=0; i<32; i++) {
- contact[i].surface.mode = dContactBounce; //dContactMu2;
- contact[i].surface.mu = dInfinity;
- contact[i].surface.mu2 = 0;
- contact[i].surface.bounce = 0.5;
- contact[i].surface.bounce_vel = 0.1;
- }
- if (int numc = dCollide (o1,o2,3,&contact[0].geom,sizeof(dContact))) {
- dMatrix3 RI;
- dRSetIdentity (RI);
- const dReal ss[3] = {0.02,0.02,0.02};
- for (i=0; i<numc; i++) {
- if (dGeomGetClass(o1) == dRayClass || dGeomGetClass(o2) == dRayClass){
- dMatrix3 Rotation;
- dRSetIdentity(Rotation);
- dsDrawSphere(contact[i].geom.pos, Rotation, REAL(0.01));
- continue;
- }
- dJointID c = dJointCreateContact (world,contactgroup,contact+i);
- dJointAttach (c,b1,b2);
- //dsDrawBox (contact[i].geom.pos,RI,ss);
-
- }
- }
- }
- // start simulation - set viewpoint
- static void start()
- {
- static float xyz[3] = {2.1640f,-1.3079f,1.7600f};
- static float hpr[3] = {125.5000f,-17.0000f,0.0000f};
- dsSetViewpoint (xyz,hpr);
- printf ("To drop another object, press:\n");
- printf (" b for box.\n");
- printf (" s for sphere.\n");
- printf (" c for cylinder.\n");
- printf (" x for a composite object.\n");
- printf ("To select an object, press space.\n");
- printf ("To disable the selected object, press d.\n");
- printf ("To enable the selected object, press e.\n");
- }
- char locase (char c)
- {
- if (c >= 'A' && c <= 'Z') return c - ('a'-'A');
- else return c;
- }
- // called when a key pressed
- static void command (int cmd)
- {
- int i,j,k;
- dReal sides[3];
- dMass m;
- cmd = locase (cmd);
- if (cmd == 'b' || cmd == 's' || cmd == 'c' || cmd == 'x') {
- if (num < NUM) {
- i = num;
- num++;
- }
- else {
- i = nextobj;
- nextobj++;
- if (nextobj >= num) nextobj = 0;
- // destroy the body and geoms for slot i
- dBodyDestroy (obj[i].body);
- for (k=0; k < GPB; k++) {
- if (obj[i].geom[k]) dGeomDestroy (obj[i].geom[k]);
- }
- memset (&obj[i],0,sizeof(obj[i]));
- }
- obj[i].body = dBodyCreate (world);
- for (k=0; k<3; k++) sides[k] = dRandReal()*0.5+0.1;
- dBodySetPosition (obj[i].body,
- dRandReal()*2-1,dRandReal()*2-1,dRandReal()+1);
- dMatrix3 R;
- dRFromAxisAndAngle (R,dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
- dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
- dBodySetRotation (obj[i].body,R);
- dBodySetData (obj[i].body,(void*) i);
- if (cmd == 'b') {
- dMassSetBox (&m,DENSITY,sides[0],sides[1],sides[2]);
- obj[i].geom[0] = dCreateBox (space,sides[0],sides[1],sides[2]);
- }
- else if (cmd == 'c') {
- sides[0] *= 0.5;
- dMassSetCappedCylinder (&m,DENSITY,3,sides[0],sides[1]);
- obj[i].geom[0] = dCreateCCylinder (space,sides[0],sides[1]);
- }
- else if (cmd == 's') {
- sides[0] *= 0.5;
- dMassSetSphere (&m,DENSITY,sides[0]);
- obj[i].geom[0] = dCreateSphere (space,sides[0]);
- }
- else if (cmd == 'x') {
- dGeomID g2[GPB]; // encapsulated geometries
- dReal dpos[GPB][3]; // delta-positions for encapsulated geometries
- // start accumulating masses for the encapsulated geometries
- dMass m2;
- dMassSetZero (&m);
- // set random delta positions
- for (j=0; j<GPB; j++) {
- for (k=0; k<3; k++) dpos[j][k] = dRandReal()*0.3-0.15;
- }
- for (k=0; k<3; k++) {
- obj[i].geom[k] = dCreateGeomTransform (space);
- dGeomTransformSetCleanup (obj[i].geom[k],1);
- if (k==0) {
- dReal radius = dRandReal()*0.25+0.05;
- g2[k] = dCreateSphere (0,radius);
- dMassSetSphere (&m2,DENSITY,radius);
- }
- else if (k==1) {
- g2[k] = dCreateBox (0,sides[0],sides[1],sides[2]);
- dMassSetBox (&m2,DENSITY,sides[0],sides[1],sides[2]);
- }
- else {
- dReal radius = dRandReal()*0.1+0.05;
- dReal length = dRandReal()*1.0+0.1;
- g2[k] = dCreateCCylinder (0,radius,length);
- dMassSetCappedCylinder (&m2,DENSITY,3,radius,length);
- }
- dGeomTransformSetGeom (obj[i].geom[k],g2[k]);
- // set the transformation (adjust the mass too)
- dGeomSetPosition (g2[k],dpos[k][0],dpos[k][1],dpos[k][2]);
- dMassTranslate (&m2,dpos[k][0],dpos[k][1],dpos[k][2]);
- dMatrix3 Rtx;
- dRFromAxisAndAngle (Rtx,dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
- dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
- dGeomSetRotation (g2[k],Rtx);
- dMassRotate (&m2,Rtx);
- // add to the total mass
- dMassAdd (&m,&m2);
- }
- // move all encapsulated objects so that the center of mass is (0,0,0)
- for (k=0; k<2; k++) {
- dGeomSetPosition (g2[k],
- dpos[k][0]-m.c[0],
- dpos[k][1]-m.c[1],
- dpos[k][2]-m.c[2]);
- }
- dMassTranslate (&m,-m.c[0],-m.c[1],-m.c[2]);
- }
- for (k=0; k < GPB; k++) {
- if (obj[i].geom[k]) dGeomSetBody (obj[i].geom[k],obj[i].body);
- }
- dBodySetMass (obj[i].body,&m);
- }
- if (cmd == ' ') {
- selected++;
- if (selected >= num) selected = 0;
- if (selected < 0) selected = 0;
- }
- else if (cmd == 'd' && selected >= 0 && selected < num) {
- dBodyDisable (obj[selected].body);
- }
- else if (cmd == 'e' && selected >= 0 && selected < num) {
- dBodyEnable (obj[selected].body);
- }
- }
- // draw a geom
- void drawGeom (dGeomID g, const dReal *pos, const dReal *R)
- {
- if (!g) return;
- if (!pos) pos = dGeomGetPosition (g);
- if (!R) R = dGeomGetRotation (g);
- int type = dGeomGetClass (g);
- if (type == dBoxClass) {
- dVector3 sides;
- dGeomBoxGetLengths (g,sides);
- dsDrawBox (pos,R,sides);
- }
- else if (type == dSphereClass) {
- dsDrawSphere (pos,R,dGeomSphereGetRadius (g));
- }
- else if (type == dCCylinderClass) {
- dReal radius,length;
- dGeomCCylinderGetParams (g,&radius,&length);
- dsDrawCappedCylinder (pos,R,length,radius);
- }
- else if (type == dGeomTransformClass) {
- dGeomID g2 = dGeomTransformGetGeom (g);
- const dReal *pos2 = dGeomGetPosition (g2);
- const dReal *R2 = dGeomGetRotation (g2);
- dVector3 actual_pos;
- dMatrix3 actual_R;
- dMULTIPLY0_331 (actual_pos,R,pos2);
- actual_pos[0] += pos[0];
- actual_pos[1] += pos[1];
- actual_pos[2] += pos[2];
- dMULTIPLY0_333 (actual_R,R,R2);
- drawGeom (g2,actual_pos,actual_R);
- }
- }
- // simulation loop
- static void simLoop (int pause)
- {
- dsSetColor (0,0,2);
- dSpaceCollide (space,0,&nearCallback);
- if (!pause) dWorldStep (world,0.05);
- // remove all contact joints
- dJointGroupEmpty (contactgroup);
- dsSetColor (1,1,0);
- dsSetTexture (DS_WOOD);
- for (int i=0; i<num; i++) {
- int color_changed = 0;
- if (i==selected) {
- dsSetColor (0,0.7,1);
- color_changed = 1;
- }
- else if (! dBodyIsEnabled (obj[i].body)) {
- dsSetColor (1,0,0);
- color_changed = 1;
- }
- for (int j=0; j < GPB; j++) drawGeom (obj[i].geom[j],0,0);
- if (color_changed) dsSetColor (1,1,0);
- }
- {for (int i = 0; i < RayCount; i++){
- dVector3 Origin, Direction;
- dGeomRayGet(Rays[i], Origin, Direction);
-
- dReal Length = dGeomRayGetLength(Rays[i]);
- dVector3 End;
- End[0] = Origin[0] + (Direction[0] * Length);
- End[1] = Origin[1] + (Direction[1] * Length);
- End[2] = Origin[2] + (Direction[2] * Length);
- End[3] = Origin[3] + (Direction[3] * Length);
-
- dsDrawLine(Origin, End);
- }}
- }
- int main (int argc, char **argv)
- {
- // setup pointers to drawstuff callback functions
- dsFunctions fn;
- fn.version = DS_VERSION;
- fn.start = &start;
- fn.step = &simLoop;
- fn.command = &command;
- fn.stop = 0;
- fn.path_to_textures = "../../drawstuff/textures";
- if(argc==2)
- {
- fn.path_to_textures = argv[1];
- }
- // create world
- world = dWorldCreate();
- space = dHashSpaceCreate();
- contactgroup = dJointGroupCreate (0);
- dWorldSetGravity (world,0,0,-0.5);
- dWorldSetCFM (world,1e-5);
- dCreatePlane (space,0,0,1,0);
- memset (obj,0,sizeof(obj));
- dVector3 Origin, Direction;
-
- RayCount = 5;
- Rays = new dGeomID[RayCount];
- /* Ray 0 */
- Origin[0] = 1;
- Origin[1] = 1;
- Origin[2] = 1.5;
- Origin[3] = 0;
- Direction[0] = 0.0f;
- Direction[1] = 0.0f;
- Direction[2] = -1;
- Direction[3] = 0;
- dNormalize3(Direction);
- Rays[0] = dGeomCreateRay(space, 5.0f);
- dGeomRaySet(Rays[0], Origin, Direction);
- /* Ray 1 */
- Origin[0] = 0;
- Origin[1] = 10;
- Origin[2] = 0.25;
- Origin[3] = 0;
- Direction[0] = 0.0f;
- Direction[1] = -1.0f;
- Direction[2] = 0.0f;
- Direction[3] = 0;
- dNormalize3(Direction);
- Rays[1] = dGeomCreateRay(space, 20.0f);
- dGeomRaySet(Rays[1], Origin, Direction);
- /* Ray 2 */
- Origin[0] = -10;
- Origin[1] = 0;
- Origin[2] = 0.20;
- Origin[3] = 0;
- Direction[0] = 1.0f;
- Direction[1] = 0.0f;
- Direction[2] = 0.0f;
- Direction[3] = 0;
- dNormalize3(Direction);
- Rays[2] = dGeomCreateRay(space, 20.0f);
- dGeomRaySet(Rays[2], Origin, Direction);
- /* Ray 3 */
- Origin[0] = -9;
- Origin[1] = 11;
- Origin[2] = 0.15;
- Origin[3] = 0;
- Direction[0] = 1.0f;
- Direction[1] = -1.0f;
- Direction[2] = 0.0f;
- Direction[3] = 0;
- dNormalize3(Direction);
- Rays[3] = dGeomCreateRay(space, 20.0f);
- dGeomRaySet(Rays[3], Origin, Direction);
- /* Ray 4 */
- Origin[0] = -0.1;
- Origin[1] = 0.3;
- Origin[2] = 0.30;
- Origin[3] = 0;
- Direction[0] = 0.3f;
- Direction[1] = 0.5f;
- Direction[2] = 1.0f;
- Direction[3] = 0;
- Rays[4] = dGeomCreateRay(space, 5.0f);
- dGeomRaySet(Rays[4], Origin, Direction);
- // run simulation
- dsSimulationLoop (argc,argv,352,288,&fn);
- dJointGroupDestroy (contactgroup);
- dSpaceDestroy (space);
- dWorldDestroy (world);
- return 0;
- }
|