Simple ODE Example / Tutorial

Started by
12 comments, last by dexterace 19 years, 1 month ago
This should be all you need, but I'm writing most of it from memory, so some of it could be wrong... What you *should* do is look at ODE's Latest User Guide, since it explains how to create objects and so forth, and you can get the basics of the collision detection functions from the source code provided in the source release.

//thinks you should always keep track of:dWorldID worldID = 0;dSpaceID spaceID = 0;dJointGroupID contactGroup = 0;//these things handle your box:dBodyID bodyID = 0;dGeomID geomID = 0;//INIT:worldID = dWorldCreate();dWorldSetGravity(worldID,0.0f,-9.8f,0.0f);spaceID = dHashSpaceCreate(0);contactGroup = dJointGroupCreate(0);dMass mass;bodyID = dBodyCreate(worldID);dMassSetBox(&mass,1.0f,0.5f,0.5f,0.5f);dBodySetMass(bodyID,&mass);dBodySetPosition(bodyID,0.0f,0.0f,0.0f);geomID = dCreateBox(spaceID,0.5f,0.5f,0.5f);dGeomSetBody(geomID,bodyID);//DEINIT:dGeomDestroy(geomID);dBodyDestroy(bodyID);dWorldDestroy(worldID);dSpaceDestroy(spaceID);dCloseODE();//final clean-up//COLLISION DETECTION:dSpaceCollide(spaceID,0,&callbackFunc);//changeInTime is the seconds passed since the last update:dWorldQuickStep(worldID,changeInTime);dJointGroupEmpty(contactGroup);//COLLISION DETECTION FUNCTION:#define MAX_CONTACTS 16void callbackFunc(void* data,dGeomID o1, dGeomID o2){  dBodyID b1 = dGeomGetBody(o1);  dBodyID b2 = dGeomGetBody(o2);    dContact contacts[MAX_CONTACTS];  int i,numc;  if (numc = dCollide(o1,o2,MAX_CONTACTS,&contacts[0].geom,sizeof(dContact))){    for (i = 0;i < numc;i++){      contacts.surface.mode = dContactSlip1 | dContactSlip2 | dContactSoftERP | dContactSoftCFM | dContactApprox1;      contacts.surface.mu = dInfinity;      contacts.surface.slip1 = 0.1;      contacts.surface.slip2 = 0.1;      contacts.surface.soft_erp = 0.5;      contacts.surface.soft_cfm = 0.0;            dJointID c = dJointCreateContact(worldID,contactGroup,&contacts);      dJointAttach(c,b1,b2);    }  }}
Advertisement
Quote:Original post by jakem3s90
Which compiler / IDE are you using? Also, which graphics API are you using?

I might be able to send you a some code to help you get started!


I'm using C with Dev-C++ and OpenGL :-) ALl open source stuff, but on windows ;-)
I would appreciate it soo much if you would write me up an example! :-) Thank you very much dude.

Gorax, I've read through most of the user manual but I dont quite get how I should structure everything, like, you can create a virtual object but how is the easiest way to read all those objects (and their coords) to draw them to the screen etc...

One useful thing I got from the manual though is this list which shows the sequence that your code should run in :-)

Quote:/*
* A typical simulation will proceed like this:
* 1. Create a dynamics world.
* 2. Create bodies in the dynamics world.
* 3. Set the state (position etc) of all bodies.
* 4. Create joints in the dynamics world.
* 5. Attach the joints to the bodies.
* 6. Set the parameters of all joints.
* 7. Create a collision world and collision geometry objects, as necessary.
* 8. Create a joint group to hold the contact joints.
* 9. Loop:
* (a) Apply forces to the bodies as necessary.
* (b) Adjust the joint parameters as necessary.
* (c) Call collision detection.
* (d) Create a contact joint for every collision point, and put it in the contact joint group.
* (e) Take a simulation step.
* (f) Remove all joints in the contact joint group.
* 10. Destroy the dynamics and collision worlds.
*/

I put it in the header of my header as a reference for me :-)
__________Michael Dawson"IRC is just multiplayer notepad." - Reverend
The examples are pretty complete in getting you up to speed on the various aspects. I recall printing out the docs as I used ODE initially for a program that (genetically) generated articulate motion for models. I should probably put some of it up somewhere as I had to write a custom loader to account for (ODE) physical aspects of the models.
You can open .tar.gz files in WinZip by renaming the file with an extension .tgz

This topic is closed to new replies.

Advertisement