Panda3D
 All Classes Functions Variables Enumerations
physxRevoluteJoint.cxx
1 // Filename: physxRevoluteJoint.cxx
2 // Created by: enn0x (02Oct09)
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 "physxRevoluteJoint.h"
16 #include "physxRevoluteJointDesc.h"
17 #include "physxJointLimitDesc.h"
18 #include "physxMotorDesc.h"
19 #include "physxSpringDesc.h"
20 
21 TypeHandle PhysxRevoluteJoint::_type_handle;
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function: PhysxRevoluteJoint::link
25 // Access: Public
26 // Description:
27 ////////////////////////////////////////////////////////////////////
28 void PhysxRevoluteJoint::
29 link(NxJoint *jointPtr) {
30 
31  _ptr = jointPtr->isRevoluteJoint();
32  _ptr->userData = this;
33  _error_type = ET_ok;
34 
35  set_name(jointPtr->getName());
36 
37  PhysxScene *scene = (PhysxScene *)_ptr->getScene().userData;
38  scene->_joints.add(this);
39 }
40 
41 ////////////////////////////////////////////////////////////////////
42 // Function: PhysxRevoluteJoint::unlink
43 // Access: Public
44 // Description:
45 ////////////////////////////////////////////////////////////////////
46 void PhysxRevoluteJoint::
47 unlink() {
48 
49  _ptr->userData = NULL;
50  _error_type = ET_released;
51 
52  PhysxScene *scene = (PhysxScene *)_ptr->getScene().userData;
53  scene->_joints.remove(this);
54 }
55 
56 ////////////////////////////////////////////////////////////////////
57 // Function : PhysxRevoluteJoint::save_to_desc
58 // Access : Published
59 // Description : Saves the state of the joint object to a
60 // descriptor.
61 ////////////////////////////////////////////////////////////////////
64 
65  nassertv(_error_type == ET_ok);
66  _ptr->saveToDesc(jointDesc._desc);
67 }
68 
69 ////////////////////////////////////////////////////////////////////
70 // Function : PhysxRevoluteJoint::load_from_desc
71 // Access : Published
72 // Description : Loads the entire state of the joint from a
73 // descriptor with a single call.
74 ////////////////////////////////////////////////////////////////////
77 
78  nassertv(_error_type == ET_ok);
79  _ptr->loadFromDesc(jointDesc._desc);
80 }
81 
82 ////////////////////////////////////////////////////////////////////
83 // Function: PhysxRevoluteJoint::get_angle
84 // Access: Published
85 // Description: Retrieves the current revolute joint angle.
86 //
87 // The relative orientation of the bodies is stored
88 // when the joint is created, or when set_axis() or
89 // set_anchor() is called. This initial orientation
90 // returns an angle of zero, and joint angles are
91 // measured relative to this pose. The angle is in
92 // the range [-180, 180], with positive angles CCW
93 // around the axis, measured from body2 to body1.
94 ////////////////////////////////////////////////////////////////////
96 get_angle() const {
97 
98  nassertr(_error_type == ET_ok, 0.0f);
99  return NxMath::radToDeg(_ptr->getAngle());
100 }
101 
102 ////////////////////////////////////////////////////////////////////
103 // Function: PhysxRevoluteJoint::get_velocity
104 // Access: Published
105 // Description: Retrieves the revolute joint angle's rate of change
106 // (angular velocity). It is the angular velocity of
107 // body1 minus body2 projected along the axis.
108 ////////////////////////////////////////////////////////////////////
110 get_velocity() const {
111 
112  nassertr(_error_type == ET_ok, 0.0f);
113  return _ptr->getVelocity();
114 }
115 
116 ////////////////////////////////////////////////////////////////////
117 // Function: PhysxRevoluteJoint::set_projection_mode
118 // Access: Published
119 // Description: Sets the joint projection mode.
120 ////////////////////////////////////////////////////////////////////
122 set_projection_mode(PhysxProjectionMode mode) {
123 
124  nassertv(_error_type == ET_ok);
125  _ptr->setProjectionMode((NxJointProjectionMode)mode);
126 }
127 
128 ////////////////////////////////////////////////////////////////////
129 // Function: PhysxRevoluteJoint::get_projection_mode
130 // Access: Published
131 // Description: Retrieves the joints projection mode.
132 ////////////////////////////////////////////////////////////////////
133 PhysxEnums::PhysxProjectionMode PhysxRevoluteJoint::
135 
136  nassertr(_error_type == ET_ok, PM_none);
137  return (PhysxProjectionMode)_ptr->getProjectionMode();
138 }
139 
140 ////////////////////////////////////////////////////////////////////
141 // Function: PhysxRevoluteJoint::set_flag
142 // Access: Published
143 // Description: Sets or clears a single RevoluteJointFlag.
144 ////////////////////////////////////////////////////////////////////
146 set_flag(PhysxRevoluteJointFlag flag, bool value) {
147 
148  nassertv( _error_type == ET_ok );
149  NxU32 flags = _ptr->getFlags();
150 
151  if (value == true) {
152  flags |= flag;
153  }
154  else {
155  flags &= ~(flag);
156  }
157 
158  _ptr->setFlags(flags);
159 }
160 
161 ////////////////////////////////////////////////////////////////////
162 // Function: PhysxRevoluteJoint::get_flag
163 // Access: Published
164 // Description: Returns the value of a single RevoluteJointFlag.
165 ////////////////////////////////////////////////////////////////////
167 get_flag(PhysxRevoluteJointFlag flag) const {
168 
169  nassertr(_error_type == ET_ok, false);
170  return (_ptr->getFlags() & flag) ? true : false;
171 }
172 
173 ////////////////////////////////////////////////////////////////////
174 // Function: PhysxRevoluteJoint::set_spring
175 // Access: Published
176 // Description: Sets spring parameters.
177 //
178 // The spring is implicitly integrated so no
179 // instability should result for arbitrary spring and
180 // damping constants. Using these settings together
181 // with a motor is not possible -- the motor will have
182 // priority and the spring settings are ignored. If
183 // you would like to simulate your motor's internal
184 // friction, do this by altering the motor parameters
185 // directly.
186 //
187 // spring - The rotational spring acts along the hinge
188 // axis and tries to force the joint angle to zero. A
189 // setting of zero disables the spring. Default is 0,
190 // should be >= 0.
191 //
192 // damper - Damping coefficient; acts against the
193 // hinge's angular velocity. A setting of zero
194 // disables the damping. The default is 0, should
195 // be >= 0.
196 //
197 // targetValue - The angle at which the spring is
198 // relaxed. In [-Pi,Pi]. Default is 0.
199 //
200 // This automatically enables the spring
201 ////////////////////////////////////////////////////////////////////
203 set_spring(const PhysxSpringDesc &spring) {
204 
205  nassertv(_error_type == ET_ok);
206  _ptr->setSpring(spring._desc);
207 }
208 
209 ////////////////////////////////////////////////////////////////////
210 // Function: PhysxRevoluteJoint::set_motor
211 // Access: Published
212 // Description: Sets motor parameters for the joint.
213 //
214 // For a positive velTarget, the motor pulls the first
215 // body towards its pulley, for a negative velTarget,
216 // the motor pulls the second body towards its pulley.
217 //
218 // velTarget - the relative velocity the motor is
219 // trying to achieve. The motor will only be able to
220 // reach this velocity if the maxForce is sufficiently
221 // large. If the joint is moving faster than this
222 // velocity, the motor will actually try to brake. If
223 // you set this to infinity then the motor will keep
224 // speeding up, unless there is some sort of
225 // resistance on the attached bodies.
226 //
227 // maxForce - the maximum force the motor can exert.
228 // Zero disables the motor. Default is 0, should
229 // be >= 0. Setting this to a very large value if
230 // velTarget is also very large may not be a good
231 // idea.
232 //
233 // freeSpin - if this flag is set, and if the joint
234 // is moving faster than velTarget, then neither
235 // braking nor additional acceleration will result.
236 // default: false.
237 //
238 // This automatically enables the motor.
239 ////////////////////////////////////////////////////////////////////
241 set_motor(const PhysxMotorDesc &motor) {
242 
243  nassertv(_error_type == ET_ok);
244  _ptr->setMotor(motor._desc);
245 }
246 
247 ////////////////////////////////////////////////////////////////////
248 // Function: PhysxRevoluteJoint::set_limits
249 // Access: Published
250 // Description: Sets angular joint limits.
251 //
252 // If either of these limits are set, any planar
253 // limits in PhysxJoint are ignored. The limits are
254 // angles defined the same way as the values
255 // that get_angle() returns.
256 //
257 // The following has to hold:
258 //
259 // Pi < lowAngle < highAngle < Pi Both limits are
260 // disabled by default.
261 // Also sets coefficients of restitutions for the low
262 // and high angular limits. These settings are only
263 // used if valid limits are set using set_limits().
264 // These restitution coefficients work the same way as
265 // for contacts.
266 //
267 // The coefficient of restitution determines whether a
268 // collision with the joint limit is completely
269 // elastic (like pool balls, restitution = 1, no
270 // energy is lost in the collision), completely
271 // inelastic (like putty, restitution = 0, no rebound
272 // after collision) or somewhere in between. The
273 // default is 0 for both.
274 //
275 // This automatically enables the limit.
276 ////////////////////////////////////////////////////////////////////
279 
280  nassertv(_error_type == ET_ok);
281 
282  NxJointLimitPairDesc limits;
283  limits.low = low._desc;
284  limits.high = high._desc;
285  _ptr->setLimits(limits);
286 }
287 
288 ////////////////////////////////////////////////////////////////////
289 // Function: PhysxRevoluteJoint::get_motor
290 // Access: Published
291 // Description:
292 ////////////////////////////////////////////////////////////////////
293 PhysxMotorDesc PhysxRevoluteJoint::
294 get_motor() const {
295 
296  nassertr(_error_type == ET_ok, NULL);
297 
298  PhysxMotorDesc value;
299  _ptr->getMotor(value._desc);
300  return value;
301 }
302 
303 ////////////////////////////////////////////////////////////////////
304 // Function: PhysxRevoluteJoint::get_spring
305 // Access: Published
306 // Description:
307 ////////////////////////////////////////////////////////////////////
308 PhysxSpringDesc PhysxRevoluteJoint::
309 get_spring() const {
310 
311  nassertr(_error_type == ET_ok, NULL);
312 
313  PhysxSpringDesc value;
314  _ptr->getSpring(value._desc);
315  return value;
316 }
317 
Descriptor class for distance joint.
PhysxProjectionMode get_projection_mode() const
Retrieves the joints projection mode.
void set_flag(PhysxRevoluteJointFlag flag, bool value)
Sets or clears a single RevoluteJointFlag.
void load_from_desc(const PhysxRevoluteJointDesc &jointDesc)
Loads the entire state of the joint from a descriptor with a single call.
Describes a joint motor.
bool get_flag(PhysxRevoluteJointFlag flag) const
Returns the value of a single RevoluteJointFlag.
Describes a joint spring.
A scene is a collection of bodies, constraints, and effectors which can interact. ...
Definition: physxScene.h:73
float get_angle() const
Retrieves the current revolute joint angle.
void save_to_desc(PhysxRevoluteJointDesc &jointDesc) const
Saves the state of the joint object to a descriptor.
void set_spring(const PhysxSpringDesc &spring)
Sets spring parameters.
void set_name(const char *name)
Sets a name string for this object.
Definition: physxJoint.cxx:100
Describes a joint limit.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
void set_motor(const PhysxMotorDesc &motor)
Sets motor parameters for the joint.
void set_projection_mode(PhysxProjectionMode mode)
Sets the joint projection mode.
void set_limits(const PhysxJointLimitDesc &low, const PhysxJointLimitDesc &high)
Sets angular joint limits.
float get_velocity() const
Retrieves the revolute joint angle&#39;s rate of change (angular velocity).