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  {
367  m_linearVelocity = lin_vel;
368  }
369 
370  inline void setAngularVelocity(const btVector3& ang_vel)
371  {
373  m_angularVelocity = ang_vel;
374  }
375 
377  {
378  //we also calculate lin/ang velocity for kinematic objects
379  return m_linearVelocity + m_angularVelocity.cross(rel_pos);
380 
381  //for kinematic objects, we could also use use:
382  // return (m_worldTransform(rel_pos) - m_interpolationWorldTransform(rel_pos)) / m_kinematicTimeStep;
383  }
384 
385  void translate(const btVector3& v)
386  {
387  m_worldTransform.getOrigin() += v;
388  }
389 
390 
391  void getAabb(btVector3& aabbMin,btVector3& aabbMax) const;
392 
393 
394 
395 
396 
398  {
399  btVector3 r0 = pos - getCenterOfMassPosition();
400 
401  btVector3 c0 = (r0).cross(normal);
402 
403  btVector3 vec = (c0 * getInvInertiaTensorWorld()).cross(r0);
404 
405  return m_inverseMass + normal.dot(vec);
406 
407  }
408 
410  {
411  btVector3 vec = axis * getInvInertiaTensorWorld();
412  return axis.dot(vec);
413  }
414 
416  {
418  return;
419 
422  {
423  m_deactivationTime += timeStep;
424  } else
425  {
428  }
429 
430  }
431 
433  {
434 
436  return false;
437 
438  //disable deactivation
440  return false;
441 
443  return true;
444 
446  {
447  return true;
448  }
449  return false;
450  }
451 
452 
453 
455  {
456  return m_broadphaseHandle;
457  }
459  {
460  return m_broadphaseHandle;
461  }
463  {
464  m_broadphaseHandle = broadphaseProxy;
465  }
466 
467  //btMotionState allows to automatic synchronize the world transform for active objects
469  {
470  return m_optionalMotionState;
471  }
473  {
474  return m_optionalMotionState;
475  }
476  void setMotionState(btMotionState* motionState)
477  {
478  m_optionalMotionState = motionState;
480  motionState->getWorldTransform(m_worldTransform);
481  }
482 
483  //for experimental overriding of friction/contact solver func
486 
487  void setAngularFactor(const btVector3& angFac)
488  {
490  m_angularFactor = angFac;
491  }
492 
494  {
496  m_angularFactor.setValue(angFac,angFac,angFac);
497  }
499  {
500  return m_angularFactor;
501  }
502 
503  //is this rigidbody added to a btCollisionWorld/btDynamicsWorld/btBroadphase?
504  bool isInWorld() const
505  {
506  return (getBroadphaseProxy() != 0);
507  }
508 
509  virtual bool checkCollideWithOverride(const btCollisionObject* co) const;
510 
513 
515  {
516  return m_constraintRefs[index];
517  }
518 
520  {
521  return m_constraintRefs.size();
522  }
523 
524  void setFlags(int flags)
525  {
526  m_rigidbodyFlags = flags;
527  }
528 
529  int getFlags() const
530  {
531  return m_rigidbodyFlags;
532  }
533 
534  btVector3 computeGyroscopicForce(btScalar maxGyroscopicForce) const;
535 
537 
538  virtual int calculateSerializeBufferSize() const;
539 
541  virtual const char* serialize(void* dataBuffer, class btSerializer* serializer) const;
542 
543  virtual void serializeSingleObject(class btSerializer* serializer) const;
544 
545 };
546 
547 //@todo add m_optionalMotionState and m_constraintRefs to btRigidBodyData
550 {
572 };
573 
576 {
598  char m_padding[4];
599 };
600 
601 
602 
603 #endif //BT_RIGIDBODY_H
604