ODE常用变量:
dWorldID world;
dSpaceID space;
dJointGroupID contactgroup;
dBodyID body;
dGeomID geom;
初始化:
void InitODE(){
dInitODE();
world=dWorldCreate();
space=dHashSpaceCreate(0);
contactgroup=dJointGroupCreate(0);
dWorldSetGravity(world,0,-9.8,0);
}
回调函数:计算碰撞点
void nearCallback(void* node, dGeomID o1, dGeomID o2)
{
int i=0;
dBodyID b1=dGeomGetBody(o1);
dBodyID b2=dGeomGetBody(o2);
const int MAX_CONTACTS = 8;
if(b1 && b2 && dAreConnectedExcluding(b1,b2,dJointTypeContact))return;
dContact contact[MAX_CONTACTS];
for(i=0;i<MAX_CONTACTS;++i)
{
contact[i].surface.mode=dContactBounce | dContactSoftCFM;
contact[i].surface.mu=100000.0;
contact[i].surface.mu2=.00001;
contact[i].surface.bounce=.2;
contact[i].surface.bounce_vel=.1;
contact[i].surface.soft_cfm=.0000001;
}
int numc=dCollide(o1,o2,MAX_CONTACTS,&contact[0].geom,sizeof(dContact));
if(numc>0)
{
for(i=0;i<numc;i++)
{
dJointID c=dJointCreateContact(world,contactgroup,&contact[i]);
dJointAttach(c,b1,b2);
}
}
}
程序主循环
while(device->run())
{
setPosition(geom);
dJointGroupEmpty (contactgroup); //清空碰撞点组
driver->beginScene(true, true, SColor(0,200,200,200));
dSpaceCollide (space,0,&nearCallback); //通过回调函数计算碰撞点等数据
dWorldQuickStep(world,0.025); //使物理世界随着时间变化
smgr->drawAll();
guienv->drawAll();
driver->endScene();
}