1 | /* |
---|
2 | Bullet Continuous Collision Detection and Physics Library |
---|
3 | Copyright (c) 2003-2006 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 | /* Hinge Constraint by Dirk Gregorius. Limits added by Marcus Hennix at Starbreeze Studios */ |
---|
17 | |
---|
18 | #ifndef HINGECONSTRAINT_H |
---|
19 | #define HINGECONSTRAINT_H |
---|
20 | |
---|
21 | #include "LinearMath/btVector3.h" |
---|
22 | #include "btJacobianEntry.h" |
---|
23 | #include "btTypedConstraint.h" |
---|
24 | |
---|
25 | class btRigidBody; |
---|
26 | |
---|
27 | #ifdef BT_USE_DOUBLE_PRECISION |
---|
28 | #define btHingeConstraintData btHingeConstraintDoubleData |
---|
29 | #define btHingeConstraintDataName "btHingeConstraintDoubleData" |
---|
30 | #else |
---|
31 | #define btHingeConstraintData btHingeConstraintFloatData |
---|
32 | #define btHingeConstraintDataName "btHingeConstraintFloatData" |
---|
33 | #endif //BT_USE_DOUBLE_PRECISION |
---|
34 | |
---|
35 | |
---|
36 | enum btHingeFlags |
---|
37 | { |
---|
38 | BT_HINGE_FLAGS_CFM_STOP = 1, |
---|
39 | BT_HINGE_FLAGS_ERP_STOP = 2, |
---|
40 | BT_HINGE_FLAGS_CFM_NORM = 4 |
---|
41 | }; |
---|
42 | |
---|
43 | |
---|
44 | /// hinge constraint between two rigidbodies each with a pivotpoint that descibes the axis location in local space |
---|
45 | /// axis defines the orientation of the hinge axis |
---|
46 | ATTRIBUTE_ALIGNED16(class) btHingeConstraint : public btTypedConstraint |
---|
47 | { |
---|
48 | #ifdef IN_PARALLELL_SOLVER |
---|
49 | public: |
---|
50 | #endif |
---|
51 | btJacobianEntry m_jac[3]; //3 orthogonal linear constraints |
---|
52 | btJacobianEntry m_jacAng[3]; //2 orthogonal angular constraints+ 1 for limit/motor |
---|
53 | |
---|
54 | btTransform m_rbAFrame; // constraint axii. Assumes z is hinge axis. |
---|
55 | btTransform m_rbBFrame; |
---|
56 | |
---|
57 | btScalar m_motorTargetVelocity; |
---|
58 | btScalar m_maxMotorImpulse; |
---|
59 | |
---|
60 | btScalar m_limitSoftness; |
---|
61 | btScalar m_biasFactor; |
---|
62 | btScalar m_relaxationFactor; |
---|
63 | |
---|
64 | btScalar m_lowerLimit; |
---|
65 | btScalar m_upperLimit; |
---|
66 | |
---|
67 | btScalar m_kHinge; |
---|
68 | |
---|
69 | btScalar m_limitSign; |
---|
70 | btScalar m_correction; |
---|
71 | |
---|
72 | btScalar m_accLimitImpulse; |
---|
73 | btScalar m_hingeAngle; |
---|
74 | btScalar m_referenceSign; |
---|
75 | |
---|
76 | bool m_angularOnly; |
---|
77 | bool m_enableAngularMotor; |
---|
78 | bool m_solveLimit; |
---|
79 | bool m_useSolveConstraintObsolete; |
---|
80 | bool m_useOffsetForConstraintFrame; |
---|
81 | bool m_useReferenceFrameA; |
---|
82 | |
---|
83 | btScalar m_accMotorImpulse; |
---|
84 | |
---|
85 | int m_flags; |
---|
86 | btScalar m_normalCFM; |
---|
87 | btScalar m_stopCFM; |
---|
88 | btScalar m_stopERP; |
---|
89 | |
---|
90 | |
---|
91 | public: |
---|
92 | |
---|
93 | btHingeConstraint(btRigidBody& rbA,btRigidBody& rbB, const btVector3& pivotInA,const btVector3& pivotInB, const btVector3& axisInA,const btVector3& axisInB, bool useReferenceFrameA = false); |
---|
94 | |
---|
95 | btHingeConstraint(btRigidBody& rbA,const btVector3& pivotInA,const btVector3& axisInA, bool useReferenceFrameA = false); |
---|
96 | |
---|
97 | btHingeConstraint(btRigidBody& rbA,btRigidBody& rbB, const btTransform& rbAFrame, const btTransform& rbBFrame, bool useReferenceFrameA = false); |
---|
98 | |
---|
99 | btHingeConstraint(btRigidBody& rbA,const btTransform& rbAFrame, bool useReferenceFrameA = false); |
---|
100 | |
---|
101 | |
---|
102 | virtual void buildJacobian(); |
---|
103 | |
---|
104 | virtual void getInfo1 (btConstraintInfo1* info); |
---|
105 | |
---|
106 | void getInfo1NonVirtual(btConstraintInfo1* info); |
---|
107 | |
---|
108 | virtual void getInfo2 (btConstraintInfo2* info); |
---|
109 | |
---|
110 | void getInfo2NonVirtual(btConstraintInfo2* info,const btTransform& transA,const btTransform& transB,const btVector3& angVelA,const btVector3& angVelB); |
---|
111 | |
---|
112 | void getInfo2Internal(btConstraintInfo2* info,const btTransform& transA,const btTransform& transB,const btVector3& angVelA,const btVector3& angVelB); |
---|
113 | void getInfo2InternalUsingFrameOffset(btConstraintInfo2* info,const btTransform& transA,const btTransform& transB,const btVector3& angVelA,const btVector3& angVelB); |
---|
114 | |
---|
115 | |
---|
116 | void updateRHS(btScalar timeStep); |
---|
117 | |
---|
118 | const btRigidBody& getRigidBodyA() const |
---|
119 | { |
---|
120 | return m_rbA; |
---|
121 | } |
---|
122 | const btRigidBody& getRigidBodyB() const |
---|
123 | { |
---|
124 | return m_rbB; |
---|
125 | } |
---|
126 | |
---|
127 | btRigidBody& getRigidBodyA() |
---|
128 | { |
---|
129 | return m_rbA; |
---|
130 | } |
---|
131 | |
---|
132 | btRigidBody& getRigidBodyB() |
---|
133 | { |
---|
134 | return m_rbB; |
---|
135 | } |
---|
136 | |
---|
137 | void setAngularOnly(bool angularOnly) |
---|
138 | { |
---|
139 | m_angularOnly = angularOnly; |
---|
140 | } |
---|
141 | |
---|
142 | void enableAngularMotor(bool enableMotor,btScalar targetVelocity,btScalar maxMotorImpulse) |
---|
143 | { |
---|
144 | m_enableAngularMotor = enableMotor; |
---|
145 | m_motorTargetVelocity = targetVelocity; |
---|
146 | m_maxMotorImpulse = maxMotorImpulse; |
---|
147 | } |
---|
148 | |
---|
149 | // extra motor API, including ability to set a target rotation (as opposed to angular velocity) |
---|
150 | // note: setMotorTarget sets angular velocity under the hood, so you must call it every tick to |
---|
151 | // maintain a given angular target. |
---|
152 | void enableMotor(bool enableMotor) { m_enableAngularMotor = enableMotor; } |
---|
153 | void setMaxMotorImpulse(btScalar maxMotorImpulse) { m_maxMotorImpulse = maxMotorImpulse; } |
---|
154 | void setMotorTarget(const btQuaternion& qAinB, btScalar dt); // qAinB is rotation of body A wrt body B. |
---|
155 | void setMotorTarget(btScalar targetAngle, btScalar dt); |
---|
156 | |
---|
157 | |
---|
158 | void setLimit(btScalar low,btScalar high,btScalar _softness = 0.9f, btScalar _biasFactor = 0.3f, btScalar _relaxationFactor = 1.0f) |
---|
159 | { |
---|
160 | m_lowerLimit = btNormalizeAngle(low); |
---|
161 | m_upperLimit = btNormalizeAngle(high); |
---|
162 | |
---|
163 | m_limitSoftness = _softness; |
---|
164 | m_biasFactor = _biasFactor; |
---|
165 | m_relaxationFactor = _relaxationFactor; |
---|
166 | |
---|
167 | } |
---|
168 | |
---|
169 | void setAxis(btVector3& axisInA) |
---|
170 | { |
---|
171 | btVector3 rbAxisA1, rbAxisA2; |
---|
172 | btPlaneSpace1(axisInA, rbAxisA1, rbAxisA2); |
---|
173 | btVector3 pivotInA = m_rbAFrame.getOrigin(); |
---|
174 | // m_rbAFrame.getOrigin() = pivotInA; |
---|
175 | m_rbAFrame.getBasis().setValue( rbAxisA1.getX(),rbAxisA2.getX(),axisInA.getX(), |
---|
176 | rbAxisA1.getY(),rbAxisA2.getY(),axisInA.getY(), |
---|
177 | rbAxisA1.getZ(),rbAxisA2.getZ(),axisInA.getZ() ); |
---|
178 | |
---|
179 | btVector3 axisInB = m_rbA.getCenterOfMassTransform().getBasis() * axisInA; |
---|
180 | |
---|
181 | btQuaternion rotationArc = shortestArcQuat(axisInA,axisInB); |
---|
182 | btVector3 rbAxisB1 = quatRotate(rotationArc,rbAxisA1); |
---|
183 | btVector3 rbAxisB2 = axisInB.cross(rbAxisB1); |
---|
184 | |
---|
185 | |
---|
186 | m_rbBFrame.getOrigin() = m_rbA.getCenterOfMassTransform()(pivotInA); |
---|
187 | m_rbBFrame.getBasis().setValue( rbAxisB1.getX(),rbAxisB2.getX(),axisInB.getX(), |
---|
188 | rbAxisB1.getY(),rbAxisB2.getY(),axisInB.getY(), |
---|
189 | rbAxisB1.getZ(),rbAxisB2.getZ(),axisInB.getZ() ); |
---|
190 | } |
---|
191 | |
---|
192 | btScalar getLowerLimit() const |
---|
193 | { |
---|
194 | return m_lowerLimit; |
---|
195 | } |
---|
196 | |
---|
197 | btScalar getUpperLimit() const |
---|
198 | { |
---|
199 | return m_upperLimit; |
---|
200 | } |
---|
201 | |
---|
202 | |
---|
203 | btScalar getHingeAngle(); |
---|
204 | |
---|
205 | btScalar getHingeAngle(const btTransform& transA,const btTransform& transB); |
---|
206 | |
---|
207 | void testLimit(const btTransform& transA,const btTransform& transB); |
---|
208 | |
---|
209 | |
---|
210 | const btTransform& getAFrame() const { return m_rbAFrame; }; |
---|
211 | const btTransform& getBFrame() const { return m_rbBFrame; }; |
---|
212 | |
---|
213 | btTransform& getAFrame() { return m_rbAFrame; }; |
---|
214 | btTransform& getBFrame() { return m_rbBFrame; }; |
---|
215 | |
---|
216 | inline int getSolveLimit() |
---|
217 | { |
---|
218 | return m_solveLimit; |
---|
219 | } |
---|
220 | |
---|
221 | inline btScalar getLimitSign() |
---|
222 | { |
---|
223 | return m_limitSign; |
---|
224 | } |
---|
225 | |
---|
226 | inline bool getAngularOnly() |
---|
227 | { |
---|
228 | return m_angularOnly; |
---|
229 | } |
---|
230 | inline bool getEnableAngularMotor() |
---|
231 | { |
---|
232 | return m_enableAngularMotor; |
---|
233 | } |
---|
234 | inline btScalar getMotorTargetVelosity() |
---|
235 | { |
---|
236 | return m_motorTargetVelocity; |
---|
237 | } |
---|
238 | inline btScalar getMaxMotorImpulse() |
---|
239 | { |
---|
240 | return m_maxMotorImpulse; |
---|
241 | } |
---|
242 | // access for UseFrameOffset |
---|
243 | bool getUseFrameOffset() { return m_useOffsetForConstraintFrame; } |
---|
244 | void setUseFrameOffset(bool frameOffsetOnOff) { m_useOffsetForConstraintFrame = frameOffsetOnOff; } |
---|
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); |
---|
250 | ///return the local value of parameter |
---|
251 | virtual btScalar getParam(int num, int axis = -1) const; |
---|
252 | |
---|
253 | virtual int calculateSerializeBufferSize() const; |
---|
254 | |
---|
255 | ///fills the dataBuffer and returns the struct name (and 0 on failure) |
---|
256 | virtual const char* serialize(void* dataBuffer, btSerializer* serializer) const; |
---|
257 | |
---|
258 | |
---|
259 | }; |
---|
260 | |
---|
261 | ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 |
---|
262 | struct btHingeConstraintDoubleData |
---|
263 | { |
---|
264 | btTypedConstraintData m_typeConstraintData; |
---|
265 | btTransformDoubleData m_rbAFrame; // constraint axii. Assumes z is hinge axis. |
---|
266 | btTransformDoubleData m_rbBFrame; |
---|
267 | int m_useReferenceFrameA; |
---|
268 | int m_angularOnly; |
---|
269 | int m_enableAngularMotor; |
---|
270 | float m_motorTargetVelocity; |
---|
271 | float m_maxMotorImpulse; |
---|
272 | |
---|
273 | float m_lowerLimit; |
---|
274 | float m_upperLimit; |
---|
275 | float m_limitSoftness; |
---|
276 | float m_biasFactor; |
---|
277 | float m_relaxationFactor; |
---|
278 | |
---|
279 | }; |
---|
280 | ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64 |
---|
281 | struct btHingeConstraintFloatData |
---|
282 | { |
---|
283 | btTypedConstraintData m_typeConstraintData; |
---|
284 | btTransformFloatData m_rbAFrame; // constraint axii. Assumes z is hinge axis. |
---|
285 | btTransformFloatData m_rbBFrame; |
---|
286 | int m_useReferenceFrameA; |
---|
287 | int m_angularOnly; |
---|
288 | |
---|
289 | int m_enableAngularMotor; |
---|
290 | float m_motorTargetVelocity; |
---|
291 | float m_maxMotorImpulse; |
---|
292 | |
---|
293 | float m_lowerLimit; |
---|
294 | float m_upperLimit; |
---|
295 | float m_limitSoftness; |
---|
296 | float m_biasFactor; |
---|
297 | float m_relaxationFactor; |
---|
298 | |
---|
299 | }; |
---|
300 | |
---|
301 | |
---|
302 | |
---|
303 | SIMD_FORCE_INLINE int btHingeConstraint::calculateSerializeBufferSize() const |
---|
304 | { |
---|
305 | return sizeof(btHingeConstraintData); |
---|
306 | } |
---|
307 | |
---|
308 | ///fills the dataBuffer and returns the struct name (and 0 on failure) |
---|
309 | SIMD_FORCE_INLINE const char* btHingeConstraint::serialize(void* dataBuffer, btSerializer* serializer) const |
---|
310 | { |
---|
311 | btHingeConstraintData* hingeData = (btHingeConstraintData*)dataBuffer; |
---|
312 | btTypedConstraint::serialize(&hingeData->m_typeConstraintData,serializer); |
---|
313 | |
---|
314 | m_rbAFrame.serialize(hingeData->m_rbAFrame); |
---|
315 | m_rbBFrame.serialize(hingeData->m_rbBFrame); |
---|
316 | |
---|
317 | hingeData->m_angularOnly = m_angularOnly; |
---|
318 | hingeData->m_enableAngularMotor = m_enableAngularMotor; |
---|
319 | hingeData->m_maxMotorImpulse = float(m_maxMotorImpulse); |
---|
320 | hingeData->m_motorTargetVelocity = float(m_motorTargetVelocity); |
---|
321 | hingeData->m_useReferenceFrameA = m_useReferenceFrameA; |
---|
322 | |
---|
323 | hingeData->m_lowerLimit = float(m_lowerLimit); |
---|
324 | hingeData->m_upperLimit = float(m_upperLimit); |
---|
325 | hingeData->m_limitSoftness = float(m_limitSoftness); |
---|
326 | hingeData->m_biasFactor = float(m_biasFactor); |
---|
327 | hingeData->m_relaxationFactor = float(m_relaxationFactor); |
---|
328 | |
---|
329 | return btHingeConstraintDataName; |
---|
330 | } |
---|
331 | |
---|
332 | #endif //HINGECONSTRAINT_H |
---|