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