moved loadPhy out of OdeWorld::Object, add<Object>() functions returning success or failure status now

git-svn-id: svn://anubis/misc/OdeWorld@194 bd8a9e45-a331-0410-811e-c64571078777
This commit is contained in:
josh 2009-10-24 21:16:39 +00:00
parent d7e9292cc6
commit a5268d4dd3
2 changed files with 30 additions and 101 deletions

View File

@ -7,26 +7,6 @@
using namespace std; using namespace std;
#define WORLD_STEP 0.001 #define WORLD_STEP 0.001
#define WHITESPACE " \t\r\n\f"
static string trim(const string & orig)
{
string result = orig;
size_t pos = result.find_first_not_of(WHITESPACE);
if (pos == string::npos)
{
result = "";
}
else
{
if (pos > 0)
result = result.substr(pos, result.length() - pos);
pos = result.find_last_not_of(WHITESPACE);
if (pos < result.length() - 1)
result = result.substr(0, pos + 1);
}
return result;
}
/* used by ODE to perform collision detection */ /* used by ODE to perform collision detection */
void OdeWorld_collide_callback(void * data, dGeomID o1, dGeomID o2) void OdeWorld_collide_callback(void * data, dGeomID o1, dGeomID o2)
@ -269,73 +249,10 @@ dGeomID OdeWorld::Object::cloneGeom(dGeomID geom, dBodyID body)
return id; return id;
} }
void OdeWorld::Object::loadPhy(const std::string & path) bool OdeWorld::Object::addCube(const vector<float> args)
{
ifstream ifs(path.c_str());
if (ifs.is_open())
{
while (!ifs.eof())
{
string line;
getline(ifs, line);
line = trim(line);
if (line == "" || line[0] == '#')
continue;
size_t pos = line.find_first_of(WHITESPACE);
if (pos == string::npos)
continue;
string type = line.substr(0, pos);
pos = line.find("\"", pos);
if (pos == string::npos)
continue;
size_t pos2 = line.find("\"", pos + 1);
if (pos2 == string::npos)
continue;
string name = line.substr(pos + 1, pos2 - pos - 1);
pos = pos2 + 1;
vector<float> args;
for (;;)
{
pos = line.find_first_not_of(WHITESPACE, pos);
if (pos == string::npos)
break;
pos2 = line.find_first_of(WHITESPACE, pos);
string n = line.substr(pos, pos2 - pos);
float f = atof(n.c_str());
args.push_back(f);
if (pos2 == string::npos)
break;
pos = pos2 + 1;
}
if (type == "cube")
{
addCube(args);
}
else if (type == "sphere")
{
addSphere(args);
}
else if (type == "cylinder")
{
addCylinder(args);
}
else if (type == "plane")
{
addPlane(args);
}
}
}
if (m_body != 0)
{
dMassTranslate(&m_mass, -m_mass.c[0], -m_mass.c[1], -m_mass.c[2]);
dBodySetMass(m_body, &m_mass);
}
}
void OdeWorld::Object::addCube(const vector<float> args)
{ {
if (args.size() != 9) if (args.size() != 9)
return; return false;
dGeomID id = dCreateBox(0, dGeomID id = dCreateBox(0,
m_scale * args[0], m_scale * args[0],
m_scale * args[1], m_scale * args[1],
@ -348,48 +265,52 @@ void OdeWorld::Object::addCube(const vector<float> args)
setupGeom(id, &mass, setupGeom(id, &mass,
args[3], args[4], args[5], args[3], args[4], args[5],
args[6], args[7], args[8]); args[6], args[7], args[8]);
return true;
} }
void OdeWorld::Object::addSphere(const vector<float> args) bool OdeWorld::Object::addSphere(const vector<float> args)
{ {
if (args.size() != 4) if (args.size() != 4)
return; return false;
dGeomID id = dCreateSphere(0, m_scale * args[0]); dGeomID id = dCreateSphere(0, m_scale * args[0]);
dMass mass; dMass mass;
dMassSetSphere(&mass, 1.0, m_scale * args[0]); dMassSetSphere(&mass, 1.0, m_scale * args[0]);
setupGeom(id, &mass, setupGeom(id, &mass,
args[1], args[2], args[3], args[1], args[2], args[3],
0.0, 0.0, 0.0); 0.0, 0.0, 0.0);
return true;
} }
void OdeWorld::Object::addCylinder(const vector<float> args) bool OdeWorld::Object::addCylinder(const vector<float> args)
{ {
if (args.size() != 8) if (args.size() != 8)
return; return false;
dGeomID id = dCreateCylinder(0, m_scale * args[0], m_scale * args[1]); dGeomID id = dCreateCylinder(0, m_scale * args[0], m_scale * args[1]);
dMass mass; dMass mass;
dMassSetCylinder(&mass, 1.0, 3, m_scale * args[0], m_scale * args[1]); dMassSetCylinder(&mass, 1.0, 3, m_scale * args[0], m_scale * args[1]);
setupGeom(id, &mass, setupGeom(id, &mass,
args[2], args[3], args[4], args[2], args[3], args[4],
args[5], args[6], args[7]); args[5], args[6], args[7]);
return true;
} }
void OdeWorld::Object::addCCylinder(const vector<float> args) bool OdeWorld::Object::addCCylinder(const vector<float> args)
{ {
if (args.size() != 8) if (args.size() != 8)
return; return false;
dGeomID id = dCreateCCylinder(0, m_scale * args[0], m_scale * args[1]); dGeomID id = dCreateCCylinder(0, m_scale * args[0], m_scale * args[1]);
dMass mass; dMass mass;
dMassSetCappedCylinder(&mass, 1.0, 3, m_scale * args[0], m_scale * args[1]); dMassSetCappedCylinder(&mass, 1.0, 3, m_scale * args[0], m_scale * args[1]);
setupGeom(id, &mass, setupGeom(id, &mass,
args[2], args[3], args[4], args[2], args[3], args[4],
args[5], args[6], args[7]); args[5], args[6], args[7]);
return true;
} }
void OdeWorld::Object::addPlane(const vector<float> args) bool OdeWorld::Object::addPlane(const vector<float> args)
{ {
if (args.size() != 6) if (args.size() != 6)
return; return false;
dMatrix3 r; dMatrix3 r;
dRFromEulerAngles(r, args[3], args[4], args[5]); dRFromEulerAngles(r, args[3], args[4], args[5]);
@ -404,6 +325,7 @@ void OdeWorld::Object::addPlane(const vector<float> args)
dGeomID id = dCreatePlane(m_space, a, b, c, d); dGeomID id = dCreatePlane(m_space, a, b, c, d);
m_geoms.push_back(id); m_geoms.push_back(id);
return true;
} }
void OdeWorld::Object::setupGeom(dGeomID geom, dMass * mass, void OdeWorld::Object::setupGeom(dGeomID geom, dMass * mass,
@ -550,3 +472,12 @@ void OdeWorld::Object::addRelTorque(dReal fx, dReal fy, dReal fz)
if (m_body != 0) if (m_body != 0)
dBodyAddRelTorque(m_body, fx, fy, fz); dBodyAddRelTorque(m_body, fx, fy, fz);
} }
void OdeWorld::Object::finalize()
{
if (m_body != 0)
{
dMassTranslate(&m_mass, -m_mass.c[0], -m_mass.c[1], -m_mass.c[2]);
dBodySetMass(m_body, &m_mass);
}
}

View File

@ -21,23 +21,21 @@ class OdeWorld
Object(const Object & orig); Object(const Object & orig);
~Object(); ~Object();
void loadPhy(const std::string & path);
void setPosition(double x, double y, double z); void setPosition(double x, double y, double z);
void getPosition(double * x, double * y, double * z); void getPosition(double * x, double * y, double * z);
void setRotation(dReal x, dReal y, dReal z); void setRotation(dReal x, dReal y, dReal z);
const dReal * getPosition(); const dReal * getPosition();
const dReal * getRotation(); const dReal * getRotation();
std::vector<dGeomID> loadPhy(const std::string & path, bool addCube(const std::vector<float> args);
dBodyID * body, bool is_static = false); bool addSphere(const std::vector<float> args);
void addCube(const std::vector<float> args); bool addCylinder(const std::vector<float> args);
void addSphere(const std::vector<float> args); bool addCCylinder(const std::vector<float> args);
void addCylinder(const std::vector<float> args); bool addPlane(const std::vector<float> args);
void addCCylinder(const std::vector<float> args);
void addPlane(const std::vector<float> args);
void addForce(dReal fx, dReal fy, dReal fz); void addForce(dReal fx, dReal fy, dReal fz);
void addRelForce(dReal fx, dReal fy, dReal fz); void addRelForce(dReal fx, dReal fy, dReal fz);
void addTorque(dReal fx, dReal fy, dReal fz); void addTorque(dReal fx, dReal fy, dReal fz);
void addRelTorque(dReal fx, dReal fy, dReal fz); void addRelTorque(dReal fx, dReal fy, dReal fz);
void finalize();
protected: protected:
bool m_is_static; bool m_is_static;