Panda3D
|
00001 // Filename: physxJoint.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 "physxJoint.h" 00016 #include "physxManager.h" 00017 #include "physxActor.h" 00018 #include "physxScene.h" 00019 #include "physxCylindricalJoint.h" 00020 #include "physxDistanceJoint.h" 00021 #include "physxFixedJoint.h" 00022 #include "physxPointInPlaneJoint.h" 00023 #include "physxPointOnLineJoint.h" 00024 #include "physxPrismaticJoint.h" 00025 #include "physxPulleyJoint.h" 00026 #include "physxRevoluteJoint.h" 00027 #include "physxSphericalJoint.h" 00028 #include "physxD6Joint.h" 00029 00030 TypeHandle PhysxJoint::_type_handle; 00031 00032 //////////////////////////////////////////////////////////////////// 00033 // Function: PhysxJoint::release 00034 // Access: Published 00035 // Description: 00036 //////////////////////////////////////////////////////////////////// 00037 void PhysxJoint:: 00038 release() { 00039 00040 nassertv(_error_type == ET_ok); 00041 00042 unlink(); 00043 ptr()->getScene().releaseJoint(*ptr()); 00044 } 00045 00046 //////////////////////////////////////////////////////////////////// 00047 // Function: PhysxJoint::factory 00048 // Access: Public 00049 // Description: 00050 //////////////////////////////////////////////////////////////////// 00051 PhysxJoint *PhysxJoint:: 00052 factory(NxJointType shapeType) { 00053 00054 switch (shapeType) { 00055 00056 case NX_JOINT_PRISMATIC: 00057 return new PhysxPrismaticJoint(); 00058 00059 case NX_JOINT_REVOLUTE: 00060 return new PhysxRevoluteJoint(); 00061 00062 case NX_JOINT_CYLINDRICAL: 00063 return new PhysxCylindricalJoint(); 00064 00065 case NX_JOINT_SPHERICAL: 00066 return new PhysxSphericalJoint(); 00067 00068 case NX_JOINT_POINT_ON_LINE: 00069 return new PhysxPointOnLineJoint(); 00070 00071 case NX_JOINT_POINT_IN_PLANE: 00072 return new PhysxPointInPlaneJoint(); 00073 00074 case NX_JOINT_DISTANCE: 00075 return new PhysxDistanceJoint(); 00076 00077 case NX_JOINT_PULLEY: 00078 return new PhysxPulleyJoint(); 00079 00080 case NX_JOINT_FIXED: 00081 return new PhysxFixedJoint(); 00082 00083 case NX_JOINT_D6: 00084 return new PhysxD6Joint(); 00085 } 00086 00087 physx_cat.error() << "Unknown joint type.\n"; 00088 return NULL; 00089 } 00090 00091 //////////////////////////////////////////////////////////////////// 00092 // Function: PhysxJoint::set_name 00093 // Access: Published 00094 // Description: Sets a name string for this object. The name can 00095 // be retrieved again with get_name(). 00096 // This is for debugging and is not used by the 00097 // physics engine. 00098 //////////////////////////////////////////////////////////////////// 00099 void PhysxJoint:: 00100 set_name(const char *name) { 00101 00102 nassertv(_error_type == ET_ok); 00103 00104 _name = name ? name : ""; 00105 ptr()->setName(_name.c_str()); 00106 } 00107 00108 //////////////////////////////////////////////////////////////////// 00109 // Function: PhysxJoint::get_name 00110 // Access: Published 00111 // Description: Returns the name string. 00112 //////////////////////////////////////////////////////////////////// 00113 const char *PhysxJoint:: 00114 get_name() const { 00115 00116 nassertr(_error_type == ET_ok, ""); 00117 return ptr()->getName(); 00118 } 00119 00120 //////////////////////////////////////////////////////////////////// 00121 // Function: PhysxJoint::get_actor 00122 // Access: Published 00123 // Description: Retrieves the actor which this joint is associated 00124 // with. 00125 //////////////////////////////////////////////////////////////////// 00126 PhysxActor *PhysxJoint:: 00127 get_actor(unsigned int idx) const { 00128 00129 nassertr_always(idx < 2, NULL); 00130 nassertr(_error_type == ET_ok, NULL); 00131 00132 NxActor *actorPtr[2]; 00133 ptr()->getActors(&actorPtr[0], &actorPtr[1]); 00134 return (PhysxActor *)(actorPtr[idx]->userData); 00135 } 00136 00137 //////////////////////////////////////////////////////////////////// 00138 // Function: PhysxJoint::get_scene 00139 // Access: Published 00140 // Description: Retrieves the scene which this joint is associated 00141 // with. 00142 //////////////////////////////////////////////////////////////////// 00143 PhysxScene *PhysxJoint:: 00144 get_scene() const { 00145 00146 nassertr(_error_type == ET_ok, NULL); 00147 return (PhysxScene *)(ptr()->getScene().userData); 00148 } 00149 00150 //////////////////////////////////////////////////////////////////// 00151 // Function: PhysxJoint::set_global_anchor 00152 // Access: Published 00153 // Description: Sets the point where the two actors are attached, 00154 // specified in global coordinates. 00155 //////////////////////////////////////////////////////////////////// 00156 void PhysxJoint:: 00157 set_global_anchor(const LPoint3f &anchor) { 00158 00159 nassertv(_error_type == ET_ok); 00160 ptr()->setGlobalAnchor(PhysxManager::point3_to_nxVec3(anchor)); 00161 } 00162 00163 //////////////////////////////////////////////////////////////////// 00164 // Function: PhysxJoint::get_global_anchor 00165 // Access: Published 00166 // Description: Retrieves the joint anchor. 00167 //////////////////////////////////////////////////////////////////// 00168 LPoint3f PhysxJoint:: 00169 get_global_anchor() const { 00170 00171 nassertr(_error_type == ET_ok, LPoint3f::zero()); 00172 return PhysxManager::nxVec3_to_point3(ptr()->getGlobalAnchor()); 00173 } 00174 00175 //////////////////////////////////////////////////////////////////// 00176 // Function: PhysxJoint::set_global_axis 00177 // Access: Published 00178 // Description: Sets the direction of the joint's primary axis, 00179 // specified in global coordinates. 00180 //////////////////////////////////////////////////////////////////// 00181 void PhysxJoint:: 00182 set_global_axis(const LVector3f &axis) { 00183 00184 nassertv(_error_type == ET_ok); 00185 ptr()->setGlobalAxis(PhysxManager::vec3_to_nxVec3(axis)); 00186 } 00187 00188 //////////////////////////////////////////////////////////////////// 00189 // Function: PhysxJoint::get_global_axis 00190 // Access: Published 00191 // Description: Retrieves the joint axis. 00192 //////////////////////////////////////////////////////////////////// 00193 LVector3f PhysxJoint:: 00194 get_global_axis() const { 00195 00196 nassertr(_error_type == ET_ok, LVector3f::zero()); 00197 return PhysxManager::nxVec3_to_vec3(ptr()->getGlobalAxis()); 00198 } 00199 00200 //////////////////////////////////////////////////////////////////// 00201 // Function: PhysxJoint::set_breakable 00202 // Access: Published 00203 // Description: Sets the maximum force magnitude that the joint 00204 // is able to withstand without breaking. 00205 // 00206 // If the joint force rises above this threshold, the 00207 // joint breaks, and becomes disabled. 00208 // 00209 // There are two values, one for linear forces, and 00210 // one for angular forces. Both values are used 00211 // directly as a value for the maximum impulse 00212 // tolerated by the joint constraints. 00213 // 00214 // Both force values are NX_MAX_REAL by default. 00215 // This setting makes the joint unbreakable. The values 00216 // should always be nonnegative. 00217 // 00218 // The distinction between maxForce and maxTorque is 00219 // dependent on how the joint is implemented 00220 // internally, which may not be obvious. For example 00221 // what appears to be an angular degree of freedom may 00222 // be constrained indirectly by a linear constraint. 00223 // 00224 // So in most practical applications the user should 00225 // set both maxTorque and maxForce to low values. 00226 //////////////////////////////////////////////////////////////////// 00227 void PhysxJoint:: 00228 set_breakable(float maxForce, float maxTorque) { 00229 00230 nassertv(_error_type == ET_ok); 00231 ptr()->setBreakable(maxForce, maxTorque); 00232 } 00233 00234 //////////////////////////////////////////////////////////////////// 00235 // Function: PhysxJoint::set_use_acceleration_spring 00236 // Access: Published 00237 // Description: Switch between acceleration and force based spring. 00238 //////////////////////////////////////////////////////////////////// 00239 void PhysxJoint:: 00240 set_use_acceleration_spring(bool value) { 00241 00242 nassertv(_error_type == ET_ok); 00243 ptr()->setUseAccelerationSpring(value); 00244 } 00245 00246 //////////////////////////////////////////////////////////////////// 00247 // Function: PhysxJoint::get_use_acceleration_spring 00248 // Access: Published 00249 // Description: Checks whether acceleration spring is used. 00250 //////////////////////////////////////////////////////////////////// 00251 bool PhysxJoint:: 00252 get_use_acceleration_spring() const { 00253 00254 nassertr(_error_type == ET_ok, false); 00255 return ptr()->getUseAccelerationSpring(); 00256 } 00257 00258 //////////////////////////////////////////////////////////////////// 00259 // Function: PhysxJoint::set_solver_extrapolation_factor 00260 // Access: Published 00261 // Description: Sets the solver extrapolation factor. 00262 //////////////////////////////////////////////////////////////////// 00263 void PhysxJoint:: 00264 set_solver_extrapolation_factor(float factor) { 00265 00266 nassertv(_error_type == ET_ok); 00267 ptr()->setSolverExtrapolationFactor(factor); 00268 } 00269 00270 //////////////////////////////////////////////////////////////////// 00271 // Function: PhysxJoint::get_solver_extrapolation_factor 00272 // Access: Published 00273 // Description: Retrieves the solver extrapolation factor. 00274 //////////////////////////////////////////////////////////////////// 00275 float PhysxJoint:: 00276 get_solver_extrapolation_factor() const { 00277 00278 nassertr(_error_type == ET_ok, false); 00279 return ptr()->getSolverExtrapolationFactor(); 00280 } 00281 00282 //////////////////////////////////////////////////////////////////// 00283 // Function: PhysxJoint::set_limit_point 00284 // Access: Published 00285 // Description: Sets the limit point. 00286 // The point is specified in the global coordinate 00287 // frame. 00288 // 00289 // All types of joints may be limited with the same 00290 // system: You may elect a point attached to one of 00291 // the two actors to act as the limit point. You may 00292 // also specify several planes attached to the other 00293 // actor. 00294 // 00295 // The points and planes move together with the actor 00296 // they are attached to. 00297 // 00298 // The simulation then makes certain that the pair 00299 // of actors only move relative to each other so that 00300 // the limit point stays on the positive side of all 00301 // limit planes. 00302 // 00303 // The default limit point is (0,0,0) in the local 00304 // frame of actor2. Calling this deletes all existing 00305 // limit planes 00306 //////////////////////////////////////////////////////////////////// 00307 void PhysxJoint:: 00308 set_limit_point(const LPoint3f &pos, bool isOnActor2) { 00309 00310 nassertv(_error_type == ET_ok); 00311 ptr()->setLimitPoint(PhysxManager::point3_to_nxVec3(pos), isOnActor2); 00312 } 00313 00314 //////////////////////////////////////////////////////////////////// 00315 // Function: PhysxJoint::add_limit_plane 00316 // Access: Published 00317 // Description: Adds a limit plane. 00318 // The parameters are given in global coordinates. 00319 // The plane is affixed to the actor that does not 00320 // have the limit point. 00321 // 00322 // The normal of the plane points toward the positive 00323 // side of the plane, and thus toward the limit point. 00324 // If the normal points away from the limit point at 00325 // the time of this call, the method returns false 00326 // and the limit plane is ignored. 00327 //////////////////////////////////////////////////////////////////// 00328 void PhysxJoint:: 00329 add_limit_plane(const LVector3f &normal, const LPoint3f &pointInPlane, float restitution) { 00330 00331 nassertv(_error_type == ET_ok); 00332 ptr()->addLimitPlane(PhysxManager::vec3_to_nxVec3(normal), 00333 PhysxManager::point3_to_nxVec3(pointInPlane), 00334 restitution); 00335 } 00336 00337 //////////////////////////////////////////////////////////////////// 00338 // Function: PhysxJoint::purge_limit_planes 00339 // Access: Published 00340 // Description: Deletes all limit planes added to the joint. 00341 //////////////////////////////////////////////////////////////////// 00342 void PhysxJoint:: 00343 purge_limit_planes() { 00344 00345 nassertv(_error_type == ET_ok); 00346 ptr()->purgeLimitPlanes(); 00347 } 00348