Panda3D
 All Classes Functions Variables Enumerations
physxJoint.cxx
1 // Filename: physxJoint.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 "physxJoint.h"
16 #include "physxManager.h"
17 #include "physxActor.h"
18 #include "physxScene.h"
19 #include "physxCylindricalJoint.h"
20 #include "physxDistanceJoint.h"
21 #include "physxFixedJoint.h"
22 #include "physxPointInPlaneJoint.h"
23 #include "physxPointOnLineJoint.h"
24 #include "physxPrismaticJoint.h"
25 #include "physxPulleyJoint.h"
26 #include "physxRevoluteJoint.h"
27 #include "physxSphericalJoint.h"
28 #include "physxD6Joint.h"
29 
30 TypeHandle PhysxJoint::_type_handle;
31 
32 ////////////////////////////////////////////////////////////////////
33 // Function: PhysxJoint::release
34 // Access: Published
35 // Description:
36 ////////////////////////////////////////////////////////////////////
37 void PhysxJoint::
38 release() {
39 
40  nassertv(_error_type == ET_ok);
41 
42  unlink();
43  ptr()->getScene().releaseJoint(*ptr());
44 }
45 
46 ////////////////////////////////////////////////////////////////////
47 // Function: PhysxJoint::factory
48 // Access: Public
49 // Description:
50 ////////////////////////////////////////////////////////////////////
51 PhysxJoint *PhysxJoint::
52 factory(NxJointType shapeType) {
53 
54  switch (shapeType) {
55 
56  case NX_JOINT_PRISMATIC:
57  return new PhysxPrismaticJoint();
58 
59  case NX_JOINT_REVOLUTE:
60  return new PhysxRevoluteJoint();
61 
62  case NX_JOINT_CYLINDRICAL:
63  return new PhysxCylindricalJoint();
64 
65  case NX_JOINT_SPHERICAL:
66  return new PhysxSphericalJoint();
67 
68  case NX_JOINT_POINT_ON_LINE:
69  return new PhysxPointOnLineJoint();
70 
71  case NX_JOINT_POINT_IN_PLANE:
72  return new PhysxPointInPlaneJoint();
73 
74  case NX_JOINT_DISTANCE:
75  return new PhysxDistanceJoint();
76 
77  case NX_JOINT_PULLEY:
78  return new PhysxPulleyJoint();
79 
80  case NX_JOINT_FIXED:
81  return new PhysxFixedJoint();
82 
83  case NX_JOINT_D6:
84  return new PhysxD6Joint();
85  }
86 
87  physx_cat.error() << "Unknown joint type.\n";
88  return NULL;
89 }
90 
91 ////////////////////////////////////////////////////////////////////
92 // Function: PhysxJoint::set_name
93 // Access: Published
94 // Description: Sets a name string for this object. The name can
95 // be retrieved again with get_name().
96 // This is for debugging and is not used by the
97 // physics engine.
98 ////////////////////////////////////////////////////////////////////
99 void PhysxJoint::
100 set_name(const char *name) {
101 
102  nassertv(_error_type == ET_ok);
103 
104  _name = name ? name : "";
105  ptr()->setName(_name.c_str());
106 }
107 
108 ////////////////////////////////////////////////////////////////////
109 // Function: PhysxJoint::get_name
110 // Access: Published
111 // Description: Returns the name string.
112 ////////////////////////////////////////////////////////////////////
113 const char *PhysxJoint::
114 get_name() const {
115 
116  nassertr(_error_type == ET_ok, "");
117  return ptr()->getName();
118 }
119 
120 ////////////////////////////////////////////////////////////////////
121 // Function: PhysxJoint::get_actor
122 // Access: Published
123 // Description: Retrieves the actor which this joint is associated
124 // with.
125 ////////////////////////////////////////////////////////////////////
127 get_actor(unsigned int idx) const {
128 
129  nassertr_always(idx < 2, NULL);
130  nassertr(_error_type == ET_ok, NULL);
131 
132  NxActor *actorPtr[2];
133  ptr()->getActors(&actorPtr[0], &actorPtr[1]);
134  return (PhysxActor *)(actorPtr[idx]->userData);
135 }
136 
137 ////////////////////////////////////////////////////////////////////
138 // Function: PhysxJoint::get_scene
139 // Access: Published
140 // Description: Retrieves the scene which this joint is associated
141 // with.
142 ////////////////////////////////////////////////////////////////////
144 get_scene() const {
145 
146  nassertr(_error_type == ET_ok, NULL);
147  return (PhysxScene *)(ptr()->getScene().userData);
148 }
149 
150 ////////////////////////////////////////////////////////////////////
151 // Function: PhysxJoint::set_global_anchor
152 // Access: Published
153 // Description: Sets the point where the two actors are attached,
154 // specified in global coordinates.
155 ////////////////////////////////////////////////////////////////////
156 void PhysxJoint::
157 set_global_anchor(const LPoint3f &anchor) {
158 
159  nassertv(_error_type == ET_ok);
160  ptr()->setGlobalAnchor(PhysxManager::point3_to_nxVec3(anchor));
161 }
162 
163 ////////////////////////////////////////////////////////////////////
164 // Function: PhysxJoint::get_global_anchor
165 // Access: Published
166 // Description: Retrieves the joint anchor.
167 ////////////////////////////////////////////////////////////////////
170 
171  nassertr(_error_type == ET_ok, LPoint3f::zero());
172  return PhysxManager::nxVec3_to_point3(ptr()->getGlobalAnchor());
173 }
174 
175 ////////////////////////////////////////////////////////////////////
176 // Function: PhysxJoint::set_global_axis
177 // Access: Published
178 // Description: Sets the direction of the joint's primary axis,
179 // specified in global coordinates.
180 ////////////////////////////////////////////////////////////////////
181 void PhysxJoint::
183 
184  nassertv(_error_type == ET_ok);
185  ptr()->setGlobalAxis(PhysxManager::vec3_to_nxVec3(axis));
186 }
187 
188 ////////////////////////////////////////////////////////////////////
189 // Function: PhysxJoint::get_global_axis
190 // Access: Published
191 // Description: Retrieves the joint axis.
192 ////////////////////////////////////////////////////////////////////
195 
196  nassertr(_error_type == ET_ok, LVector3f::zero());
197  return PhysxManager::nxVec3_to_vec3(ptr()->getGlobalAxis());
198 }
199 
200 ////////////////////////////////////////////////////////////////////
201 // Function: PhysxJoint::set_breakable
202 // Access: Published
203 // Description: Sets the maximum force magnitude that the joint
204 // is able to withstand without breaking.
205 //
206 // If the joint force rises above this threshold, the
207 // joint breaks, and becomes disabled.
208 //
209 // There are two values, one for linear forces, and
210 // one for angular forces. Both values are used
211 // directly as a value for the maximum impulse
212 // tolerated by the joint constraints.
213 //
214 // Both force values are NX_MAX_REAL by default.
215 // This setting makes the joint unbreakable. The values
216 // should always be nonnegative.
217 //
218 // The distinction between maxForce and maxTorque is
219 // dependent on how the joint is implemented
220 // internally, which may not be obvious. For example
221 // what appears to be an angular degree of freedom may
222 // be constrained indirectly by a linear constraint.
223 //
224 // So in most practical applications the user should
225 // set both maxTorque and maxForce to low values.
226 ////////////////////////////////////////////////////////////////////
227 void PhysxJoint::
228 set_breakable(float maxForce, float maxTorque) {
229 
230  nassertv(_error_type == ET_ok);
231  ptr()->setBreakable(maxForce, maxTorque);
232 }
233 
234 ////////////////////////////////////////////////////////////////////
235 // Function: PhysxJoint::set_use_acceleration_spring
236 // Access: Published
237 // Description: Switch between acceleration and force based spring.
238 ////////////////////////////////////////////////////////////////////
239 void PhysxJoint::
241 
242  nassertv(_error_type == ET_ok);
243  ptr()->setUseAccelerationSpring(value);
244 }
245 
246 ////////////////////////////////////////////////////////////////////
247 // Function: PhysxJoint::get_use_acceleration_spring
248 // Access: Published
249 // Description: Checks whether acceleration spring is used.
250 ////////////////////////////////////////////////////////////////////
251 bool PhysxJoint::
253 
254  nassertr(_error_type == ET_ok, false);
255  return ptr()->getUseAccelerationSpring();
256 }
257 
258 ////////////////////////////////////////////////////////////////////
259 // Function: PhysxJoint::set_solver_extrapolation_factor
260 // Access: Published
261 // Description: Sets the solver extrapolation factor.
262 ////////////////////////////////////////////////////////////////////
263 void PhysxJoint::
265 
266  nassertv(_error_type == ET_ok);
267  ptr()->setSolverExtrapolationFactor(factor);
268 }
269 
270 ////////////////////////////////////////////////////////////////////
271 // Function: PhysxJoint::get_solver_extrapolation_factor
272 // Access: Published
273 // Description: Retrieves the solver extrapolation factor.
274 ////////////////////////////////////////////////////////////////////
275 float PhysxJoint::
277 
278  nassertr(_error_type == ET_ok, false);
279  return ptr()->getSolverExtrapolationFactor();
280 }
281 
282 ////////////////////////////////////////////////////////////////////
283 // Function: PhysxJoint::set_limit_point
284 // Access: Published
285 // Description: Sets the limit point.
286 // The point is specified in the global coordinate
287 // frame.
288 //
289 // All types of joints may be limited with the same
290 // system: You may elect a point attached to one of
291 // the two actors to act as the limit point. You may
292 // also specify several planes attached to the other
293 // actor.
294 //
295 // The points and planes move together with the actor
296 // they are attached to.
297 //
298 // The simulation then makes certain that the pair
299 // of actors only move relative to each other so that
300 // the limit point stays on the positive side of all
301 // limit planes.
302 //
303 // The default limit point is (0,0,0) in the local
304 // frame of actor2. Calling this deletes all existing
305 // limit planes
306 ////////////////////////////////////////////////////////////////////
307 void PhysxJoint::
308 set_limit_point(const LPoint3f &pos, bool isOnActor2) {
309 
310  nassertv(_error_type == ET_ok);
311  ptr()->setLimitPoint(PhysxManager::point3_to_nxVec3(pos), isOnActor2);
312 }
313 
314 ////////////////////////////////////////////////////////////////////
315 // Function: PhysxJoint::add_limit_plane
316 // Access: Published
317 // Description: Adds a limit plane.
318 // The parameters are given in global coordinates.
319 // The plane is affixed to the actor that does not
320 // have the limit point.
321 //
322 // The normal of the plane points toward the positive
323 // side of the plane, and thus toward the limit point.
324 // If the normal points away from the limit point at
325 // the time of this call, the method returns false
326 // and the limit plane is ignored.
327 ////////////////////////////////////////////////////////////////////
328 void PhysxJoint::
329 add_limit_plane(const LVector3f &normal, const LPoint3f &pointInPlane, float restitution) {
330 
331  nassertv(_error_type == ET_ok);
332  ptr()->addLimitPlane(PhysxManager::vec3_to_nxVec3(normal),
333  PhysxManager::point3_to_nxVec3(pointInPlane),
334  restitution);
335 }
336 
337 ////////////////////////////////////////////////////////////////////
338 // Function: PhysxJoint::purge_limit_planes
339 // Access: Published
340 // Description: Deletes all limit planes added to the joint.
341 ////////////////////////////////////////////////////////////////////
342 void PhysxJoint::
344 
345  nassertv(_error_type == ET_ok);
346  ptr()->purgeLimitPlanes();
347 }
348 
A point on line joint constrains a point on one body to only move along a line attached to another bo...
Cylindrical Joints permit relative translational movement between two bodies along an axis...
void set_use_acceleration_spring(bool value)
Switch between acceleration and force based spring.
Definition: physxJoint.cxx:240
A fixed joint permits no relative movement between two bodies.
static const LPoint3f & zero()
Returns a zero-length point.
Definition: lpoint3.h:258
A pulley joint simulates a rope between two objects passing over two pulleys.
static NxVec3 point3_to_nxVec3(const LPoint3f &p)
Converts from LPoint3f to NxVec3.
Definition: physxManager.I:77
Abstract base class for the different types of joints.
Definition: physxJoint.h:35
static const LVector3f & zero()
Returns a zero-length vector.
Definition: lvector3.h:269
A distance joint maintains a certain distance between two points on two actors.
float get_solver_extrapolation_factor() const
Retrieves the solver extrapolation factor.
Definition: physxJoint.cxx:276
void set_limit_point(const LPoint3f &pos, bool isOnActor2=true)
Sets the limit point.
Definition: physxJoint.cxx:308
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:100
PhysxActor * get_actor(unsigned int idx) const
Retrieves the actor which this joint is associated with.
Definition: physxJoint.cxx:127
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
A scene is a collection of bodies, constraints, and effectors which can interact. ...
Definition: physxScene.h:73
void set_breakable(float maxForce, float maxTorque)
Sets the maximum force magnitude that the joint is able to withstand without breaking.
Definition: physxJoint.cxx:228
LVector3f get_global_axis() const
Retrieves the joint axis.
Definition: physxJoint.cxx:194
bool get_use_acceleration_spring() const
Checks whether acceleration spring is used.
Definition: physxJoint.cxx:252
A D6 joint is a general constraint between two actors.
Definition: physxD6Joint.h:33
void set_global_axis(const LVector3f &axis)
Sets the direction of the joint&#39;s primary axis, specified in global coordinates.
Definition: physxJoint.cxx:182
static NxVec3 vec3_to_nxVec3(const LVector3f &v)
Converts from LVector3f to NxVec3.
Definition: physxManager.I:33
static LVector3f nxVec3_to_vec3(const NxVec3 &v)
Converts from NxVec3 to LVector3f.
Definition: physxManager.I:44
LPoint3f get_global_anchor() const
Retrieves the joint anchor.
Definition: physxJoint.cxx:169
Actors are the main simulation objects.
Definition: physxActor.h:48
void purge_limit_planes()
Deletes all limit planes added to the joint.
Definition: physxJoint.cxx:343
A prismatic joint permits relative translational movement between two bodies along an axis...
static LPoint3f nxVec3_to_point3(const NxVec3 &p)
Converts from NxVec3 to LPoint3f.
Definition: physxManager.I:88
PhysxScene * get_scene() const
Retrieves the scene which this joint is associated with.
Definition: physxJoint.cxx:144
void set_name(const char *name)
Sets a name string for this object.
Definition: physxJoint.cxx:100
A point in plane joint constrains a point on one body to only move inside a plane attached to another...
const char * get_name() const
Returns the name string.
Definition: physxJoint.cxx:114
A sphere joint constrains two points on two bodies to coincide.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
void set_global_anchor(const LPoint3f &anchor)
Sets the point where the two actors are attached, specified in global coordinates.
Definition: physxJoint.cxx:157
void add_limit_plane(const LVector3f &normal, const LPoint3f &pointInPlane, float restitution=0.0f)
Adds a limit plane.
Definition: physxJoint.cxx:329
A joint which behaves in a similar way to a hinge or axel.
void set_solver_extrapolation_factor(float factor)
Sets the solver extrapolation factor.
Definition: physxJoint.cxx:264