Panda3D
odeUtil.cxx
1 // Filename: odeUtil.cxx
2 // Created by: joswilso (27Dec06)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "odeUtil.h"
16 
17 dReal OdeUtil::OC_infinity = dInfinity;
18 
19 ////////////////////////////////////////////////////////////////////
20 // Function: OdeUtil::get_connecting_joint
21 // Access: Public, Static
22 // Description: Returns the joint that connects the given bodies.
23 ////////////////////////////////////////////////////////////////////
25 get_connecting_joint(const OdeBody &body1, const OdeBody &body2) {
26  return OdeJoint(dConnectingJoint(body1.get_id(),body2.get_id()));
27 }
28 
29 ////////////////////////////////////////////////////////////////////
30 // Function: OdeUtil::get_connecting_joint_list
31 // Access: Public, Static
32 // Description: Returns a collection of joints connecting the
33 // specified bodies.
34 ////////////////////////////////////////////////////////////////////
36 get_connecting_joint_list(const OdeBody &body1, const OdeBody &body2) {
37  const int max_possible_joints = min(body1.get_num_joints(), body1.get_num_joints());
38 
39  dJointID *joint_list = (dJointID *)PANDA_MALLOC_ARRAY(max_possible_joints * sizeof(dJointID));
40  int num_joints = dConnectingJointList(body1.get_id(), body2.get_id(),
41  joint_list);
42  OdeJointCollection joints;
43  for (int i = 0; i < num_joints; i++) {
44  joints.add_joint(OdeJoint(joint_list[i]));
45  }
46 
47  PANDA_FREE_ARRAY(joint_list);
48  return joints;
49 }
50 
51 ////////////////////////////////////////////////////////////////////
52 // Function: OdeUtil::are_connected
53 // Access: Public, Static
54 // Description: Returns 1 if the given bodies are connected
55 // by a joint, returns 0 otherwise.
56 ////////////////////////////////////////////////////////////////////
57 int OdeUtil::
58 are_connected(const OdeBody &body1, const OdeBody &body2) {
59  return dAreConnected(body1.get_id(),body2.get_id());
60 }
61 
62 ////////////////////////////////////////////////////////////////////
63 // Function: OdeUtil::are_connected_excluding
64 // Access: Public, Static
65 // Description: Returns 1 if the given bodies are connected
66 // by a joint that does not match the given
67 // joint_type, returns 0 otherwise. This is useful
68 // for deciding whether to add contact joints between
69 // two bodies: if they are already connected by
70 // non-contact joints then it may not be appropriate
71 // to add contacts, however it is okay to add more
72 // contact between bodies that already have contacts.
73 ////////////////////////////////////////////////////////////////////
74 int OdeUtil::
76  const OdeBody &body2,
77  const int joint_type) {
78  return dAreConnectedExcluding(body1.get_id(),
79  body2.get_id(),
80  joint_type);
81 }
82 
83 ////////////////////////////////////////////////////////////////////
84 // Function: OdeUtil::collide
85 // Access: Public, Static
86 // Description: Given two geometry objects that potentially touch
87 // (geom1 and geom2), generate contact information
88 // for them. Returns an OdeCollisionEntry.
89 ////////////////////////////////////////////////////////////////////
90 PT(OdeCollisionEntry) OdeUtil::
91 collide(const OdeGeom &geom1, const OdeGeom &geom2, const short int max_contacts) {
92  dContactGeom *contact_list = (dContactGeom *)PANDA_MALLOC_ARRAY(max_contacts * sizeof(dContactGeom));
93  int num_contacts = dCollide(geom1.get_id(), geom2.get_id(), max_contacts, contact_list, sizeof(dContactGeom));
94  PT(OdeCollisionEntry) entry = new OdeCollisionEntry();
95  entry->_geom1 = geom1.get_id();
96  entry->_geom2 = geom2.get_id();
97  entry->_body1 = dGeomGetBody(geom1.get_id());
98  entry->_body2 = dGeomGetBody(geom2.get_id());
99  entry->_num_contacts = num_contacts;
100  entry->_contact_geoms = new OdeContactGeom[num_contacts];
101  for (int i = 0; i < num_contacts; i++) {
102  entry->_contact_geoms[i] = contact_list[i];
103  }
104 
105  PANDA_FREE_ARRAY(contact_list);
106  return entry;
107 }
108 
109 OdeGeom OdeUtil::
110 space_to_geom(const OdeSpace &space) {
111  return OdeGeom((dGeomID)space.get_id());
112 }
dSpaceID get_id() const
Returns the underlying dSpaceID.
Definition: odeSpace.I:34
dGeomID get_id() const
Returns the underlying dGeomID.
Definition: odeGeom.I:34
A class used to hold information about a collision that has occurred.
static OdeJoint get_connecting_joint(const OdeBody &body1, const OdeBody &body2)
Returns the joint that connects the given bodies.
Definition: odeUtil.cxx:25
dBodyID get_id() const
Returns the underlying dBodyID.
Definition: odeBody.I:34
static int are_connected(const OdeBody &body1, const OdeBody &body2)
Returns 1 if the given bodies are connected by a joint, returns 0 otherwise.
Definition: odeUtil.cxx:58
static OdeJointCollection get_connecting_joint_list(const OdeBody &body1, const OdeBody &body2)
Returns a collection of joints connecting the specified bodies.
Definition: odeUtil.cxx:36
static int are_connected_excluding(const OdeBody &body1, const OdeBody &body2, const int joint_type)
Returns 1 if the given bodies are connected by a joint that does not match the given joint_type...
Definition: odeUtil.cxx:75