Commit patch from Josh Enes (see LP bug #1027758)

This commit is contained in:
rdb 2013-02-17 21:36:05 +00:00
parent 192d10b937
commit 5846d8d8c4
2 changed files with 32 additions and 101 deletions

View File

@ -28,12 +28,9 @@
TypeHandle OdeSpace::_type_handle;
// this data is used in auto_collide
const int OdeSpace::MAX_CONTACTS = 16;
OdeWorld* OdeSpace::_collide_world;
OdeSpace* OdeSpace::_collide_space;
dJointGroupID OdeSpace::_collide_joint_group;
int OdeSpace::contactCount = 0;
double OdeSpace::contact_data[192];
int OdeSpace::contact_ids[128];
OdeWorld* OdeSpace::_static_auto_collide_world;
OdeSpace* OdeSpace::_static_auto_collide_space;
dJointGroupID OdeSpace::_static_auto_collide_joint_group;
#ifdef HAVE_PYTHON
PyObject* OdeSpace::_python_callback = NULL;
#endif
@ -41,7 +38,8 @@ PyObject* OdeSpace::_python_callback = NULL;
OdeSpace::
OdeSpace(dSpaceID id) :
_id(id) {
my_world = NULL;
_auto_collide_world = NULL;
_auto_collide_joint_group = NULL;
}
OdeSpace::
@ -115,48 +113,24 @@ operator bool () const {
void OdeSpace::
set_auto_collide_world(OdeWorld &world) {
my_world = &world;
_auto_collide_world = &world;
}
void OdeSpace::
set_auto_collide_joint_group(OdeJointGroup &joint_group) {
_collide_joint_group = joint_group.get_id();
_auto_collide_joint_group = joint_group.get_id();
}
int OdeSpace::
void OdeSpace::
auto_collide() {
if (my_world == NULL) {
if (_auto_collide_world == NULL) {
odespace_cat.error() << "No collide world has been set!\n";
return 0;
} else {
nassertr(_id, 0);
OdeSpace::contactCount = 0;
_collide_space = this;
_collide_world = my_world;
_static_auto_collide_space = this;
_static_auto_collide_world = _auto_collide_world;
_static_auto_collide_joint_group = _auto_collide_joint_group;
dSpaceCollide(_id, this, &auto_callback);
return OdeSpace::contactCount;
}
}
double OdeSpace::
get_contact_data(int data_index) {
// get the contact data it looks like so [x1,y1,z1,x2,y2,z2... x64,y64,z64]
// use the return in from autoCollide to determine how much of the data is
// valid. The data would be more straight forward but the callbacks have to be
// static.
return OdeSpace::contact_data[data_index];
}
int OdeSpace::
get_contact_id(int data_index, int first) {
// get the contact data it looks like so [x1,y1,z1,x2,y2,z2... x64,y64,z64]
// use the return in from autoCollide to determine how much of the data is
// valid. The data would be more straight forward but the callbacks have to be
// static.
if (first == 0) {
return OdeSpace::contact_ids[(data_index * 2) + 0];
} else {
return OdeSpace::contact_ids[(data_index * 2) + 1];
}
}
@ -164,18 +138,17 @@ void OdeSpace::
auto_callback(void *data, dGeomID o1, dGeomID o2) {
// uses data stored on the world to resolve collisions so you don't have to use near_callbacks in python
int i;
static int autoCallbackCounter = 0;
dBodyID b1 = dGeomGetBody(o1);
dBodyID b2 = dGeomGetBody(o2);
dContact contact[OdeSpace::MAX_CONTACTS];
int surface1 = _collide_space->get_surface_type(o1);
int surface2 = _collide_space->get_surface_type(o2);
int surface1 = _static_auto_collide_space->get_surface_type(o1);
int surface2 = _static_auto_collide_space->get_surface_type(o2);
nassertv(_collide_world != NULL);
nassertv(_static_auto_collide_world != NULL);
sSurfaceParams collide_params;
collide_params = _collide_world->get_surface(surface1, surface2);
collide_params = _static_auto_collide_world->get_surface(surface1, surface2);
for (i=0; i < OdeSpace::MAX_CONTACTS; i++) {
contact[i].surface.mode = collide_params.colparams.mode;
@ -190,15 +163,12 @@ auto_callback(void *data, dGeomID o1, dGeomID o2) {
numc = dCollide(o1, o2, OdeSpace::MAX_CONTACTS, &contact[0].geom, sizeof(dContact));
if (numc) {
if (odespace_cat.is_debug() && (autoCallbackCounter%30 == 0)) {
odespace_cat.debug() << autoCallbackCounter <<" collision between geoms " << o1 << " and " << o2 << "\n";
odespace_cat.debug() << "collision between body " << b1 << " and " << b2 << "\n";
odespace_cat.debug() << "surface1= "<< surface1 << " surface2=" << surface2 << "\n";
}
autoCallbackCounter += 1;
odespace_cat.debug() << "collision between geoms " << o1 << " and " << o2 << "\n";
odespace_cat.debug() << "collision between body " << b1 << " and " << b2 << "\n";
odespace_cat.debug() << "surface1= "<< surface1 << " surface2=" << surface2 << "\n";
PT(OdeCollisionEntry) entry;
if (!_collide_space->_collision_event.empty()) {
if (!_static_auto_collide_space->_collision_event.empty()) {
entry = new OdeCollisionEntry;
entry->_geom1 = o1;
entry->_geom2 = o2;
@ -209,27 +179,18 @@ auto_callback(void *data, dGeomID o1, dGeomID o2) {
}
for(i=0; i < numc; i++) {
dJointID c = dJointCreateContact(_collide_world->get_id(), _collide_joint_group, contact + i);
if ((_collide_space->get_collide_id(o1) >= 0) && (_collide_space->get_collide_id(o2) >= 0)) {
dJointID c = dJointCreateContact(_static_auto_collide_world->get_id(), _static_auto_collide_joint_group, contact + i);
if ((_static_auto_collide_space->get_collide_id(o1) >= 0) && (_static_auto_collide_space->get_collide_id(o2) >= 0)) {
dJointAttach(c, b1, b2);
}
if (!_collide_space->_collision_event.empty()) {
if (!_static_auto_collide_space->_collision_event.empty()) {
entry->_contact_geoms[i] = contact[i].geom;
}
// this creates contact position data for python. It is useful for debugging only 64 points are stored
if(contactCount < 64) {
OdeSpace::contact_data[0 + (OdeSpace::contactCount * 3)] = contact[i].geom.pos[0];
OdeSpace::contact_data[1 + (OdeSpace::contactCount * 3)] = contact[i].geom.pos[1];
OdeSpace::contact_data[2 + (OdeSpace::contactCount * 3)] = contact[i].geom.pos[2];
OdeSpace::contact_ids[0 + (OdeSpace::contactCount * 2)] = _collide_space->get_collide_id(o1);
OdeSpace::contact_ids[1 + (OdeSpace::contactCount * 2)] = _collide_space->get_collide_id(o2);
OdeSpace::contactCount += 1;
}
}
_collide_world->set_dampen_on_bodies(b1, b2, collide_params.dampen);
_static_auto_collide_world->set_dampen_on_bodies(b1, b2, collide_params.dampen);
if (!_collide_space->_collision_event.empty()) {
throw_event(_collide_space->_collision_event, EventParameter(entry));
if (!_static_auto_collide_space->_collision_event.empty()) {
throw_event(_static_auto_collide_space->_collision_event, EventParameter(entry));
}
}
}
@ -309,10 +270,8 @@ int OdeSpace::
get_surface_type(dGeomID id) {
GeomSurfaceMap::iterator iter = _geom_surface_map.find(id);
if (iter != _geom_surface_map.end()) {
// odespace_cat.debug() << "get_default_surface_type the geomId =" << id <<" surfaceType=" << iter->second << "\n";
return iter->second;
}
// odespace_cat.debug() << "get_default_surface_type not in map, returning 0" ;
return 0;
}
@ -326,18 +285,6 @@ get_surface_type(OdeGeom& geom) {
int OdeSpace::
set_collide_id( int collide_id, dGeomID id) {
_geom_collide_id_map[id]= collide_id;
//odespace_cat.debug() << "set_collide_id " << id << " " << _geom_collide_id_map[id] <<"\n";
/*
GeomCollideIdMap::iterator iter2 = _geom_collide_id_map.begin();
while (iter2 != _geom_collide_id_map.end()) {
odespace_cat.debug() << "set print get_collide_id the geomId =" << iter2->first <<" collide_id=" << iter2->second << "\n";
iter2++;
}
get_collide_id(id);
*/
return _geom_collide_id_map[id];
}
@ -356,21 +303,10 @@ get_collide_id(OdeGeom& geom) {
int OdeSpace::
get_collide_id(dGeomID id) {
/*
GeomCollideIdMap::iterator iter2 = _geom_collide_id_map.begin();
while (iter2 != _geom_collide_id_map.end()) {
odespace_cat.debug() << "get print get_collide_id the geomId =" << iter2->first <<" collide_id=" << iter2->second << "\n";
iter2++;
}
*/
GeomCollideIdMap::iterator iter = _geom_collide_id_map.find(id);
if (iter != _geom_collide_id_map.end()) {
//odespace_cat.debug() << "get_collide_id the geomId =" << id <<" collide_id=" << iter->second << "\n";
return iter->second;
}
//odespace_cat.debug() << "get_collide_id not in map, returning 0 id =" << id << "\n" ;
return 0;
}

View File

@ -88,12 +88,10 @@ PUBLISHED:
OdeHashSpace convert_to_hash_space() const;
OdeQuadTreeSpace convert_to_quad_tree_space() const;
int auto_collide();
void auto_collide();
#ifdef HAVE_PYTHON
int collide(PyObject* arg, PyObject* near_callback);
#endif
static double get_contact_data(int data_index);
int get_contact_id(int data_index, int first = 0);
int set_collide_id(int collide_id, dGeomID id);
int set_collide_id(OdeGeom& geom, int collide_id);
void set_surface_type( int surface_type, dGeomID id);
@ -113,22 +111,19 @@ public:
#endif
INLINE dSpaceID get_id() const;
static OdeWorld* _collide_world;
static OdeSpace* _collide_space;
static dJointGroupID _collide_joint_group;
static OdeWorld* _static_auto_collide_world;
static OdeSpace* _static_auto_collide_space;
static dJointGroupID _static_auto_collide_joint_group;
#ifdef HAVE_PYTHON
static PyObject* _python_callback;
#endif
static int contactCount;
string _collision_event;
static double contact_data[192]; // 64 times three
static int contact_ids[128]; // 64 times two
protected:
dSpaceID _id;
int _g; // REMOVE ME
OdeWorld* my_world;
OdeWorld* _auto_collide_world;
dJointGroupID _auto_collide_joint_group;
public:
static TypeHandle get_class_type() {