00001 // Filename: physxRevoluteJoint.cxx 00002 // Created by: enn0x (02Oct09) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "physxRevoluteJoint.h" 00016 #include "physxRevoluteJointDesc.h" 00017 #include "physxJointLimitDesc.h" 00018 #include "physxMotorDesc.h" 00019 #include "physxSpringDesc.h" 00020 00021 TypeHandle PhysxRevoluteJoint::_type_handle; 00022 00023 //////////////////////////////////////////////////////////////////// 00024 // Function: PhysxRevoluteJoint::link 00025 // Access: Public 00026 // Description: 00027 //////////////////////////////////////////////////////////////////// 00028 void PhysxRevoluteJoint:: 00029 link(NxJoint *jointPtr) { 00030 00031 _ptr = jointPtr->isRevoluteJoint(); 00032 _ptr->userData = this; 00033 _error_type = ET_ok; 00034 00035 set_name(jointPtr->getName()); 00036 00037 PhysxScene *scene = (PhysxScene *)_ptr->getScene().userData; 00038 scene->_joints.add(this); 00039 } 00040 00041 //////////////////////////////////////////////////////////////////// 00042 // Function: PhysxRevoluteJoint::unlink 00043 // Access: Public 00044 // Description: 00045 //////////////////////////////////////////////////////////////////// 00046 void PhysxRevoluteJoint:: 00047 unlink() { 00048 00049 _ptr->userData = NULL; 00050 _error_type = ET_released; 00051 00052 PhysxScene *scene = (PhysxScene *)_ptr->getScene().userData; 00053 scene->_joints.remove(this); 00054 } 00055 00056 //////////////////////////////////////////////////////////////////// 00057 // Function : PhysxRevoluteJoint::save_to_desc 00058 // Access : Published 00059 // Description : Saves the state of the joint object to a 00060 // descriptor. 00061 //////////////////////////////////////////////////////////////////// 00062 void PhysxRevoluteJoint:: 00063 save_to_desc(PhysxRevoluteJointDesc &jointDesc) const { 00064 00065 nassertv(_error_type == ET_ok); 00066 _ptr->saveToDesc(jointDesc._desc); 00067 } 00068 00069 //////////////////////////////////////////////////////////////////// 00070 // Function : PhysxRevoluteJoint::load_from_desc 00071 // Access : Published 00072 // Description : Loads the entire state of the joint from a 00073 // descriptor with a single call. 00074 //////////////////////////////////////////////////////////////////// 00075 void PhysxRevoluteJoint:: 00076 load_from_desc(const PhysxRevoluteJointDesc &jointDesc) { 00077 00078 nassertv(_error_type == ET_ok); 00079 _ptr->loadFromDesc(jointDesc._desc); 00080 } 00081 00082 //////////////////////////////////////////////////////////////////// 00083 // Function: PhysxRevoluteJoint::get_angle 00084 // Access: Published 00085 // Description: Retrieves the current revolute joint angle. 00086 // 00087 // The relative orientation of the bodies is stored 00088 // when the joint is created, or when set_axis() or 00089 // set_anchor() is called. This initial orientation 00090 // returns an angle of zero, and joint angles are 00091 // measured relative to this pose. The angle is in 00092 // the range [-180, 180], with positive angles CCW 00093 // around the axis, measured from body2 to body1. 00094 //////////////////////////////////////////////////////////////////// 00095 float PhysxRevoluteJoint:: 00096 get_angle() const { 00097 00098 nassertr(_error_type == ET_ok, 0.0f); 00099 return NxMath::radToDeg(_ptr->getAngle()); 00100 } 00101 00102 //////////////////////////////////////////////////////////////////// 00103 // Function: PhysxRevoluteJoint::get_velocity 00104 // Access: Published 00105 // Description: Retrieves the revolute joint angle's rate of change 00106 // (angular velocity). It is the angular velocity of 00107 // body1 minus body2 projected along the axis. 00108 //////////////////////////////////////////////////////////////////// 00109 float PhysxRevoluteJoint:: 00110 get_velocity() const { 00111 00112 nassertr(_error_type == ET_ok, 0.0f); 00113 return _ptr->getVelocity(); 00114 } 00115 00116 //////////////////////////////////////////////////////////////////// 00117 // Function: PhysxRevoluteJoint::set_projection_mode 00118 // Access: Published 00119 // Description: Sets the joint projection mode. 00120 //////////////////////////////////////////////////////////////////// 00121 void PhysxRevoluteJoint:: 00122 set_projection_mode(PhysxProjectionMode mode) { 00123 00124 nassertv(_error_type == ET_ok); 00125 _ptr->setProjectionMode((NxJointProjectionMode)mode); 00126 } 00127 00128 //////////////////////////////////////////////////////////////////// 00129 // Function: PhysxRevoluteJoint::get_projection_mode 00130 // Access: Published 00131 // Description: Retrieves the joints projection mode. 00132 //////////////////////////////////////////////////////////////////// 00133 PhysxEnums::PhysxProjectionMode PhysxRevoluteJoint:: 00134 get_projection_mode() const { 00135 00136 nassertr(_error_type == ET_ok, PM_none); 00137 return (PhysxProjectionMode)_ptr->getProjectionMode(); 00138 } 00139 00140 //////////////////////////////////////////////////////////////////// 00141 // Function: PhysxRevoluteJoint::set_flag 00142 // Access: Published 00143 // Description: Sets or clears a single RevoluteJointFlag. 00144 //////////////////////////////////////////////////////////////////// 00145 void PhysxRevoluteJoint:: 00146 set_flag(PhysxRevoluteJointFlag flag, bool value) { 00147 00148 nassertv( _error_type == ET_ok ); 00149 NxU32 flags = _ptr->getFlags(); 00150 00151 if (value == true) { 00152 flags |= flag; 00153 } 00154 else { 00155 flags &= ~(flag); 00156 } 00157 00158 _ptr->setFlags(flags); 00159 } 00160 00161 //////////////////////////////////////////////////////////////////// 00162 // Function: PhysxRevoluteJoint::get_flag 00163 // Access: Published 00164 // Description: Returns the value of a single RevoluteJointFlag. 00165 //////////////////////////////////////////////////////////////////// 00166 bool PhysxRevoluteJoint:: 00167 get_flag(PhysxRevoluteJointFlag flag) const { 00168 00169 nassertr(_error_type == ET_ok, false); 00170 return (_ptr->getFlags() & flag) ? true : false; 00171 } 00172 00173 //////////////////////////////////////////////////////////////////// 00174 // Function: PhysxRevoluteJoint::set_spring 00175 // Access: Published 00176 // Description: Sets spring parameters. 00177 // 00178 // The spring is implicitly integrated so no 00179 // instability should result for arbitrary spring and 00180 // damping constants. Using these settings together 00181 // with a motor is not possible -- the motor will have 00182 // priority and the spring settings are ignored. If 00183 // you would like to simulate your motor's internal 00184 // friction, do this by altering the motor parameters 00185 // directly. 00186 // 00187 // spring - The rotational spring acts along the hinge 00188 // axis and tries to force the joint angle to zero. A 00189 // setting of zero disables the spring. Default is 0, 00190 // should be >= 0. 00191 // 00192 // damper - Damping coefficient; acts against the 00193 // hinge's angular velocity. A setting of zero 00194 // disables the damping. The default is 0, should 00195 // be >= 0. 00196 // 00197 // targetValue - The angle at which the spring is 00198 // relaxed. In [-Pi,Pi]. Default is 0. 00199 // 00200 // This automatically enables the spring 00201 //////////////////////////////////////////////////////////////////// 00202 void PhysxRevoluteJoint:: 00203 set_spring(const PhysxSpringDesc &spring) { 00204 00205 nassertv(_error_type == ET_ok); 00206 _ptr->setSpring(spring._desc); 00207 } 00208 00209 //////////////////////////////////////////////////////////////////// 00210 // Function: PhysxRevoluteJoint::set_motor 00211 // Access: Published 00212 // Description: Sets motor parameters for the joint. 00213 // 00214 // For a positive velTarget, the motor pulls the first 00215 // body towards its pulley, for a negative velTarget, 00216 // the motor pulls the second body towards its pulley. 00217 // 00218 // velTarget - the relative velocity the motor is 00219 // trying to achieve. The motor will only be able to 00220 // reach this velocity if the maxForce is sufficiently 00221 // large. If the joint is moving faster than this 00222 // velocity, the motor will actually try to brake. If 00223 // you set this to infinity then the motor will keep 00224 // speeding up, unless there is some sort of 00225 // resistance on the attached bodies. 00226 // 00227 // maxForce - the maximum force the motor can exert. 00228 // Zero disables the motor. Default is 0, should 00229 // be >= 0. Setting this to a very large value if 00230 // velTarget is also very large may not be a good 00231 // idea. 00232 // 00233 // freeSpin - if this flag is set, and if the joint 00234 // is moving faster than velTarget, then neither 00235 // braking nor additional acceleration will result. 00236 // default: false. 00237 // 00238 // This automatically enables the motor. 00239 //////////////////////////////////////////////////////////////////// 00240 void PhysxRevoluteJoint:: 00241 set_motor(const PhysxMotorDesc &motor) { 00242 00243 nassertv(_error_type == ET_ok); 00244 _ptr->setMotor(motor._desc); 00245 } 00246 00247 //////////////////////////////////////////////////////////////////// 00248 // Function: PhysxRevoluteJoint::set_limits 00249 // Access: Published 00250 // Description: Sets angular joint limits. 00251 // 00252 // If either of these limits are set, any planar 00253 // limits in PhysxJoint are ignored. The limits are 00254 // angles defined the same way as the values 00255 // that get_angle() returns. 00256 // 00257 // The following has to hold: 00258 // 00259 // Pi < lowAngle < highAngle < Pi Both limits are 00260 // disabled by default. 00261 // Also sets coefficients of restitutions for the low 00262 // and high angular limits. These settings are only 00263 // used if valid limits are set using set_limits(). 00264 // These restitution coefficients work the same way as 00265 // for contacts. 00266 // 00267 // The coefficient of restitution determines whether a 00268 // collision with the joint limit is completely 00269 // elastic (like pool balls, restitution = 1, no 00270 // energy is lost in the collision), completely 00271 // inelastic (like putty, restitution = 0, no rebound 00272 // after collision) or somewhere in between. The 00273 // default is 0 for both. 00274 // 00275 // This automatically enables the limit. 00276 //////////////////////////////////////////////////////////////////// 00277 void PhysxRevoluteJoint:: 00278 set_limits(const PhysxJointLimitDesc &low, const PhysxJointLimitDesc &high) { 00279 00280 nassertv(_error_type == ET_ok); 00281 00282 NxJointLimitPairDesc limits; 00283 limits.low = low._desc; 00284 limits.high = high._desc; 00285 _ptr->setLimits(limits); 00286 } 00287 00288 //////////////////////////////////////////////////////////////////// 00289 // Function: PhysxRevoluteJoint::get_motor 00290 // Access: Published 00291 // Description: 00292 //////////////////////////////////////////////////////////////////// 00293 PhysxMotorDesc PhysxRevoluteJoint:: 00294 get_motor() const { 00295 00296 nassertr(_error_type == ET_ok, NULL); 00297 00298 PhysxMotorDesc value; 00299 _ptr->getMotor(value._desc); 00300 return value; 00301 } 00302 00303 //////////////////////////////////////////////////////////////////// 00304 // Function: PhysxRevoluteJoint::get_spring 00305 // Access: Published 00306 // Description: 00307 //////////////////////////////////////////////////////////////////// 00308 PhysxSpringDesc PhysxRevoluteJoint:: 00309 get_spring() const { 00310 00311 nassertr(_error_type == ET_ok, NULL); 00312 00313 PhysxSpringDesc value; 00314 _ptr->getSpring(value._desc); 00315 return value; 00316 } 00317