Bullet Collision Detection & Physics Library
btTypedConstraint.h
Go to the documentation of this file.
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 BT_TYPED_CONSTRAINT_H
17 #define BT_TYPED_CONSTRAINT_H
18 
19 
20 #include "LinearMath/btScalar.h"
21 #include "btSolverConstraint.h"
23 
24 class btSerializer;
25 
26 //Don't change any of the existing enum values, so add enum types at the end for serialization compatibility
28 {
38 };
39 
40 
42 {
47 };
48 
49 #if 1
50  #define btAssertConstrParams(_par) btAssert(_par)
51 #else
52  #define btAssertConstrParams(_par)
53 #endif
54 
55 
57 {
62 };
63 
64 
67 {
69 
70  union
71  {
74  };
75 
80 
81 
83  {
84  btAssert(0);
85  (void) other;
86  return *this;
87  }
88 
89 protected:
95 
97  btScalar getMotorFactor(btScalar pos, btScalar lowLim, btScalar uppLim, btScalar vel, btScalar timeFact);
98 
99 
100 public:
101 
103 
104  virtual ~btTypedConstraint() {};
107 
109  int m_numConstraintRows,nub;
110  };
111 
112  static btRigidBody& getFixedBody();
113 
115  // integrator parameters: frames per second (1/stepsize), default error
116  // reduction parameter (0..1).
118 
119  // for the first and second body, pointers to two (linear and angular)
120  // n*3 jacobian sub matrices, stored by rows. these matrices will have
121  // been initialized to 0 on entry. if the second body is zero then the
122  // J2xx pointers may be 0.
123  btScalar *m_J1linearAxis,*m_J1angularAxis,*m_J2linearAxis,*m_J2angularAxis;
124 
125  // elements to jump from one row to the next in J's
126  int rowskip;
127 
128  // right hand sides of the equation J*v = c + cfm * lambda. cfm is the
129  // "constraint force mixing" vector. c is set to zero on entry, cfm is
130  // set to a constant value (typically very small or zero) value on entry.
132 
133  // lo and hi limits for variables (set to -/+ infinity on entry).
134  btScalar *m_lowerLimit,*m_upperLimit;
135 
136  // findex vector for variables. see the LCP solver interface for a
137  // description of what this does. this is set to -1 on entry.
138  // note that the returned indexes are relative to the first index of
139  // the constraint.
140  int *findex;
141  // number of solver iterations
143 
144  //damping of the velocity
146  };
147 
149  {
150  return m_overrideNumSolverIterations;
151  }
152 
155  void setOverrideNumSolverIterations(int overideNumIterations)
156  {
157  m_overrideNumSolverIterations = overideNumIterations;
158  }
159 
161  virtual void buildJacobian() {};
162 
164  virtual void setupSolverConstraint(btConstraintArray& ca, int solverBodyA,int solverBodyB, btScalar timeStep)
165  {
166  (void)ca;
167  (void)solverBodyA;
168  (void)solverBodyB;
169  (void)timeStep;
170  }
171 
173  virtual void getInfo1 (btConstraintInfo1* info)=0;
174 
176  virtual void getInfo2 (btConstraintInfo2* info)=0;
177 
179  void internalSetAppliedImpulse(btScalar appliedImpulse)
180  {
181  m_appliedImpulse = appliedImpulse;
182  }
185  {
186  return m_appliedImpulse;
187  }
188 
189 
191  {
192  return m_breakingImpulseThreshold;
193  }
194 
196  {
197  m_breakingImpulseThreshold = threshold;
198  }
199 
200  bool isEnabled() const
201  {
202  return m_isEnabled;
203  }
204 
205  void setEnabled(bool enabled)
206  {
207  m_isEnabled=enabled;
208  }
209 
210 
212  virtual void solveConstraintObsolete(btSolverBody& /*bodyA*/,btSolverBody& /*bodyB*/,btScalar /*timeStep*/) {};
213 
214 
215  const btRigidBody& getRigidBodyA() const
216  {
217  return m_rbA;
218  }
219  const btRigidBody& getRigidBodyB() const
220  {
221  return m_rbB;
222  }
223 
225  {
226  return m_rbA;
227  }
229  {
230  return m_rbB;
231  }
232 
234  {
235  return m_userConstraintType ;
236  }
237 
238  void setUserConstraintType(int userConstraintType)
239  {
240  m_userConstraintType = userConstraintType;
241  };
242 
243  void setUserConstraintId(int uid)
244  {
245  m_userConstraintId = uid;
246  }
247 
249  {
250  return m_userConstraintId;
251  }
252 
253  void setUserConstraintPtr(void* ptr)
254  {
255  m_userConstraintPtr = ptr;
256  }
257 
259  {
260  return m_userConstraintPtr;
261  }
262 
263  void setJointFeedback(btJointFeedback* jointFeedback)
264  {
265  m_jointFeedback = jointFeedback;
266  }
267 
269  {
270  return m_jointFeedback;
271  }
272 
274  {
275  return m_jointFeedback;
276  }
277 
278 
279  int getUid() const
280  {
281  return m_userConstraintId;
282  }
283 
284  bool needsFeedback() const
285  {
286  return m_needsFeedback;
287  }
288 
291  void enableFeedback(bool needsFeedback)
292  {
293  m_needsFeedback = needsFeedback;
294  }
295 
299  {
300  btAssert(m_needsFeedback);
301  return m_appliedImpulse;
302  }
303 
305  {
306  return btTypedConstraintType(m_objectType);
307  }
308 
309  void setDbgDrawSize(btScalar dbgDrawSize)
310  {
311  m_dbgDrawSize = dbgDrawSize;
312  }
314  {
315  return m_dbgDrawSize;
316  }
317 
320  virtual void setParam(int num, btScalar value, int axis = -1) = 0;
321 
323  virtual btScalar getParam(int num, int axis = -1) const = 0;
324 
325  virtual int calculateSerializeBufferSize() const;
326 
328  virtual const char* serialize(void* dataBuffer, btSerializer* serializer) const;
329 
330 };
331 
332 // returns angle in range [-SIMD_2_PI, SIMD_2_PI], closest to one of the limits
333 // all arguments should be normalized angles (i.e. in range [-SIMD_PI, SIMD_PI])
334 SIMD_FORCE_INLINE btScalar btAdjustAngleToLimits(btScalar angleInRadians, btScalar angleLowerLimitInRadians, btScalar angleUpperLimitInRadians)
335 {
336  if(angleLowerLimitInRadians >= angleUpperLimitInRadians)
337  {
338  return angleInRadians;
339  }
340  else if(angleInRadians < angleLowerLimitInRadians)
341  {
342  btScalar diffLo = btFabs(btNormalizeAngle(angleLowerLimitInRadians - angleInRadians));
343  btScalar diffHi = btFabs(btNormalizeAngle(angleUpperLimitInRadians - angleInRadians));
344  return (diffLo < diffHi) ? angleInRadians : (angleInRadians + SIMD_2_PI);
345  }
346  else if(angleInRadians > angleUpperLimitInRadians)
347  {
348  btScalar diffHi = btFabs(btNormalizeAngle(angleInRadians - angleUpperLimitInRadians));
349  btScalar diffLo = btFabs(btNormalizeAngle(angleInRadians - angleLowerLimitInRadians));
350  return (diffLo < diffHi) ? (angleInRadians - SIMD_2_PI) : angleInRadians;
351  }
352  else
353  {
354  return angleInRadians;
355  }
356 }
357 
360 {
363  char *m_name;
364 
369 
372 
375 
378 
379 };
380 
382 {
383  return sizeof(btTypedConstraintData);
384 }
385 
386 
387 
389 {
390 private:
391  btScalar
393  m_halfRange,
394  m_softness,
395  m_biasFactor,
397  m_correction,
398  m_sign;
399 
400  bool
402 
403 public:
406  :m_center(0.0f),
407  m_halfRange(-1.0f),
408  m_softness(0.9f),
409  m_biasFactor(0.3f),
410  m_relaxationFactor(1.0f),
411  m_correction(0.0f),
412  m_sign(0.0f),
413  m_solveLimit(false)
414  {}
415 
419  void set(btScalar low, btScalar high, btScalar _softness = 0.9f, btScalar _biasFactor = 0.3f, btScalar _relaxationFactor = 1.0f);
420 
423  void test(const btScalar angle);
424 
426  inline btScalar getSoftness() const
427  {
428  return m_softness;
429  }
430 
432  inline btScalar getBiasFactor() const
433  {
434  return m_biasFactor;
435  }
436 
439  {
440  return m_relaxationFactor;
441  }
442 
444  inline btScalar getCorrection() const
445  {
446  return m_correction;
447  }
448 
450  inline btScalar getSign() const
451  {
452  return m_sign;
453  }
454 
456  inline btScalar getHalfRange() const
457  {
458  return m_halfRange;
459  }
460 
462  inline bool isLimit() const
463  {
464  return m_solveLimit;
465  }
466 
469  void fit(btScalar& angle) const;
470 
472  btScalar getError() const;
473 
474  btScalar getLow() const;
475 
476  btScalar getHigh() const;
477 
478 };
479 
480 
481 
482 #endif //BT_TYPED_CONSTRAINT_H