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