[1963] | 1 | /* |
---|
| 2 | Bullet Continuous Collision Detection and Physics Library |
---|
[8351] | 3 | Copyright (c) 2003-2010 Erwin Coumans http://continuousphysics.com/Bullet/ |
---|
[1963] | 4 | |
---|
| 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: |
---|
| 10 | |
---|
| 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. |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | #ifndef TYPED_CONSTRAINT_H |
---|
| 17 | #define TYPED_CONSTRAINT_H |
---|
| 18 | |
---|
| 19 | class btRigidBody; |
---|
| 20 | #include "LinearMath/btScalar.h" |
---|
[2882] | 21 | #include "btSolverConstraint.h" |
---|
[8351] | 22 | #include "BulletCollision/NarrowPhaseCollision/btPersistentManifold.h" |
---|
[1963] | 23 | |
---|
[8351] | 24 | class btSerializer; |
---|
[2882] | 25 | |
---|
[1963] | 26 | enum btTypedConstraintType |
---|
| 27 | { |
---|
[8351] | 28 | POINT2POINT_CONSTRAINT_TYPE=MAX_CONTACT_MANIFOLD_TYPE+1, |
---|
[1963] | 29 | HINGE_CONSTRAINT_TYPE, |
---|
| 30 | CONETWIST_CONSTRAINT_TYPE, |
---|
| 31 | D6_CONSTRAINT_TYPE, |
---|
[8351] | 32 | SLIDER_CONSTRAINT_TYPE, |
---|
| 33 | CONTACT_CONSTRAINT_TYPE |
---|
[1963] | 34 | }; |
---|
| 35 | |
---|
[8351] | 36 | |
---|
| 37 | enum btConstraintParams |
---|
| 38 | { |
---|
| 39 | BT_CONSTRAINT_ERP=1, |
---|
| 40 | BT_CONSTRAINT_STOP_ERP, |
---|
| 41 | BT_CONSTRAINT_CFM, |
---|
| 42 | BT_CONSTRAINT_STOP_CFM |
---|
| 43 | }; |
---|
| 44 | |
---|
| 45 | #if 1 |
---|
| 46 | #define btAssertConstrParams(_par) btAssert(_par) |
---|
| 47 | #else |
---|
| 48 | #define btAssertConstrParams(_par) |
---|
| 49 | #endif |
---|
| 50 | |
---|
| 51 | |
---|
[1963] | 52 | ///TypedConstraint is the baseclass for Bullet constraints and vehicles |
---|
[8351] | 53 | class btTypedConstraint : public btTypedObject |
---|
[1963] | 54 | { |
---|
| 55 | int m_userConstraintType; |
---|
| 56 | |
---|
[8351] | 57 | union |
---|
| 58 | { |
---|
| 59 | int m_userConstraintId; |
---|
| 60 | void* m_userConstraintPtr; |
---|
| 61 | }; |
---|
[1963] | 62 | |
---|
[8351] | 63 | bool m_needsFeedback; |
---|
| 64 | |
---|
[1963] | 65 | btTypedConstraint& operator=(btTypedConstraint& other) |
---|
| 66 | { |
---|
| 67 | btAssert(0); |
---|
| 68 | (void) other; |
---|
| 69 | return *this; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | protected: |
---|
| 73 | btRigidBody& m_rbA; |
---|
| 74 | btRigidBody& m_rbB; |
---|
| 75 | btScalar m_appliedImpulse; |
---|
[2882] | 76 | btScalar m_dbgDrawSize; |
---|
[1963] | 77 | |
---|
[8351] | 78 | ///internal method used by the constraint solver, don't use them directly |
---|
| 79 | btScalar getMotorFactor(btScalar pos, btScalar lowLim, btScalar uppLim, btScalar vel, btScalar timeFact); |
---|
| 80 | |
---|
| 81 | static btRigidBody& getFixedBody(); |
---|
[1963] | 82 | |
---|
| 83 | public: |
---|
| 84 | |
---|
| 85 | virtual ~btTypedConstraint() {}; |
---|
| 86 | btTypedConstraint(btTypedConstraintType type, btRigidBody& rbA); |
---|
| 87 | btTypedConstraint(btTypedConstraintType type, btRigidBody& rbA,btRigidBody& rbB); |
---|
| 88 | |
---|
[2882] | 89 | struct btConstraintInfo1 { |
---|
| 90 | int m_numConstraintRows,nub; |
---|
| 91 | }; |
---|
| 92 | |
---|
| 93 | struct btConstraintInfo2 { |
---|
| 94 | // integrator parameters: frames per second (1/stepsize), default error |
---|
| 95 | // reduction parameter (0..1). |
---|
| 96 | btScalar fps,erp; |
---|
| 97 | |
---|
| 98 | // for the first and second body, pointers to two (linear and angular) |
---|
| 99 | // n*3 jacobian sub matrices, stored by rows. these matrices will have |
---|
| 100 | // been initialized to 0 on entry. if the second body is zero then the |
---|
| 101 | // J2xx pointers may be 0. |
---|
| 102 | btScalar *m_J1linearAxis,*m_J1angularAxis,*m_J2linearAxis,*m_J2angularAxis; |
---|
| 103 | |
---|
| 104 | // elements to jump from one row to the next in J's |
---|
| 105 | int rowskip; |
---|
| 106 | |
---|
| 107 | // right hand sides of the equation J*v = c + cfm * lambda. cfm is the |
---|
| 108 | // "constraint force mixing" vector. c is set to zero on entry, cfm is |
---|
| 109 | // set to a constant value (typically very small or zero) value on entry. |
---|
| 110 | btScalar *m_constraintError,*cfm; |
---|
| 111 | |
---|
| 112 | // lo and hi limits for variables (set to -/+ infinity on entry). |
---|
| 113 | btScalar *m_lowerLimit,*m_upperLimit; |
---|
| 114 | |
---|
| 115 | // findex vector for variables. see the LCP solver interface for a |
---|
| 116 | // description of what this does. this is set to -1 on entry. |
---|
| 117 | // note that the returned indexes are relative to the first index of |
---|
| 118 | // the constraint. |
---|
| 119 | int *findex; |
---|
[8351] | 120 | // number of solver iterations |
---|
| 121 | int m_numIterations; |
---|
| 122 | |
---|
| 123 | //damping of the velocity |
---|
| 124 | btScalar m_damping; |
---|
[2882] | 125 | }; |
---|
| 126 | |
---|
[8351] | 127 | ///internal method used by the constraint solver, don't use them directly |
---|
| 128 | virtual void buildJacobian() {}; |
---|
[2882] | 129 | |
---|
[8351] | 130 | ///internal method used by the constraint solver, don't use them directly |
---|
[2882] | 131 | virtual void setupSolverConstraint(btConstraintArray& ca, int solverBodyA,int solverBodyB, btScalar timeStep) |
---|
| 132 | { |
---|
[8351] | 133 | (void)ca; |
---|
| 134 | (void)solverBodyA; |
---|
| 135 | (void)solverBodyB; |
---|
| 136 | (void)timeStep; |
---|
[2882] | 137 | } |
---|
[8351] | 138 | |
---|
| 139 | ///internal method used by the constraint solver, don't use them directly |
---|
[2882] | 140 | virtual void getInfo1 (btConstraintInfo1* info)=0; |
---|
[1963] | 141 | |
---|
[8351] | 142 | ///internal method used by the constraint solver, don't use them directly |
---|
[2882] | 143 | virtual void getInfo2 (btConstraintInfo2* info)=0; |
---|
| 144 | |
---|
[8351] | 145 | ///internal method used by the constraint solver, don't use them directly |
---|
| 146 | void internalSetAppliedImpulse(btScalar appliedImpulse) |
---|
| 147 | { |
---|
| 148 | m_appliedImpulse = appliedImpulse; |
---|
| 149 | } |
---|
| 150 | ///internal method used by the constraint solver, don't use them directly |
---|
| 151 | btScalar internalGetAppliedImpulse() |
---|
| 152 | { |
---|
| 153 | return m_appliedImpulse; |
---|
| 154 | } |
---|
[2882] | 155 | |
---|
[8351] | 156 | ///internal method used by the constraint solver, don't use them directly |
---|
| 157 | virtual void solveConstraintObsolete(btRigidBody& /*bodyA*/,btRigidBody& /*bodyB*/,btScalar /*timeStep*/) {}; |
---|
| 158 | |
---|
[2882] | 159 | |
---|
[1963] | 160 | const btRigidBody& getRigidBodyA() const |
---|
| 161 | { |
---|
| 162 | return m_rbA; |
---|
| 163 | } |
---|
| 164 | const btRigidBody& getRigidBodyB() const |
---|
| 165 | { |
---|
| 166 | return m_rbB; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | btRigidBody& getRigidBodyA() |
---|
| 170 | { |
---|
| 171 | return m_rbA; |
---|
| 172 | } |
---|
| 173 | btRigidBody& getRigidBodyB() |
---|
| 174 | { |
---|
| 175 | return m_rbB; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | int getUserConstraintType() const |
---|
| 179 | { |
---|
| 180 | return m_userConstraintType ; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | void setUserConstraintType(int userConstraintType) |
---|
| 184 | { |
---|
| 185 | m_userConstraintType = userConstraintType; |
---|
| 186 | }; |
---|
| 187 | |
---|
| 188 | void setUserConstraintId(int uid) |
---|
| 189 | { |
---|
| 190 | m_userConstraintId = uid; |
---|
| 191 | } |
---|
[2882] | 192 | |
---|
[1963] | 193 | int getUserConstraintId() const |
---|
| 194 | { |
---|
| 195 | return m_userConstraintId; |
---|
| 196 | } |
---|
| 197 | |
---|
[8351] | 198 | void setUserConstraintPtr(void* ptr) |
---|
| 199 | { |
---|
| 200 | m_userConstraintPtr = ptr; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | void* getUserConstraintPtr() |
---|
| 204 | { |
---|
| 205 | return m_userConstraintPtr; |
---|
| 206 | } |
---|
| 207 | |
---|
[1963] | 208 | int getUid() const |
---|
| 209 | { |
---|
| 210 | return m_userConstraintId; |
---|
| 211 | } |
---|
| 212 | |
---|
[8351] | 213 | bool needsFeedback() const |
---|
| 214 | { |
---|
| 215 | return m_needsFeedback; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | ///enableFeedback will allow to read the applied linear and angular impulse |
---|
| 219 | ///use getAppliedImpulse, getAppliedLinearImpulse and getAppliedAngularImpulse to read feedback information |
---|
| 220 | void enableFeedback(bool needsFeedback) |
---|
| 221 | { |
---|
| 222 | m_needsFeedback = needsFeedback; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | ///getAppliedImpulse is an estimated total applied impulse. |
---|
| 226 | ///This feedback could be used to determine breaking constraints or playing sounds. |
---|
[1963] | 227 | btScalar getAppliedImpulse() const |
---|
| 228 | { |
---|
[8351] | 229 | btAssert(m_needsFeedback); |
---|
[1963] | 230 | return m_appliedImpulse; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | btTypedConstraintType getConstraintType () const |
---|
| 234 | { |
---|
[8351] | 235 | return btTypedConstraintType(m_objectType); |
---|
[1963] | 236 | } |
---|
[2882] | 237 | |
---|
| 238 | void setDbgDrawSize(btScalar dbgDrawSize) |
---|
| 239 | { |
---|
| 240 | m_dbgDrawSize = dbgDrawSize; |
---|
| 241 | } |
---|
| 242 | btScalar getDbgDrawSize() |
---|
| 243 | { |
---|
| 244 | return m_dbgDrawSize; |
---|
| 245 | } |
---|
[8351] | 246 | |
---|
| 247 | ///override the default global value of a parameter (such as ERP or CFM), optionally provide the axis (0..5). |
---|
| 248 | ///If no axis is provided, it uses the default axis for this constraint. |
---|
| 249 | virtual void setParam(int num, btScalar value, int axis = -1) = 0; |
---|
| 250 | |
---|
| 251 | ///return the local value of parameter |
---|
| 252 | virtual btScalar getParam(int num, int axis = -1) const = 0; |
---|
[2882] | 253 | |
---|
[8351] | 254 | virtual int calculateSerializeBufferSize() const; |
---|
| 255 | |
---|
| 256 | ///fills the dataBuffer and returns the struct name (and 0 on failure) |
---|
| 257 | virtual const char* serialize(void* dataBuffer, btSerializer* serializer) const; |
---|
| 258 | |
---|
[1963] | 259 | }; |
---|
| 260 | |
---|
[8351] | 261 | // returns angle in range [-SIMD_2_PI, SIMD_2_PI], closest to one of the limits |
---|
| 262 | // all arguments should be normalized angles (i.e. in range [-SIMD_PI, SIMD_PI]) |
---|
| 263 | SIMD_FORCE_INLINE btScalar btAdjustAngleToLimits(btScalar angleInRadians, btScalar angleLowerLimitInRadians, btScalar angleUpperLimitInRadians) |
---|
| 264 | { |
---|
| 265 | if(angleLowerLimitInRadians >= angleUpperLimitInRadians) |
---|
| 266 | { |
---|
| 267 | return angleInRadians; |
---|
| 268 | } |
---|
| 269 | else if(angleInRadians < angleLowerLimitInRadians) |
---|
| 270 | { |
---|
| 271 | btScalar diffLo = btFabs(btNormalizeAngle(angleLowerLimitInRadians - angleInRadians)); |
---|
| 272 | btScalar diffHi = btFabs(btNormalizeAngle(angleUpperLimitInRadians - angleInRadians)); |
---|
| 273 | return (diffLo < diffHi) ? angleInRadians : (angleInRadians + SIMD_2_PI); |
---|
| 274 | } |
---|
| 275 | else if(angleInRadians > angleUpperLimitInRadians) |
---|
| 276 | { |
---|
| 277 | btScalar diffHi = btFabs(btNormalizeAngle(angleInRadians - angleUpperLimitInRadians)); |
---|
| 278 | btScalar diffLo = btFabs(btNormalizeAngle(angleInRadians - angleLowerLimitInRadians)); |
---|
| 279 | return (diffLo < diffHi) ? (angleInRadians - SIMD_2_PI) : angleInRadians; |
---|
| 280 | } |
---|
| 281 | else |
---|
| 282 | { |
---|
| 283 | return angleInRadians; |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 |
---|
| 288 | struct btTypedConstraintData |
---|
| 289 | { |
---|
| 290 | btRigidBodyData *m_rbA; |
---|
| 291 | btRigidBodyData *m_rbB; |
---|
| 292 | char *m_name; |
---|
| 293 | |
---|
| 294 | int m_objectType; |
---|
| 295 | int m_userConstraintType; |
---|
| 296 | int m_userConstraintId; |
---|
| 297 | int m_needsFeedback; |
---|
| 298 | |
---|
| 299 | float m_appliedImpulse; |
---|
| 300 | float m_dbgDrawSize; |
---|
| 301 | |
---|
| 302 | int m_disableCollisionsBetweenLinkedBodies; |
---|
| 303 | char m_pad4[4]; |
---|
| 304 | |
---|
| 305 | }; |
---|
| 306 | |
---|
| 307 | SIMD_FORCE_INLINE int btTypedConstraint::calculateSerializeBufferSize() const |
---|
| 308 | { |
---|
| 309 | return sizeof(btTypedConstraintData); |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | |
---|
| 313 | |
---|
| 314 | |
---|
[1963] | 315 | #endif //TYPED_CONSTRAINT_H |
---|