Bullet Collision Detection & Physics Library
btRigidBody.h
Go to the documentation of this file.
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 #ifndef BT_RIGIDBODY_H
17 #define BT_RIGIDBODY_H
18 
20 #include "LinearMath/btTransform.h"
23 
24 class btCollisionShape;
25 class btMotionState;
26 class btTypedConstraint;
27 
28 
30 extern bool gDisableDeactivation;
31 
32 #ifdef BT_USE_DOUBLE_PRECISION
33 #define btRigidBodyData btRigidBodyDoubleData
34 #define btRigidBodyDataName "btRigidBodyDoubleData"
35 #else
36 #define btRigidBodyData btRigidBodyFloatData
37 #define btRigidBodyDataName "btRigidBodyFloatData"
38 #endif //BT_USE_DOUBLE_PRECISION
39 
40 
42 {
48 };
49 
50 
60 {
61 
67 
73 
76 
82 
83 
86 
87  //m_optionalMotionState allows to automatic synchronize the world transform for active objects
89 
90  //keep track of typed constraints referencing this rigid body
92 
94 
96 
97 
98 protected:
99 
106 
107 
108 public:
109 
110 
117  {
119 
124 
129 
137 
140 
141  //Additional damping can help avoiding lowpass jitter motion, help stability for ragdolls etc.
142  //Such damping is undesirable, so once the overall simulation quality of the rigid body dynamics system has improved, this should become obsolete
148 
149  btRigidBodyConstructionInfo( btScalar mass, btMotionState* motionState, btCollisionShape* collisionShape, const btVector3& localInertia=btVector3(0,0,0)):
150  m_mass(mass),
151  m_motionState(motionState),
152  m_collisionShape(collisionShape),
153  m_localInertia(localInertia),
156  m_friction(btScalar(0.5)),
158  m_restitution(btScalar(0.)),
161  m_additionalDamping(false),
166  {
168  }
169  };
170 
172  btRigidBody( const btRigidBodyConstructionInfo& constructionInfo);
173 
176  btRigidBody( btScalar mass, btMotionState* motionState, btCollisionShape* collisionShape, const btVector3& localInertia=btVector3(0,0,0));
177 
178 
179  virtual ~btRigidBody()
180  {
181  //No constraints should point to this rigidbody
182  //Remove constraints from the dynamics world before you delete the related rigidbodies.
184  }
185 
186 protected:
187 
189  void setupRigidBody(const btRigidBodyConstructionInfo& constructionInfo);
190 
191 public:
192 
193  void proceedToTransform(const btTransform& newTrans);
194 
197  static const btRigidBody* upcast(const btCollisionObject* colObj)
198  {
200  return (const btRigidBody*)colObj;
201  return 0;
202  }
204  {
206  return (btRigidBody*)colObj;
207  return 0;
208  }
209 
211  void predictIntegratedTransform(btScalar step, btTransform& predictedTransform) ;
212 
213  void saveKinematicState(btScalar step);
214 
215  void applyGravity();
216 
217  void setGravity(const btVector3& acceleration);
218 
219  const btVector3& getGravity() const
220  {
221  return m_gravity_acceleration;
222  }
223 
224  void setDamping(btScalar lin_damping, btScalar ang_damping);
225 
227  {
228  return m_linearDamping;
229  }
230 
232  {
233  return m_angularDamping;
234  }
235 
237  {
239  }
240 
242  {
244  }
245 
246  void applyDamping(btScalar timeStep);
247 
249  return m_collisionShape;
250  }
251 
253  return m_collisionShape;
254  }
255 
256  void setMassProps(btScalar mass, const btVector3& inertia);
257 
258  const btVector3& getLinearFactor() const
259  {
260  return m_linearFactor;
261  }
262  void setLinearFactor(const btVector3& linearFactor)
263  {
264  m_linearFactor = linearFactor;
266  }
267  btScalar getInvMass() const { return m_inverseMass; }
269  return m_invInertiaTensorWorld;
270  }
271 
272  void integrateVelocities(btScalar step);
273 
274  void setCenterOfMassTransform(const btTransform& xform);
275 
276  void applyCentralForce(const btVector3& force)
277  {
278  m_totalForce += force*m_linearFactor;
279  }
280 
281  const btVector3& getTotalForce() const
282  {
283  return m_totalForce;
284  };
285 
286  const btVector3& getTotalTorque() const
287  {
288  return m_totalTorque;
289  };
290 
292  {
293  return m_invInertiaLocal;
294  };
295 
296  void setInvInertiaDiagLocal(const btVector3& diagInvInertia)
297  {
298  m_invInertiaLocal = diagInvInertia;
299  }
300 
302  {
303  m_linearSleepingThreshold = linear;
304  m_angularSleepingThreshold = angular;
305  }
306 
307  void applyTorque(const btVector3& torque)
308  {
309  m_totalTorque += torque*m_angularFactor;
310  }
311 
312  void applyForce(const btVector3& force, const btVector3& rel_pos)
313  {
314  applyCentralForce(force);
315  applyTorque(rel_pos.cross(force*m_linearFactor));
316  }
317 
318  void applyCentralImpulse(const btVector3& impulse)
319  {
321  }
322 
323  void applyTorqueImpulse(const btVector3& torque)
324  {
326  }
327 
328  void applyImpulse(const btVector3& impulse, const btVector3& rel_pos)
329  {
330  if (m_inverseMass != btScalar(0.))
331  {
332  applyCentralImpulse(impulse);
333  if (m_angularFactor)
334  {
335  applyTorqueImpulse(rel_pos.cross(impulse*m_linearFactor));
336  }
337  }
338  }
339 
340  void clearForces()
341  {
342  m_totalForce.setValue(btScalar(0.0), btScalar(0.0), btScalar(0.0));
343  m_totalTorque.setValue(btScalar(0.0), btScalar(0.0), btScalar(0.0));
344  }
345 
346  void updateInertiaTensor();
347 
349  return m_worldTransform.getOrigin();
350  }
352 
354  return m_worldTransform;
355  }
356  const btVector3& getLinearVelocity() const {
357  return m_linearVelocity;
358  }
359  const btVector3& getAngularVelocity() const {
360  return m_angularVelocity;
361  }
362 
363 
364  inline void setLinearVelocity(const btVector3& lin_vel)
365  {
366  m_linearVelocity = lin_vel;
367  }
368 
369  inline void setAngularVelocity(const btVector3& ang_vel)
370  {
371  m_angularVelocity = ang_vel;
372  }
373 
375  {
376  //we also calculate lin/ang velocity for kinematic objects
377  return m_linearVelocity + m_angularVelocity.cross(rel_pos);
378 
379  //for kinematic objects, we could also use use:
380  // return (m_worldTransform(rel_pos) - m_interpolationWorldTransform(rel_pos)) / m_kinematicTimeStep;
381  }
382 
383  void translate(const btVector3& v)
384  {
385  m_worldTransform.getOrigin() += v;
386  }
387 
388 
389  void getAabb(btVector3& aabbMin,btVector3& aabbMax) const;
390 
391 
392 
393 
394 
396  {
397  btVector3 r0 = pos - getCenterOfMassPosition();
398 
399  btVector3 c0 = (r0).cross(normal);
400 
401  btVector3 vec = (c0 * getInvInertiaTensorWorld()).cross(r0);
402 
403  return m_inverseMass + normal.dot(vec);
404 
405  }
406 
408  {
409  btVector3 vec = axis * getInvInertiaTensorWorld();
410  return axis.dot(vec);
411  }
412 
414  {
416  return;
417 
420  {
421  m_deactivationTime += timeStep;
422  } else
423  {
426  }
427 
428  }
429 
431  {
432 
434  return false;
435 
436  //disable deactivation
438  return false;
439 
441  return true;
442 
444  {
445  return true;
446  }
447  return false;
448  }
449 
450 
451 
453  {
454  return m_broadphaseHandle;
455  }
457  {
458  return m_broadphaseHandle;
459  }
461  {
462  m_broadphaseHandle = broadphaseProxy;
463  }
464 
465  //btMotionState allows to automatic synchronize the world transform for active objects
467  {
468  return m_optionalMotionState;
469  }
471  {
472  return m_optionalMotionState;
473  }
474  void setMotionState(btMotionState* motionState)
475  {
476  m_optionalMotionState = motionState;
478  motionState->getWorldTransform(m_worldTransform);
479  }
480 
481  //for experimental overriding of friction/contact solver func
484 
485  void setAngularFactor(const btVector3& angFac)
486  {
487  m_angularFactor = angFac;
488  }
489 
491  {
492  m_angularFactor.setValue(angFac,angFac,angFac);
493  }
495  {
496  return m_angularFactor;
497  }
498 
499  //is this rigidbody added to a btCollisionWorld/btDynamicsWorld/btBroadphase?
500  bool isInWorld() const
501  {
502  return (getBroadphaseProxy() != 0);
503  }
504 
505  virtual bool checkCollideWithOverride(const btCollisionObject* co) const;
506 
509 
511  {
512  return m_constraintRefs[index];
513  }
514 
516  {
517  return m_constraintRefs.size();
518  }
519 
520  void setFlags(int flags)
521  {
522  m_rigidbodyFlags = flags;
523  }
524 
525  int getFlags() const
526  {
527  return m_rigidbodyFlags;
528  }
529 
530  btVector3 computeGyroscopicForce(btScalar maxGyroscopicForce) const;
531 
533 
534  virtual int calculateSerializeBufferSize() const;
535 
537  virtual const char* serialize(void* dataBuffer, class btSerializer* serializer) const;
538 
539  virtual void serializeSingleObject(class btSerializer* serializer) const;
540 
541 };
542 
543 //@todo add m_optionalMotionState and m_constraintRefs to btRigidBodyData
546 {
568 };
569 
572 {
594  char m_padding[4];
595 };
596 
597 
598 
599 #endif //BT_RIGIDBODY_H
600