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