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 */
54 void PhysxRevoluteJoint::
55 save_to_desc(PhysxRevoluteJointDesc &jointDesc) const {
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 */
64 void PhysxRevoluteJoint::
65 load_from_desc(const PhysxRevoluteJointDesc &jointDesc) {
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 */
80 float PhysxRevoluteJoint::
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 */
91 float PhysxRevoluteJoint::
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 */
101 void PhysxRevoluteJoint::
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::
112 get_projection_mode() const {
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 */
121 void PhysxRevoluteJoint::
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 */
140 bool PhysxRevoluteJoint::
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 */
168 void PhysxRevoluteJoint::
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 */
199 void PhysxRevoluteJoint::
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 */
228 void PhysxRevoluteJoint::
229 set_limits(const PhysxJointLimitDesc &low, const PhysxJointLimitDesc &high) {
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 }
PhysxJointLimitDesc
Describes a joint limit.
Definition: physxJointLimitDesc.h:24
PhysxJoint::set_name
void set_name(const char *name)
Sets a name string for this object.
Definition: physxJoint.cxx:91
PhysxMotorDesc
Describes a joint motor.
Definition: physxMotorDesc.h:26
PhysxRevoluteJointDesc
Descriptor class for distance joint.
Definition: physxRevoluteJointDesc.h:29
PhysxRevoluteJoint::get_velocity
float get_velocity() const
Retrieves the revolute joint angle's rate of change (angular velocity).
Definition: physxRevoluteJoint.cxx:92
PhysxRevoluteJoint::set_motor
void set_motor(const PhysxMotorDesc &motor)
Sets motor parameters for the joint.
Definition: physxRevoluteJoint.cxx:200
PhysxRevoluteJoint::get_angle
float get_angle() const
Retrieves the current revolute joint angle.
Definition: physxRevoluteJoint.cxx:81
PhysxRevoluteJoint::save_to_desc
void save_to_desc(PhysxRevoluteJointDesc &jointDesc) const
Saves the state of the joint object to a descriptor.
Definition: physxRevoluteJoint.cxx:55
PhysxRevoluteJoint::set_flag
void set_flag(PhysxRevoluteJointFlag flag, bool value)
Sets or clears a single RevoluteJointFlag.
Definition: physxRevoluteJoint.cxx:122
PhysxRevoluteJoint::set_limits
void set_limits(const PhysxJointLimitDesc &low, const PhysxJointLimitDesc &high)
Sets angular joint limits.
Definition: physxRevoluteJoint.cxx:229
PhysxRevoluteJoint::set_spring
void set_spring(const PhysxSpringDesc &spring)
Sets spring parameters.
Definition: physxRevoluteJoint.cxx:169
PhysxRevoluteJoint::get_flag
bool get_flag(PhysxRevoluteJointFlag flag) const
Returns the value of a single RevoluteJointFlag.
Definition: physxRevoluteJoint.cxx:141
PhysxRevoluteJoint::load_from_desc
void load_from_desc(const PhysxRevoluteJointDesc &jointDesc)
Loads the entire state of the joint from a descriptor with a single call.
Definition: physxRevoluteJoint.cxx:65
PhysxRevoluteJoint::set_projection_mode
void set_projection_mode(PhysxProjectionMode mode)
Sets the joint projection mode.
Definition: physxRevoluteJoint.cxx:102
PhysxRevoluteJoint::get_projection_mode
PhysxProjectionMode get_projection_mode() const
Retrieves the joints projection mode.
Definition: physxRevoluteJoint.cxx:112
PhysxScene
A scene is a collection of bodies, constraints, and effectors which can interact.
Definition: physxScene.h:69
PhysxSpringDesc
Describes a joint spring.
Definition: physxSpringDesc.h:25
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
physxJointLimitDesc.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
physxMotorDesc.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
physxRevoluteJointDesc.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
physxRevoluteJoint.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
physxSpringDesc.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.