diff --git a/OdeWorld.cc b/OdeWorld.cc index 9e25612..5863a67 100644 --- a/OdeWorld.cc +++ b/OdeWorld.cc @@ -81,7 +81,7 @@ void OdeWorld::step() } vector OdeWorld::loadPhy(const std::string & path, - dBodyID * body, bool static_data) + dBodyID * body, bool is_static) { vector ret; @@ -123,19 +123,19 @@ vector OdeWorld::loadPhy(const std::string & path, } if (type == "cube") { - ret.push_back(addCube(name, static_data, body, args)); + ret.push_back(addCube(name, is_static, body, args)); } else if (type == "sphere") { - ret.push_back(addSphere(name, static_data, body, args)); + ret.push_back(addSphere(name, is_static, body, args)); } else if (type == "cylinder") { - ret.push_back(addCylinder(name, static_data, body, args)); + ret.push_back(addCylinder(name, is_static, body, args)); } else if (type == "plane") { - ret.push_back(addPlane(name, static_data, body, args)); + ret.push_back(addPlane(name, is_static, body, args)); } } } @@ -143,63 +143,63 @@ vector OdeWorld::loadPhy(const std::string & path, return ret; } -dGeomID OdeWorld::addCube(const string & name, bool static_data, +dGeomID OdeWorld::addCube(const string & name, bool is_static, dBodyID * body, const vector args) { if (args.size() != 9) return 0; - dGeomID id = dCreateBox(m_space, args[0], args[1], args[2]); + dGeomID id = dCreateBox(0, args[0], args[1], args[2]); dMass mass; dMassSetBox(&mass, 1.0, args[0], args[1], args[2]); - setupGeom(name, static_data, body, id, &mass, + setupGeom(name, is_static, body, id, &mass, args[3], args[4], args[5], args[6], args[7], args[8]); return id; } -dGeomID OdeWorld::addSphere(const string & name, bool static_data, +dGeomID OdeWorld::addSphere(const string & name, bool is_static, dBodyID * body, const vector args) { if (args.size() != 4) return 0; - dGeomID id = dCreateSphere(m_space, args[0]); + dGeomID id = dCreateSphere(0, args[0]); dMass mass; dMassSetSphere(&mass, 1.0, args[0]); - setupGeom(name, static_data, body, id, &mass, + setupGeom(name, is_static, body, id, &mass, args[1], args[2], args[3], 0.0, 0.0, 0.0); return id; } -dGeomID OdeWorld::addCylinder(const string & name, bool static_data, +dGeomID OdeWorld::addCylinder(const string & name, bool is_static, dBodyID * body, const vector args) { if (args.size() != 8) return 0; - dGeomID id = dCreateCylinder(m_space, args[0], args[1]); + dGeomID id = dCreateCylinder(0, args[0], args[1]); dMass mass; dMassSetCylinder(&mass, 1.0, 3, args[0], args[1]); - setupGeom(name, static_data, body, id, &mass, + setupGeom(name, is_static, body, id, &mass, args[2], args[3], args[4], args[5], args[6], args[7]); return id; } -dGeomID OdeWorld::addCCylinder(const string & name, bool static_data, +dGeomID OdeWorld::addCCylinder(const string & name, bool is_static, dBodyID * body, const vector args) { if (args.size() != 8) return 0; - dGeomID id = dCreateCCylinder(m_space, args[0], args[1]); + dGeomID id = dCreateCCylinder(0, args[0], args[1]); dMass mass; dMassSetCappedCylinder(&mass, 1.0, 3, args[0], args[1]); - setupGeom(name, static_data, body, id, &mass, + setupGeom(name, is_static, body, id, &mass, args[2], args[3], args[4], args[5], args[6], args[7]); return id; } -dGeomID OdeWorld::addPlane(const string & name, bool static_data, +dGeomID OdeWorld::addPlane(const string & name, bool is_static, dBodyID * body, const vector args) { if (args.size() != 6) @@ -220,21 +220,27 @@ dGeomID OdeWorld::addPlane(const string & name, bool static_data, return id; } -void OdeWorld::setupGeom(const std::string & name, bool static_data, +void OdeWorld::setupGeom(const std::string & name, bool is_static, dBodyID * body, dGeomID geom, dMass * mass, float locx, float locy, float locz, float rotx, float roty, float rotz) { - if (!static_data) + dMatrix3 rot; + dRFromEulerAngles(rot, rotx, roty, rotz); + dGeomSetRotation(geom, rot); + dGeomSetPosition(geom, locx, locy, locz); + + if (!is_static) { + /* attach the geometry to the body */ if (*body == 0) *body = dBodyCreate(m_world); - dGeomSetBody(geom, *body); + dGeomId transform = dCreateGeomTransform(m_space); + dGeomTransformSetGeom(transform, geom); + dGeomTransformSetCleanup(transform, 1); - dMatrix3 rot; - dRFromEulerAngles(rot, rotx, roty, rotz); - dGeomSetRotation(geom, rot); + dGeomSetBody(transform, *body); dMassRotate(mass, rot); dMassTranslate(mass, locx, locy, locz); @@ -243,13 +249,6 @@ void OdeWorld::setupGeom(const std::string & name, bool static_data, dMassAdd(&origmass, mass); dBodySetMass(*body, &origmass); } - else - { - dMatrix3 rot; - dRFromEulerAngles(rot, rotx, roty, rotz); - dGeomSetRotation(geom, rot); - dGeomSetPosition(geom, locx, locy, locz); - } } /* push an OpenGL matrix onto the matrix stack for a given @@ -276,3 +275,44 @@ void OdeWorld::pushTransform(const float pos[3], const float R[12]) glPushMatrix(); glMultMatrixf(matrix); } + +OdeWorld::Object::Object(bool is_static) +{ + m_is_static = is_static; +} + +void OdeWorld::Object::setPosition(double x, double y, double z) +{ + if (m_is_static) + { + for (int i = 0, sz = m_geoms.size(); i < sz; i++) + dGeomSetPosition(m_geoms[i], x, y, z); + } + else + { + if (m_body != 0) + dBodySetPosition(m_body, x, y, z); + } +} + +void OdeWorld::Object::getPosition(double * x, double * y, double * z) +{ + const dReal * pos = NULL; + if (m_is_static) + { + if (m_geoms.size() > 0) + pos = dGeomGetPosition(m_geoms[0]); + } + else + { + if (m_body != 0) + pos = dBodyGetPosition(body); + } + + if (pos != NULL) + { + *x = pos[0]; + *y = pos[1]; + *z = pos[2]; + } +} diff --git a/OdeWorld.h b/OdeWorld.h index de66f30..63b7007 100644 --- a/OdeWorld.h +++ b/OdeWorld.h @@ -12,22 +12,38 @@ class OdeWorld public: OdeWorld(); ~OdeWorld(); + + class Object + { + public: + Object(bool is_static); + + void loadPhy(const std::string & path); + void setPosition(double x, double y, double z); + void getPosition(double * x, double * y, double * z); + + protected: + bool m_is_static; + dBodyID m_body; + std::vector m_geoms; + }; + void setGravity(float x, float y, float z) { dWorldSetGravity(m_world, x, y, z); } void step(); std::vector loadPhy(const std::string & path, - dBodyID * body, bool static_data = false); - dGeomID addCube(const std::string & name, bool static_data, + dBodyID * body, bool is_static = false); + dGeomID addCube(const std::string & name, bool is_static, dBodyID * body, const std::vector args); - dGeomID addSphere(const std::string & name, bool static_data, + dGeomID addSphere(const std::string & name, bool is_static, dBodyID * body, const std::vector args); - dGeomID addCylinder(const std::string & name, bool static_data, + dGeomID addCylinder(const std::string & name, bool is_static, dBodyID * body, const std::vector args); - dGeomID addCCylinder(const std::string & name, bool static_data, + dGeomID addCCylinder(const std::string & name, bool is_static, dBodyID * body, const std::vector args); - dGeomID addPlane(const std::string & name, bool static_data, + dGeomID addPlane(const std::string & name, bool is_static, dBodyID * body, const std::vector args); friend void OdeWorld_collide_callback(void * data, @@ -41,7 +57,7 @@ class OdeWorld dJointGroupID m_contactJointGroup; std::map m_objects; - void setupGeom(const std::string & name, bool static_data, + void setupGeom(const std::string & name, bool is_static, dBodyID * body, dGeomID geom, dMass * mass, float locx, float locy, float locz, float rotx, float roty, float rotz);