2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2010 Erwin Coumans https://bulletphysics.org
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 B3_TYPED_CONSTRAINT_H
17 #define B3_TYPED_CONSTRAINT_H
19 #include "Bullet3Common/b3Scalar.h"
20 #include "b3SolverConstraint.h"
24 //Don't change any of the existing enum values, so add enum types at the end for serialization compatibility
25 enum b3TypedConstraintType
27 B3_POINT2POINT_CONSTRAINT_TYPE = 3,
28 B3_HINGE_CONSTRAINT_TYPE,
29 B3_CONETWIST_CONSTRAINT_TYPE,
30 B3_D6_CONSTRAINT_TYPE,
31 B3_SLIDER_CONSTRAINT_TYPE,
32 B3_CONTACT_CONSTRAINT_TYPE,
33 B3_D6_SPRING_CONSTRAINT_TYPE,
34 B3_GEAR_CONSTRAINT_TYPE,
35 B3_FIXED_CONSTRAINT_TYPE,
36 B3_MAX_CONSTRAINT_TYPE
39 enum b3ConstraintParams
41 B3_CONSTRAINT_ERP = 1,
42 B3_CONSTRAINT_STOP_ERP,
44 B3_CONSTRAINT_STOP_CFM
48 #define b3AssertConstrParams(_par) b3Assert(_par)
50 #define b3AssertConstrParams(_par)
53 B3_ATTRIBUTE_ALIGNED16(struct)
56 b3Vector3 m_appliedForceBodyA;
57 b3Vector3 m_appliedTorqueBodyA;
58 b3Vector3 m_appliedForceBodyB;
59 b3Vector3 m_appliedTorqueBodyB;
62 struct b3RigidBodyData;
64 ///TypedConstraint is the baseclass for Bullet constraints and vehicles
65 B3_ATTRIBUTE_ALIGNED16(class)
66 b3TypedConstraint : public b3TypedObject
68 int m_userConstraintType;
71 int m_userConstraintId;
72 void* m_userConstraintPtr;
75 b3Scalar m_breakingImpulseThreshold;
78 int m_overrideNumSolverIterations;
80 b3TypedConstraint& operator=(b3TypedConstraint& other)
90 b3Scalar m_appliedImpulse;
91 b3Scalar m_dbgDrawSize;
92 b3JointFeedback* m_jointFeedback;
94 ///internal method used by the constraint solver, don't use them directly
95 b3Scalar getMotorFactor(b3Scalar pos, b3Scalar lowLim, b3Scalar uppLim, b3Scalar vel, b3Scalar timeFact);
98 B3_DECLARE_ALIGNED_ALLOCATOR();
100 virtual ~b3TypedConstraint(){};
101 b3TypedConstraint(b3TypedConstraintType type, int bodyA, int bodyB);
103 struct b3ConstraintInfo1
105 int m_numConstraintRows, nub;
108 struct b3ConstraintInfo2
110 // integrator parameters: frames per second (1/stepsize), default error
111 // reduction parameter (0..1).
114 // for the first and second body, pointers to two (linear and angular)
115 // n*3 jacobian sub matrices, stored by rows. these matrices will have
116 // been initialized to 0 on entry. if the second body is zero then the
117 // J2xx pointers may be 0.
118 b3Scalar *m_J1linearAxis, *m_J1angularAxis, *m_J2linearAxis, *m_J2angularAxis;
120 // elements to jump from one row to the next in J's
123 // right hand sides of the equation J*v = c + cfm * lambda. cfm is the
124 // "constraint force mixing" vector. c is set to zero on entry, cfm is
125 // set to a constant value (typically very small or zero) value on entry.
126 b3Scalar *m_constraintError, *cfm;
128 // lo and hi limits for variables (set to -/+ infinity on entry).
129 b3Scalar *m_lowerLimit, *m_upperLimit;
131 // findex vector for variables. see the LCP solver interface for a
132 // description of what this does. this is set to -1 on entry.
133 // note that the returned indexes are relative to the first index of
136 // number of solver iterations
139 //damping of the velocity
143 int getOverrideNumSolverIterations() const
145 return m_overrideNumSolverIterations;
148 ///override the number of constraint solver iterations used to solve this constraint
149 ///-1 will use the default number of iterations, as specified in SolverInfo.m_numIterations
150 void setOverrideNumSolverIterations(int overideNumIterations)
152 m_overrideNumSolverIterations = overideNumIterations;
155 ///internal method used by the constraint solver, don't use them directly
156 virtual void setupSolverConstraint(b3ConstraintArray & ca, int solverBodyA, int solverBodyB, b3Scalar timeStep)
164 ///internal method used by the constraint solver, don't use them directly
165 virtual void getInfo1(b3ConstraintInfo1 * info, const b3RigidBodyData* bodies) = 0;
167 ///internal method used by the constraint solver, don't use them directly
168 virtual void getInfo2(b3ConstraintInfo2 * info, const b3RigidBodyData* bodies) = 0;
170 ///internal method used by the constraint solver, don't use them directly
171 void internalSetAppliedImpulse(b3Scalar appliedImpulse)
173 m_appliedImpulse = appliedImpulse;
175 ///internal method used by the constraint solver, don't use them directly
176 b3Scalar internalGetAppliedImpulse()
178 return m_appliedImpulse;
181 b3Scalar getBreakingImpulseThreshold() const
183 return m_breakingImpulseThreshold;
186 void setBreakingImpulseThreshold(b3Scalar threshold)
188 m_breakingImpulseThreshold = threshold;
191 bool isEnabled() const
196 void setEnabled(bool enabled)
198 m_isEnabled = enabled;
201 ///internal method used by the constraint solver, don't use them directly
202 virtual void solveConstraintObsolete(b3SolverBody& /*bodyA*/, b3SolverBody& /*bodyB*/, b3Scalar /*timeStep*/){};
204 int getRigidBodyA() const
208 int getRigidBodyB() const
222 int getUserConstraintType() const
224 return m_userConstraintType;
227 void setUserConstraintType(int userConstraintType)
229 m_userConstraintType = userConstraintType;
232 void setUserConstraintId(int uid)
234 m_userConstraintId = uid;
237 int getUserConstraintId() const
239 return m_userConstraintId;
242 void setUserConstraintPtr(void* ptr)
244 m_userConstraintPtr = ptr;
247 void* getUserConstraintPtr()
249 return m_userConstraintPtr;
252 void setJointFeedback(b3JointFeedback * jointFeedback)
254 m_jointFeedback = jointFeedback;
257 const b3JointFeedback* getJointFeedback() const
259 return m_jointFeedback;
262 b3JointFeedback* getJointFeedback()
264 return m_jointFeedback;
269 return m_userConstraintId;
272 bool needsFeedback() const
274 return m_needsFeedback;
277 ///enableFeedback will allow to read the applied linear and angular impulse
278 ///use getAppliedImpulse, getAppliedLinearImpulse and getAppliedAngularImpulse to read feedback information
279 void enableFeedback(bool needsFeedback)
281 m_needsFeedback = needsFeedback;
284 ///getAppliedImpulse is an estimated total applied impulse.
285 ///This feedback could be used to determine breaking constraints or playing sounds.
286 b3Scalar getAppliedImpulse() const
288 b3Assert(m_needsFeedback);
289 return m_appliedImpulse;
292 b3TypedConstraintType getConstraintType() const
294 return b3TypedConstraintType(m_objectType);
297 void setDbgDrawSize(b3Scalar dbgDrawSize)
299 m_dbgDrawSize = dbgDrawSize;
301 b3Scalar getDbgDrawSize()
303 return m_dbgDrawSize;
306 ///override the default global value of a parameter (such as ERP or CFM), optionally provide the axis (0..5).
307 ///If no axis is provided, it uses the default axis for this constraint.
308 virtual void setParam(int num, b3Scalar value, int axis = -1) = 0;
310 ///return the local value of parameter
311 virtual b3Scalar getParam(int num, int axis = -1) const = 0;
313 // virtual int calculateSerializeBufferSize() const;
315 ///fills the dataBuffer and returns the struct name (and 0 on failure)
316 //virtual const char* serialize(void* dataBuffer, b3Serializer* serializer) const;
319 // returns angle in range [-B3_2_PI, B3_2_PI], closest to one of the limits
320 // all arguments should be normalized angles (i.e. in range [-B3_PI, B3_PI])
321 B3_FORCE_INLINE b3Scalar b3AdjustAngleToLimits(b3Scalar angleInRadians, b3Scalar angleLowerLimitInRadians, b3Scalar angleUpperLimitInRadians)
323 if (angleLowerLimitInRadians >= angleUpperLimitInRadians)
325 return angleInRadians;
327 else if (angleInRadians < angleLowerLimitInRadians)
329 b3Scalar diffLo = b3Fabs(b3NormalizeAngle(angleLowerLimitInRadians - angleInRadians));
330 b3Scalar diffHi = b3Fabs(b3NormalizeAngle(angleUpperLimitInRadians - angleInRadians));
331 return (diffLo < diffHi) ? angleInRadians : (angleInRadians + B3_2_PI);
333 else if (angleInRadians > angleUpperLimitInRadians)
335 b3Scalar diffHi = b3Fabs(b3NormalizeAngle(angleInRadians - angleUpperLimitInRadians));
336 b3Scalar diffLo = b3Fabs(b3NormalizeAngle(angleInRadians - angleLowerLimitInRadians));
337 return (diffLo < diffHi) ? (angleInRadians - B3_2_PI) : angleInRadians;
341 return angleInRadians;
346 ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
347 struct b3TypedConstraintData
354 int m_userConstraintType;
355 int m_userConstraintId;
358 float m_appliedImpulse;
361 int m_disableCollisionsBetweenLinkedBodies;
362 int m_overrideNumSolverIterations;
364 float m_breakingImpulseThreshold;
371 /*B3_FORCE_INLINE int b3TypedConstraint::calculateSerializeBufferSize() const
373 return sizeof(b3TypedConstraintData);
393 /// Default constructor initializes limit as inactive, allowing free constraint movement
399 m_relaxationFactor(1.0f),
406 /// Sets all limit's parameters.
407 /// When low > high limit becomes inactive.
408 /// When high - low > 2PI limit is ineffective too becouse no angle can exceed the limit
409 void set(b3Scalar low, b3Scalar high, b3Scalar _softness = 0.9f, b3Scalar _biasFactor = 0.3f, b3Scalar _relaxationFactor = 1.0f);
411 /// Checks conastaint angle against limit. If limit is active and the angle violates the limit
412 /// correction is calculated.
413 void test(const b3Scalar angle);
415 /// Returns limit's softness
416 inline b3Scalar getSoftness() const
421 /// Returns limit's bias factor
422 inline b3Scalar getBiasFactor() const
427 /// Returns limit's relaxation factor
428 inline b3Scalar getRelaxationFactor() const
430 return m_relaxationFactor;
433 /// Returns correction value evaluated when test() was invoked
434 inline b3Scalar getCorrection() const
439 /// Returns sign value evaluated when test() was invoked
440 inline b3Scalar getSign() const
445 /// Gives half of the distance between min and max limit angle
446 inline b3Scalar getHalfRange() const
451 /// Returns true when the last test() invocation recognized limit violation
452 inline bool isLimit() const
457 /// Checks given angle against limit. If limit is active and angle doesn't fit it, the angle
458 /// returned is modified so it equals to the limit closest to given angle.
459 void fit(b3Scalar& angle) const;
461 /// Returns correction value multiplied by sign value
462 b3Scalar getError() const;
464 b3Scalar getLow() const;
466 b3Scalar getHigh() const;
469 #endif //B3_TYPED_CONSTRAINT_H