1 | /* |
---|
2 | Bullet Continuous Collision Detection and Physics Library |
---|
3 | Copyright (c) 2003-2010 Erwin Coumans http://continuousphysics.com/Bullet/ |
---|
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" |
---|
21 | #include "btSolverConstraint.h" |
---|
22 | #include "BulletCollision/NarrowPhaseCollision/btPersistentManifold.h" |
---|
23 | |
---|
24 | class btSerializer; |
---|
25 | |
---|
26 | enum btTypedConstraintType |
---|
27 | { |
---|
28 | POINT2POINT_CONSTRAINT_TYPE=MAX_CONTACT_MANIFOLD_TYPE+1, |
---|
29 | HINGE_CONSTRAINT_TYPE, |
---|
30 | CONETWIST_CONSTRAINT_TYPE, |
---|
31 | D6_CONSTRAINT_TYPE, |
---|
32 | SLIDER_CONSTRAINT_TYPE, |
---|
33 | CONTACT_CONSTRAINT_TYPE |
---|
34 | }; |
---|
35 | |
---|
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 | |
---|
52 | ///TypedConstraint is the baseclass for Bullet constraints and vehicles |
---|
53 | class btTypedConstraint : public btTypedObject |
---|
54 | { |
---|
55 | int m_userConstraintType; |
---|
56 | |
---|
57 | union |
---|
58 | { |
---|
59 | int m_userConstraintId; |
---|
60 | void* m_userConstraintPtr; |
---|
61 | }; |
---|
62 | |
---|
63 | bool m_needsFeedback; |
---|
64 | |
---|
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; |
---|
76 | btScalar m_dbgDrawSize; |
---|
77 | |
---|
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(); |
---|
82 | |
---|
83 | public: |
---|
84 | |
---|
85 | virtual ~btTypedConstraint() {}; |
---|
86 | btTypedConstraint(btTypedConstraintType type, btRigidBody& rbA); |
---|
87 | btTypedConstraint(btTypedConstraintType type, btRigidBody& rbA,btRigidBody& rbB); |
---|
88 | |
---|
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; |
---|
120 | // number of solver iterations |
---|
121 | int m_numIterations; |
---|
122 | |
---|
123 | //damping of the velocity |
---|
124 | btScalar m_damping; |
---|
125 | }; |
---|
126 | |
---|
127 | ///internal method used by the constraint solver, don't use them directly |
---|
128 | virtual void buildJacobian() {}; |
---|
129 | |
---|
130 | ///internal method used by the constraint solver, don't use them directly |
---|
131 | virtual void setupSolverConstraint(btConstraintArray& ca, int solverBodyA,int solverBodyB, btScalar timeStep) |
---|
132 | { |
---|
133 | (void)ca; |
---|
134 | (void)solverBodyA; |
---|
135 | (void)solverBodyB; |
---|
136 | (void)timeStep; |
---|
137 | } |
---|
138 | |
---|
139 | ///internal method used by the constraint solver, don't use them directly |
---|
140 | virtual void getInfo1 (btConstraintInfo1* info)=0; |
---|
141 | |
---|
142 | ///internal method used by the constraint solver, don't use them directly |
---|
143 | virtual void getInfo2 (btConstraintInfo2* info)=0; |
---|
144 | |
---|
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 | } |
---|
155 | |
---|
156 | ///internal method used by the constraint solver, don't use them directly |
---|
157 | virtual void solveConstraintObsolete(btRigidBody& /*bodyA*/,btRigidBody& /*bodyB*/,btScalar /*timeStep*/) {}; |
---|
158 | |
---|
159 | |
---|
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 | } |
---|
192 | |
---|
193 | int getUserConstraintId() const |
---|
194 | { |
---|
195 | return m_userConstraintId; |
---|
196 | } |
---|
197 | |
---|
198 | void setUserConstraintPtr(void* ptr) |
---|
199 | { |
---|
200 | m_userConstraintPtr = ptr; |
---|
201 | } |
---|
202 | |
---|
203 | void* getUserConstraintPtr() |
---|
204 | { |
---|
205 | return m_userConstraintPtr; |
---|
206 | } |
---|
207 | |
---|
208 | int getUid() const |
---|
209 | { |
---|
210 | return m_userConstraintId; |
---|
211 | } |
---|
212 | |
---|
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. |
---|
227 | btScalar getAppliedImpulse() const |
---|
228 | { |
---|
229 | btAssert(m_needsFeedback); |
---|
230 | return m_appliedImpulse; |
---|
231 | } |
---|
232 | |
---|
233 | btTypedConstraintType getConstraintType () const |
---|
234 | { |
---|
235 | return btTypedConstraintType(m_objectType); |
---|
236 | } |
---|
237 | |
---|
238 | void setDbgDrawSize(btScalar dbgDrawSize) |
---|
239 | { |
---|
240 | m_dbgDrawSize = dbgDrawSize; |
---|
241 | } |
---|
242 | btScalar getDbgDrawSize() |
---|
243 | { |
---|
244 | return m_dbgDrawSize; |
---|
245 | } |
---|
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; |
---|
253 | |
---|
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 | |
---|
259 | }; |
---|
260 | |
---|
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 | |
---|
315 | #endif //TYPED_CONSTRAINT_H |
---|