2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2010 Erwin Coumans http://continuousphysics.com/Bullet/
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
16 #ifndef BT_TYPED_CONSTRAINT_H
17 #define BT_TYPED_CONSTRAINT_H
20 #include "LinearMath/btScalar.h"
21 #include "btSolverConstraint.h"
25 //Don't change any of the existing enum values, so add enum types at the end for serialization compatibility
26 enum btTypedConstraintType
28 POINT2POINT_CONSTRAINT_TYPE=3,
29 HINGE_CONSTRAINT_TYPE,
30 CONETWIST_CONSTRAINT_TYPE,
32 SLIDER_CONSTRAINT_TYPE,
33 CONTACT_CONSTRAINT_TYPE,
34 D6_SPRING_CONSTRAINT_TYPE,
39 enum btConstraintParams
42 BT_CONSTRAINT_STOP_ERP,
44 BT_CONSTRAINT_STOP_CFM
48 #define btAssertConstrParams(_par) btAssert(_par)
50 #define btAssertConstrParams(_par)
54 ///TypedConstraint is the baseclass for Bullet constraints and vehicles
55 class btTypedConstraint : public btTypedObject
57 int m_userConstraintType;
61 int m_userConstraintId;
62 void* m_userConstraintPtr;
65 btScalar m_breakingImpulseThreshold;
68 int m_overrideNumSolverIterations;
71 btTypedConstraint& operator=(btTypedConstraint& other)
81 btScalar m_appliedImpulse;
82 btScalar m_dbgDrawSize;
84 ///internal method used by the constraint solver, don't use them directly
85 btScalar getMotorFactor(btScalar pos, btScalar lowLim, btScalar uppLim, btScalar vel, btScalar timeFact);
90 virtual ~btTypedConstraint() {};
91 btTypedConstraint(btTypedConstraintType type, btRigidBody& rbA);
92 btTypedConstraint(btTypedConstraintType type, btRigidBody& rbA,btRigidBody& rbB);
94 struct btConstraintInfo1 {
95 int m_numConstraintRows,nub;
98 static btRigidBody& getFixedBody();
100 struct btConstraintInfo2 {
101 // integrator parameters: frames per second (1/stepsize), default error
102 // reduction parameter (0..1).
105 // for the first and second body, pointers to two (linear and angular)
106 // n*3 jacobian sub matrices, stored by rows. these matrices will have
107 // been initialized to 0 on entry. if the second body is zero then the
108 // J2xx pointers may be 0.
109 btScalar *m_J1linearAxis,*m_J1angularAxis,*m_J2linearAxis,*m_J2angularAxis;
111 // elements to jump from one row to the next in J's
114 // right hand sides of the equation J*v = c + cfm * lambda. cfm is the
115 // "constraint force mixing" vector. c is set to zero on entry, cfm is
116 // set to a constant value (typically very small or zero) value on entry.
117 btScalar *m_constraintError,*cfm;
119 // lo and hi limits for variables (set to -/+ infinity on entry).
120 btScalar *m_lowerLimit,*m_upperLimit;
122 // findex vector for variables. see the LCP solver interface for a
123 // description of what this does. this is set to -1 on entry.
124 // note that the returned indexes are relative to the first index of
127 // number of solver iterations
130 //damping of the velocity
134 int getOverrideNumSolverIterations() const
136 return m_overrideNumSolverIterations;
139 ///override the number of constraint solver iterations used to solve this constraint
140 ///-1 will use the default number of iterations, as specified in SolverInfo.m_numIterations
141 void setOverrideNumSolverIterations(int overideNumIterations)
143 m_overrideNumSolverIterations = overideNumIterations;
146 ///internal method used by the constraint solver, don't use them directly
147 virtual void buildJacobian() {};
149 ///internal method used by the constraint solver, don't use them directly
150 virtual void setupSolverConstraint(btConstraintArray& ca, int solverBodyA,int solverBodyB, btScalar timeStep)
158 ///internal method used by the constraint solver, don't use them directly
159 virtual void getInfo1 (btConstraintInfo1* info)=0;
161 ///internal method used by the constraint solver, don't use them directly
162 virtual void getInfo2 (btConstraintInfo2* info)=0;
164 ///internal method used by the constraint solver, don't use them directly
165 void internalSetAppliedImpulse(btScalar appliedImpulse)
167 m_appliedImpulse = appliedImpulse;
169 ///internal method used by the constraint solver, don't use them directly
170 btScalar internalGetAppliedImpulse()
172 return m_appliedImpulse;
176 btScalar getBreakingImpulseThreshold() const
178 return m_breakingImpulseThreshold;
181 void setBreakingImpulseThreshold(btScalar threshold)
183 m_breakingImpulseThreshold = threshold;
186 bool isEnabled() const
191 void setEnabled(bool enabled)
197 ///internal method used by the constraint solver, don't use them directly
198 virtual void solveConstraintObsolete(btRigidBody& /*bodyA*/,btRigidBody& /*bodyB*/,btScalar /*timeStep*/) {};
201 const btRigidBody& getRigidBodyA() const
205 const btRigidBody& getRigidBodyB() const
210 btRigidBody& getRigidBodyA()
214 btRigidBody& getRigidBodyB()
219 int getUserConstraintType() const
221 return m_userConstraintType ;
224 void setUserConstraintType(int userConstraintType)
226 m_userConstraintType = userConstraintType;
229 void setUserConstraintId(int uid)
231 m_userConstraintId = uid;
234 int getUserConstraintId() const
236 return m_userConstraintId;
239 void setUserConstraintPtr(void* ptr)
241 m_userConstraintPtr = ptr;
244 void* getUserConstraintPtr()
246 return m_userConstraintPtr;
251 return m_userConstraintId;
254 bool needsFeedback() const
256 return m_needsFeedback;
259 ///enableFeedback will allow to read the applied linear and angular impulse
260 ///use getAppliedImpulse, getAppliedLinearImpulse and getAppliedAngularImpulse to read feedback information
261 void enableFeedback(bool needsFeedback)
263 m_needsFeedback = needsFeedback;
266 ///getAppliedImpulse is an estimated total applied impulse.
267 ///This feedback could be used to determine breaking constraints or playing sounds.
268 btScalar getAppliedImpulse() const
270 btAssert(m_needsFeedback);
271 return m_appliedImpulse;
274 btTypedConstraintType getConstraintType () const
276 return btTypedConstraintType(m_objectType);
279 void setDbgDrawSize(btScalar dbgDrawSize)
281 m_dbgDrawSize = dbgDrawSize;
283 btScalar getDbgDrawSize()
285 return m_dbgDrawSize;
288 ///override the default global value of a parameter (such as ERP or CFM), optionally provide the axis (0..5).
289 ///If no axis is provided, it uses the default axis for this constraint.
290 virtual void setParam(int num, btScalar value, int axis = -1) = 0;
292 ///return the local value of parameter
293 virtual btScalar getParam(int num, int axis = -1) const = 0;
295 virtual int calculateSerializeBufferSize() const;
297 ///fills the dataBuffer and returns the struct name (and 0 on failure)
298 virtual const char* serialize(void* dataBuffer, btSerializer* serializer) const;
302 // returns angle in range [-SIMD_2_PI, SIMD_2_PI], closest to one of the limits
303 // all arguments should be normalized angles (i.e. in range [-SIMD_PI, SIMD_PI])
304 SIMD_FORCE_INLINE btScalar btAdjustAngleToLimits(btScalar angleInRadians, btScalar angleLowerLimitInRadians, btScalar angleUpperLimitInRadians)
306 if(angleLowerLimitInRadians >= angleUpperLimitInRadians)
308 return angleInRadians;
310 else if(angleInRadians < angleLowerLimitInRadians)
312 btScalar diffLo = btFabs(btNormalizeAngle(angleLowerLimitInRadians - angleInRadians));
313 btScalar diffHi = btFabs(btNormalizeAngle(angleUpperLimitInRadians - angleInRadians));
314 return (diffLo < diffHi) ? angleInRadians : (angleInRadians + SIMD_2_PI);
316 else if(angleInRadians > angleUpperLimitInRadians)
318 btScalar diffHi = btFabs(btNormalizeAngle(angleInRadians - angleUpperLimitInRadians));
319 btScalar diffLo = btFabs(btNormalizeAngle(angleInRadians - angleLowerLimitInRadians));
320 return (diffLo < diffHi) ? (angleInRadians - SIMD_2_PI) : angleInRadians;
324 return angleInRadians;
328 ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
329 struct btTypedConstraintData
331 btRigidBodyData *m_rbA;
332 btRigidBodyData *m_rbB;
336 int m_userConstraintType;
337 int m_userConstraintId;
340 float m_appliedImpulse;
343 int m_disableCollisionsBetweenLinkedBodies;
344 int m_overrideNumSolverIterations;
346 float m_breakingImpulseThreshold;
351 SIMD_FORCE_INLINE int btTypedConstraint::calculateSerializeBufferSize() const
353 return sizeof(btTypedConstraintData);
374 /// Default constructor initializes limit as inactive, allowing free constraint movement
380 m_relaxationFactor(1.0f),
386 /// Sets all limit's parameters.
387 /// When low > high limit becomes inactive.
388 /// When high - low > 2PI limit is ineffective too becouse no angle can exceed the limit
389 void set(btScalar low, btScalar high, btScalar _softness = 0.9f, btScalar _biasFactor = 0.3f, btScalar _relaxationFactor = 1.0f);
391 /// Checks conastaint angle against limit. If limit is active and the angle violates the limit
392 /// correction is calculated.
393 void test(const btScalar angle);
395 /// Returns limit's softness
396 inline btScalar getSoftness() const
401 /// Returns limit's bias factor
402 inline btScalar getBiasFactor() const
407 /// Returns limit's relaxation factor
408 inline btScalar getRelaxationFactor() const
410 return m_relaxationFactor;
413 /// Returns correction value evaluated when test() was invoked
414 inline btScalar getCorrection() const
419 /// Returns sign value evaluated when test() was invoked
420 inline btScalar getSign() const
425 /// Gives half of the distance between min and max limit angle
426 inline btScalar getHalfRange() const
431 /// Returns true when the last test() invocation recognized limit violation
432 inline bool isLimit() const
437 /// Checks given angle against limit. If limit is active and angle doesn't fit it, the angle
438 /// returned is modified so it equals to the limit closest to given angle.
439 void fit(btScalar& angle) const;
441 /// Returns correction value multiplied by sign value
442 btScalar getError() const;
444 btScalar getLow() const;
446 btScalar getHigh() const;
452 #endif //BT_TYPED_CONSTRAINT_H