26 const int OdeSpace::MAX_CONTACTS = 16;
27 OdeWorld* OdeSpace::_static_auto_collide_world;
28 OdeSpace* OdeSpace::_static_auto_collide_space;
29 dJointGroupID OdeSpace::_static_auto_collide_joint_group;
32 OdeSpace(dSpaceID
id) :
34 _auto_collide_world =
nullptr;
35 _auto_collide_joint_group =
nullptr;
49 query(
const OdeGeom& geom)
const {
51 return dSpaceQuery(_id, geom.
get_id());
57 return dSpaceQuery(_id, (dGeomID)space.
get_id());
63 dSpaceAdd(_id, (dGeomID)space.
get_id());
69 dSpaceRemove(_id, (dGeomID)space.
get_id());
75 dSpaceAdd(_id, geom.
get_id());
81 dSpaceRemove(_id, geom.
get_id());
92 nassertr(_id,
OdeGeom(
nullptr));
93 return OdeGeom(dSpaceGetGeom(_id, i));
98 write(std::ostream &out,
unsigned int indent)
const {
99 out.width(
indent); out <<
"" << get_type() <<
"(id = " << _id <<
")";
103 operator bool ()
const {
104 return (_id !=
nullptr);
108 set_auto_collide_world(
OdeWorld &world) {
109 _auto_collide_world = &world;
114 _auto_collide_joint_group = joint_group.get_id();
119 if (_auto_collide_world ==
nullptr) {
120 odespace_cat.error() <<
"No collide world has been set!\n";
122 nassertv(_id !=
nullptr);
123 _static_auto_collide_space =
this;
124 _static_auto_collide_world = _auto_collide_world;
125 _static_auto_collide_joint_group = _auto_collide_joint_group;
126 dSpaceCollide(_id,
this, &auto_callback);
131 auto_callback(
void *data, dGeomID o1, dGeomID o2) {
135 dBodyID b1 = dGeomGetBody(o1);
136 dBodyID b2 = dGeomGetBody(o2);
138 dContact contact[OdeSpace::MAX_CONTACTS];
140 int surface1 = _static_auto_collide_space->get_surface_type(o1);
141 int surface2 = _static_auto_collide_space->get_surface_type(o2);
143 nassertv(_static_auto_collide_world !=
nullptr);
145 collide_params = _static_auto_collide_world->get_surface(surface1, surface2);
147 for (i=0; i < OdeSpace::MAX_CONTACTS; i++) {
148 contact[i].surface.mode = collide_params.colparams.mode;
149 contact[i].surface.mu = collide_params.colparams.mu;
150 contact[i].surface.mu2 = collide_params.colparams.mu2;
151 contact[i].surface.bounce = collide_params.colparams.bounce;
152 contact[i].surface.bounce_vel = collide_params.colparams.bounce_vel;
153 contact[i].surface.soft_cfm = collide_params.colparams.soft_cfm;
157 numc = dCollide(o1, o2, OdeSpace::MAX_CONTACTS, &contact[0].geom,
sizeof(dContact));
160 if (odespace_cat.is_debug()) {
161 odespace_cat.debug() <<
"collision between geoms " << o1 <<
" and " << o2 <<
"\n";
162 odespace_cat.debug() <<
"collision between body " << b1 <<
" and " << b2 <<
"\n";
163 odespace_cat.debug() <<
"surface1= "<< surface1 <<
" surface2=" << surface2 <<
"\n";
167 if (!_static_auto_collide_space->_collision_event.empty()) {
173 entry->_num_contacts = numc;
177 for(i=0; i < numc; i++) {
178 dJointID c = dJointCreateContact(_static_auto_collide_world->
get_id(), _static_auto_collide_joint_group, contact + i);
179 if ((_static_auto_collide_space->get_collide_id(o1) >= 0) && (_static_auto_collide_space->get_collide_id(o2) >= 0)) {
180 dJointAttach(c, b1, b2);
182 if (!_static_auto_collide_space->_collision_event.empty()) {
183 entry->_contact_geoms[i] = contact[i].geom;
186 _static_auto_collide_world->set_dampen_on_bodies(b1, b2, collide_params.dampen);
188 if (!_static_auto_collide_space->_collision_event.empty()) {
189 throw_event(_static_auto_collide_space->_collision_event,
EventParameter(entry));
195 convert_to_simple_space()
const {
197 nassertr(get_class() == OdeGeom::GC_simple_space,
OdeSimpleSpace(
nullptr));
202 convert_to_hash_space()
const {
204 nassertr(get_class() == OdeGeom::GC_hash_space,
OdeHashSpace(
nullptr));
209 convert_to_quad_tree_space()
const {
211 nassertr(get_class() == OdeGeom::GC_quad_tree_space,
OdeQuadTreeSpace(
nullptr));
217 set_surface_type(
int surfaceType, dGeomID
id){
218 _geom_surface_map[id]= surfaceType;
222 set_surface_type(
OdeGeom& geom,
int surfaceType){
223 dGeomID
id = geom.
get_id();
224 set_surface_type(surfaceType,
id);
228 get_surface_type(dGeomID
id) {
229 GeomSurfaceMap::iterator iter = _geom_surface_map.find(
id);
230 if (iter != _geom_surface_map.end()) {
237 get_surface_type(
OdeGeom& geom) {
238 dGeomID
id = geom.
get_id();
239 return get_surface_type(
id);
244 set_collide_id(
int collide_id, dGeomID
id) {
245 _geom_collide_id_map[id]= collide_id;
246 return _geom_collide_id_map[id];
250 set_collide_id(
OdeGeom& geom,
int collide_id) {
251 dGeomID
id = geom.
get_id();
252 return set_collide_id(collide_id,
id);
256 get_collide_id(
OdeGeom& geom) {
257 dGeomID
id = geom.
get_id();
258 return get_collide_id(
id);
263 get_collide_id(dGeomID
id) {
264 GeomCollideIdMap::iterator iter = _geom_collide_id_map.find(
id);
265 if (iter != _geom_collide_id_map.end()) {