Panda3D
 All Classes Functions Variables Enumerations
odeJoint.cxx
1 // Filename: odeJoint.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 "config_ode.h"
16 #include "odeJoint.h"
17 #include "odeBallJoint.h"
18 #include "odeHingeJoint.h"
19 #include "odeHinge2Joint.h"
20 #include "odeSliderJoint.h"
21 #include "odeContactJoint.h"
22 #include "odeUniversalJoint.h"
23 #include "odeFixedJoint.h"
24 #include "odeNullJoint.h"
25 #include "odePlane2dJoint.h"
26 #include "odeAMotorJoint.h"
27 #include "odeLMotorJoint.h"
28 #include "odeBody.h"
29 
30 TypeHandle OdeJoint::_type_handle;
31 
32 OdeJoint::
33 OdeJoint() :
34  _id(0) {
35  ostream &out = odejoint_cat.debug();
36  out << get_type() << "(" << _id << ")\n";
37 }
38 
39 OdeJoint::
40 OdeJoint(dJointID id) :
41  _id(id) {
42  ostream &out = odejoint_cat.debug();
43  out << get_type() << "(" << _id << ")\n";
44 }
45 
46 OdeJoint::
47 ~OdeJoint() {
48 }
49 
50 void OdeJoint::
51 destroy() {
52  nassertv(_id);
53  dJointDestroy(_id);
54 }
55 
56 ////////////////////////////////////////////////////////////////////
57 // Function: OdeJoint::attach_bodies
58 // Access: Published
59 // Description: Attaches two OdeBody objects to this joint.
60 // Order is important.
61 // Consider using the OdeJoint::attach extension
62 // function if you're using the Python interface.
63 ////////////////////////////////////////////////////////////////////
64 void OdeJoint::
65 attach_bodies(const OdeBody &body1, const OdeBody &body2) {
66  nassertv(_id);
67  nassertv(body1.get_id() != 0 || body2.get_id() != 0);
68  dJointAttach(_id, body1.get_id(), body2.get_id());
69 }
70 
71 ////////////////////////////////////////////////////////////////////
72 // Function: OdeJoint::attach_body
73 // Access: Published
74 // Description: Attaches a single OdeBody to this joint at the
75 // specified index (0 or 1). The other index will be
76 // set to the environment (null).
77 // Consider using the OdeJoint::attach extension
78 // function if you're using the Python interface.
79 ////////////////////////////////////////////////////////////////////
80 void OdeJoint::
81 attach_body(const OdeBody &body, int index) {
82  nassertv(_id);
83  nassertv(body.get_id() != 0);
84  nassertv(index == 0 || index == 1);
85  if (index == 0) {
86  dJointAttach(_id, body.get_id(), 0);
87  } else {
88  dJointAttach(_id, 0, body.get_id());
89  }
90 }
91 
92 void OdeJoint::
93 detach() {
94  nassertv(_id);
95  dJointAttach(_id, 0, 0);
96 }
97 
98 OdeBody OdeJoint::
99 get_body(int index) const {
100  nassertr(_id, OdeBody(0));
101  nassertr(index == 0 || index == 1, OdeBody(0));
102  return OdeBody(dJointGetBody(_id, index));
103 }
104 
105 void OdeJoint::
106 write(ostream &out, unsigned int indent) const {
107  out.width(indent); out << "" << get_type() \
108  << "(id = " << _id \
109  << ", body1 = ";
110  OdeBody body = get_body(0);
111  if (body.get_id() != 0) {
112  body.write(out);
113  }
114  else {
115  out << "0";
116  }
117  out << ", body2 = ";
118  body = get_body(1);
119  if (body.get_id() != 0) {
120  body.write(out);
121  }
122  else {
123  out << "0";
124  }
125  out << ")";
126 
127 }
128 
129 OdeJoint::
130 operator bool () const {
131  return (_id != NULL);
132 }
133 
134 OdeBallJoint OdeJoint::
135 convert_to_ball() const {
136  nassertr(_id != 0, OdeBallJoint(0));
137  nassertr(get_joint_type() == JT_ball, OdeBallJoint(0));
138  return OdeBallJoint(_id);
139 }
140 
141 OdeHingeJoint OdeJoint::
142 convert_to_hinge() const {
143  nassertr(_id != 0, OdeHingeJoint(0));
144  nassertr(get_joint_type() == JT_hinge, OdeHingeJoint(0));
145  return OdeHingeJoint(_id);
146 }
147 
148 OdeSliderJoint OdeJoint::
149 convert_to_slider() const {
150  nassertr(_id != 0, OdeSliderJoint(0));
151  nassertr(get_joint_type() == JT_slider, OdeSliderJoint(0));
152  return OdeSliderJoint(_id);
153 }
154 
155 OdeContactJoint OdeJoint::
156 convert_to_contact() const {
157  nassertr(_id != 0, OdeContactJoint(0));
158  nassertr(get_joint_type() == JT_contact, OdeContactJoint(0));
159  return OdeContactJoint(_id);
160 }
161 
162 OdeUniversalJoint OdeJoint::
163 convert_to_universal() const {
164  nassertr(_id != 0, OdeUniversalJoint(0));
165  nassertr(get_joint_type() == JT_universal, OdeUniversalJoint(0));
166  return OdeUniversalJoint(_id);
167 }
168 
169 OdeHinge2Joint OdeJoint::
170 convert_to_hinge2() const {
171  nassertr(_id != 0, OdeHinge2Joint(0));
172  nassertr(get_joint_type() == JT_hinge2, OdeHinge2Joint(0));
173  return OdeHinge2Joint(_id);
174 }
175 
176 OdeFixedJoint OdeJoint::
177 convert_to_fixed() const {
178  nassertr(_id != 0, OdeFixedJoint(0));
179  nassertr(get_joint_type() == JT_fixed, OdeFixedJoint(0));
180  return OdeFixedJoint(_id);
181 }
182 
183 OdeNullJoint OdeJoint::
184 convert_to_null() const {
185  nassertr(_id != 0, OdeNullJoint(0));
186  nassertr(get_joint_type() == JT_null, OdeNullJoint(0));
187  return OdeNullJoint(_id);
188 }
189 
190 OdeAMotorJoint OdeJoint::
191 convert_to_a_motor() const {
192  nassertr(_id != 0, OdeAMotorJoint(0));
193  nassertr(get_joint_type() == JT_a_motor, OdeAMotorJoint(0));
194  return OdeAMotorJoint(_id);
195 }
196 
197 OdeLMotorJoint OdeJoint::
198 convert_to_l_motor() const {
199  nassertr(_id != 0, OdeLMotorJoint(0));
200  nassertr(get_joint_type() == JT_l_motor, OdeLMotorJoint(0));
201  return OdeLMotorJoint(_id);
202 }
203 
204 OdePlane2dJoint OdeJoint::
205 convert_to_plane2d() const {
206  nassertr(_id != 0, OdePlane2dJoint(0));
207  nassertr(get_joint_type() == JT_plane2d, OdePlane2dJoint(0));
208  return OdePlane2dJoint(_id);
209 }
210 
void attach_bodies(const OdeBody &body1, const OdeBody &body2)
Attaches two OdeBody objects to this joint.
Definition: odeJoint.cxx:65
dBodyID get_id() const
Returns the underlying dBodyID.
Definition: odeBody.I:34
void attach_body(const OdeBody &body, int index)
Attaches a single OdeBody to this joint at the specified index (0 or 1).
Definition: odeJoint.cxx:81
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85