2017-08-01 14:30:58 +02:00
/*
Bullet Continuous Collision Detection and Physics Library
Copyright ( c ) 2003 - 2006 Erwin Coumans http : //continuousphysics.com/Bullet/
This software is provided ' as - is ' , without any express or implied warranty .
In no event will the authors be held liable for any damages arising from the use of this software .
Permission is granted to anyone to use this software for any purpose ,
including commercial applications , and to alter it and redistribute it freely ,
subject to the following restrictions :
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 .
2. Altered source versions must be plainly marked as such , and must not be misrepresented as being the original software .
3. This notice may not be removed or altered from any source distribution .
*/
# ifndef BT_RIGIDBODY_H
# define BT_RIGIDBODY_H
# include "LinearMath/btAlignedObjectArray.h"
# include "LinearMath/btTransform.h"
# include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
# include "BulletCollision/CollisionDispatch/btCollisionObject.h"
class btCollisionShape ;
class btMotionState ;
class btTypedConstraint ;
extern btScalar gDeactivationTime ;
extern bool gDisableDeactivation ;
# ifdef BT_USE_DOUBLE_PRECISION
2019-01-03 14:26:51 +01:00
# define btRigidBodyData btRigidBodyDoubleData
# define btRigidBodyDataName "btRigidBodyDoubleData"
2017-08-01 14:30:58 +02:00
# else
2019-01-03 14:26:51 +01:00
# define btRigidBodyData btRigidBodyFloatData
# define btRigidBodyDataName "btRigidBodyFloatData"
# endif //BT_USE_DOUBLE_PRECISION
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
enum btRigidBodyFlags
2017-08-01 14:30:58 +02:00
{
BT_DISABLE_WORLD_GRAVITY = 1 ,
///BT_ENABLE_GYROPSCOPIC_FORCE flags is enabled by default in Bullet 2.83 and onwards.
///and it BT_ENABLE_GYROPSCOPIC_FORCE becomes equivalent to BT_ENABLE_GYROSCOPIC_FORCE_IMPLICIT_BODY
///See Demos/GyroscopicDemo and computeGyroscopicImpulseImplicit
BT_ENABLE_GYROSCOPIC_FORCE_EXPLICIT = 2 ,
2019-01-03 14:26:51 +01:00
BT_ENABLE_GYROSCOPIC_FORCE_IMPLICIT_WORLD = 4 ,
BT_ENABLE_GYROSCOPIC_FORCE_IMPLICIT_BODY = 8 ,
2017-08-01 14:30:58 +02:00
BT_ENABLE_GYROPSCOPIC_FORCE = BT_ENABLE_GYROSCOPIC_FORCE_IMPLICIT_BODY ,
} ;
///The btRigidBody is the main class for rigid body objects. It is derived from btCollisionObject, so it keeps a pointer to a btCollisionShape.
///It is recommended for performance and memory use to share btCollisionShape objects whenever possible.
2019-01-03 14:26:51 +01:00
///There are 3 types of rigid bodies:
2017-08-01 14:30:58 +02:00
///- A) Dynamic rigid bodies, with positive mass. Motion is controlled by rigid body dynamics.
///- B) Fixed objects with zero mass. They are not moving (basically collision objects)
2019-01-03 14:26:51 +01:00
///- C) Kinematic objects, which are objects without mass, but the user can move them. There is one-way interaction, and Bullet calculates a velocity based on the timestep and previous and current world transform.
2017-08-01 14:30:58 +02:00
///Bullet automatically deactivates dynamic rigid bodies, when the velocity is below a threshold for a given time.
///Deactivated (sleeping) rigid bodies don't take any processing time, except a minor broadphase collision detection impact (to allow active objects to activate/wake up sleeping objects)
2019-01-03 14:26:51 +01:00
class btRigidBody : public btCollisionObject
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
btMatrix3x3 m_invInertiaTensorWorld ;
btVector3 m_linearVelocity ;
btVector3 m_angularVelocity ;
btScalar m_inverseMass ;
btVector3 m_linearFactor ;
btVector3 m_gravity ;
btVector3 m_gravity_acceleration ;
btVector3 m_invInertiaLocal ;
btVector3 m_totalForce ;
btVector3 m_totalTorque ;
btScalar m_linearDamping ;
btScalar m_angularDamping ;
bool m_additionalDamping ;
btScalar m_additionalDampingFactor ;
btScalar m_additionalLinearDampingThresholdSqr ;
btScalar m_additionalAngularDampingThresholdSqr ;
btScalar m_additionalAngularDampingFactor ;
btScalar m_linearSleepingThreshold ;
btScalar m_angularSleepingThreshold ;
2017-08-01 14:30:58 +02:00
//m_optionalMotionState allows to automatic synchronize the world transform for active objects
2019-01-03 14:26:51 +01:00
btMotionState * m_optionalMotionState ;
2017-08-01 14:30:58 +02:00
//keep track of typed constraints referencing this rigid body, to disable collision between linked bodies
btAlignedObjectArray < btTypedConstraint * > m_constraintRefs ;
2019-01-03 14:26:51 +01:00
int m_rigidbodyFlags ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
int m_debugBodyId ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
protected :
ATTRIBUTE_ALIGNED16 ( btVector3 m_deltaLinearVelocity ) ;
btVector3 m_deltaAngularVelocity ;
btVector3 m_angularFactor ;
btVector3 m_invMass ;
btVector3 m_pushVelocity ;
btVector3 m_turnVelocity ;
2017-08-01 14:30:58 +02:00
public :
///The btRigidBodyConstructionInfo structure provides information to create a rigid body. Setting mass to zero creates a fixed (non-dynamic) rigid body.
///For dynamic objects, you can use the collision shape to approximate the local inertia tensor, otherwise use the zero vector (default argument)
2019-01-03 14:26:51 +01:00
///You can use the motion state to synchronize the world transform between physics and graphics objects.
2017-08-01 14:30:58 +02:00
///And if the motion state is provided, the rigid body will initialize its initial world transform from the motion state,
///m_startWorldTransform is only used when you don't provide a motion state.
2019-01-03 14:26:51 +01:00
struct btRigidBodyConstructionInfo
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
btScalar m_mass ;
2017-08-01 14:30:58 +02:00
///When a motionState is provided, the rigid body will initialize its world transform from the motion state
///In this case, m_startWorldTransform is ignored.
2019-01-03 14:26:51 +01:00
btMotionState * m_motionState ;
btTransform m_startWorldTransform ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
btCollisionShape * m_collisionShape ;
btVector3 m_localInertia ;
btScalar m_linearDamping ;
btScalar m_angularDamping ;
2017-08-01 14:30:58 +02:00
///best simulation results when friction is non-zero
2019-01-03 14:26:51 +01:00
btScalar m_friction ;
2017-08-01 14:30:58 +02:00
///the m_rollingFriction prevents rounded shapes, such as spheres, cylinders and capsules from rolling forever.
///See Bullet/Demos/RollingFrictionDemo for usage
2019-01-03 14:26:51 +01:00
btScalar m_rollingFriction ;
btScalar m_spinningFriction ; //torsional friction around contact normal
2017-08-01 14:30:58 +02:00
///best simulation results using zero restitution.
2019-01-03 14:26:51 +01:00
btScalar m_restitution ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
btScalar m_linearSleepingThreshold ;
btScalar m_angularSleepingThreshold ;
2017-08-01 14:30:58 +02:00
//Additional damping can help avoiding lowpass jitter motion, help stability for ragdolls etc.
//Such damping is undesirable, so once the overall simulation quality of the rigid body dynamics system has improved, this should become obsolete
2019-01-03 14:26:51 +01:00
bool m_additionalDamping ;
btScalar m_additionalDampingFactor ;
btScalar m_additionalLinearDampingThresholdSqr ;
btScalar m_additionalAngularDampingThresholdSqr ;
btScalar m_additionalAngularDampingFactor ;
btRigidBodyConstructionInfo ( btScalar mass , btMotionState * motionState , btCollisionShape * collisionShape , const btVector3 & localInertia = btVector3 ( 0 , 0 , 0 ) ) : m_mass ( mass ) ,
m_motionState ( motionState ) ,
m_collisionShape ( collisionShape ) ,
m_localInertia ( localInertia ) ,
m_linearDamping ( btScalar ( 0. ) ) ,
m_angularDamping ( btScalar ( 0. ) ) ,
m_friction ( btScalar ( 0.5 ) ) ,
m_rollingFriction ( btScalar ( 0 ) ) ,
m_spinningFriction ( btScalar ( 0 ) ) ,
m_restitution ( btScalar ( 0. ) ) ,
m_linearSleepingThreshold ( btScalar ( 0.8 ) ) ,
m_angularSleepingThreshold ( btScalar ( 1.f ) ) ,
m_additionalDamping ( false ) ,
m_additionalDampingFactor ( btScalar ( 0.005 ) ) ,
m_additionalLinearDampingThresholdSqr ( btScalar ( 0.01 ) ) ,
m_additionalAngularDampingThresholdSqr ( btScalar ( 0.01 ) ) ,
m_additionalAngularDampingFactor ( btScalar ( 0.01 ) )
2017-08-01 14:30:58 +02:00
{
m_startWorldTransform . setIdentity ( ) ;
}
} ;
///btRigidBody constructor using construction info
2019-01-03 14:26:51 +01:00
btRigidBody ( const btRigidBodyConstructionInfo & constructionInfo ) ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
///btRigidBody constructor for backwards compatibility.
2017-08-01 14:30:58 +02:00
///To specify friction (etc) during rigid body construction, please use the other constructor (using btRigidBodyConstructionInfo)
2019-01-03 14:26:51 +01:00
btRigidBody ( btScalar mass , btMotionState * motionState , btCollisionShape * collisionShape , const btVector3 & localInertia = btVector3 ( 0 , 0 , 0 ) ) ;
2017-08-01 14:30:58 +02:00
virtual ~ btRigidBody ( )
2019-01-03 14:26:51 +01:00
{
//No constraints should point to this rigidbody
//Remove constraints from the dynamics world before you delete the related rigidbodies.
btAssert ( m_constraintRefs . size ( ) = = 0 ) ;
}
2017-08-01 14:30:58 +02:00
protected :
///setupRigidBody is only used internally by the constructor
2019-01-03 14:26:51 +01:00
void setupRigidBody ( const btRigidBodyConstructionInfo & constructionInfo ) ;
2017-08-01 14:30:58 +02:00
public :
2019-01-03 14:26:51 +01:00
void proceedToTransform ( const btTransform & newTrans ) ;
2017-08-01 14:30:58 +02:00
///to keep collision detection and dynamics separate we don't store a rigidbody pointer
///but a rigidbody is derived from btCollisionObject, so we can safely perform an upcast
2019-01-03 14:26:51 +01:00
static const btRigidBody * upcast ( const btCollisionObject * colObj )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
if ( colObj - > getInternalType ( ) & btCollisionObject : : CO_RIGID_BODY )
2017-08-01 14:30:58 +02:00
return ( const btRigidBody * ) colObj ;
return 0 ;
}
2019-01-03 14:26:51 +01:00
static btRigidBody * upcast ( btCollisionObject * colObj )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
if ( colObj - > getInternalType ( ) & btCollisionObject : : CO_RIGID_BODY )
2017-08-01 14:30:58 +02:00
return ( btRigidBody * ) colObj ;
return 0 ;
}
2019-01-03 14:26:51 +01:00
2017-08-01 14:30:58 +02:00
/// continuous collision detection needs prediction
2019-01-03 14:26:51 +01:00
void predictIntegratedTransform ( btScalar step , btTransform & predictedTransform ) ;
void saveKinematicState ( btScalar step ) ;
void applyGravity ( ) ;
2020-01-08 18:05:43 +01:00
void clearGravity ( ) ;
2019-01-03 14:26:51 +01:00
void setGravity ( const btVector3 & acceleration ) ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
const btVector3 & getGravity ( ) const
2017-08-01 14:30:58 +02:00
{
return m_gravity_acceleration ;
}
2019-01-03 14:26:51 +01:00
void setDamping ( btScalar lin_damping , btScalar ang_damping ) ;
2017-08-01 14:30:58 +02:00
btScalar getLinearDamping ( ) const
{
return m_linearDamping ;
}
btScalar getAngularDamping ( ) const
{
return m_angularDamping ;
}
btScalar getLinearSleepingThreshold ( ) const
{
return m_linearSleepingThreshold ;
}
btScalar getAngularSleepingThreshold ( ) const
{
return m_angularSleepingThreshold ;
}
2019-01-03 14:26:51 +01:00
void applyDamping ( btScalar timeStep ) ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
SIMD_FORCE_INLINE const btCollisionShape * getCollisionShape ( ) const
{
2017-08-01 14:30:58 +02:00
return m_collisionShape ;
}
2019-01-03 14:26:51 +01:00
SIMD_FORCE_INLINE btCollisionShape * getCollisionShape ( )
{
return m_collisionShape ;
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
void setMassProps ( btScalar mass , const btVector3 & inertia ) ;
2017-08-01 14:30:58 +02:00
const btVector3 & getLinearFactor ( ) const
{
return m_linearFactor ;
}
void setLinearFactor ( const btVector3 & linearFactor )
{
m_linearFactor = linearFactor ;
2019-01-03 14:26:51 +01:00
m_invMass = m_linearFactor * m_inverseMass ;
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
btScalar getInvMass ( ) const { return m_inverseMass ; }
2020-01-08 18:05:43 +01:00
btScalar getMass ( ) const { return m_inverseMass = = btScalar ( 0. ) ? btScalar ( 0. ) : btScalar ( 1.0 ) / m_inverseMass ; }
2019-01-03 14:26:51 +01:00
const btMatrix3x3 & getInvInertiaTensorWorld ( ) const
{
return m_invInertiaTensorWorld ;
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
void integrateVelocities ( btScalar step ) ;
void setCenterOfMassTransform ( const btTransform & xform ) ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
void applyCentralForce ( const btVector3 & force )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
m_totalForce + = force * m_linearFactor ;
2017-08-01 14:30:58 +02:00
}
const btVector3 & getTotalForce ( ) const
{
return m_totalForce ;
} ;
const btVector3 & getTotalTorque ( ) const
{
return m_totalTorque ;
} ;
2019-01-03 14:26:51 +01:00
2017-08-01 14:30:58 +02:00
const btVector3 & getInvInertiaDiagLocal ( ) const
{
return m_invInertiaLocal ;
} ;
2019-01-03 14:26:51 +01:00
void setInvInertiaDiagLocal ( const btVector3 & diagInvInertia )
2017-08-01 14:30:58 +02:00
{
m_invInertiaLocal = diagInvInertia ;
}
2019-01-03 14:26:51 +01:00
void setSleepingThresholds ( btScalar linear , btScalar angular )
2017-08-01 14:30:58 +02:00
{
m_linearSleepingThreshold = linear ;
m_angularSleepingThreshold = angular ;
}
2019-01-03 14:26:51 +01:00
void applyTorque ( const btVector3 & torque )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
m_totalTorque + = torque * m_angularFactor ;
2020-04-27 10:15:23 +02:00
# if defined(BT_CLAMP_VELOCITY_TO) && BT_CLAMP_VELOCITY_TO > 0
clampVelocity ( m_totalTorque ) ;
# endif
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
void applyForce ( const btVector3 & force , const btVector3 & rel_pos )
2017-08-01 14:30:58 +02:00
{
applyCentralForce ( force ) ;
2019-01-03 14:26:51 +01:00
applyTorque ( rel_pos . cross ( force * m_linearFactor ) ) ;
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
2017-08-01 14:30:58 +02:00
void applyCentralImpulse ( const btVector3 & impulse )
{
2019-01-03 14:26:51 +01:00
m_linearVelocity + = impulse * m_linearFactor * m_inverseMass ;
2020-04-27 10:15:23 +02:00
# if defined(BT_CLAMP_VELOCITY_TO) && BT_CLAMP_VELOCITY_TO > 0
clampVelocity ( m_linearVelocity ) ;
# endif
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
void applyTorqueImpulse ( const btVector3 & torque )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
m_angularVelocity + = m_invInertiaTensorWorld * torque * m_angularFactor ;
2020-04-27 10:15:23 +02:00
# if defined(BT_CLAMP_VELOCITY_TO) && BT_CLAMP_VELOCITY_TO > 0
clampVelocity ( m_angularVelocity ) ;
# endif
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
void applyImpulse ( const btVector3 & impulse , const btVector3 & rel_pos )
2017-08-01 14:30:58 +02:00
{
if ( m_inverseMass ! = btScalar ( 0. ) )
{
applyCentralImpulse ( impulse ) ;
if ( m_angularFactor )
{
2019-01-03 14:26:51 +01:00
applyTorqueImpulse ( rel_pos . cross ( impulse * m_linearFactor ) ) ;
2017-08-01 14:30:58 +02:00
}
}
}
2020-01-08 18:05:43 +01:00
void applyPushImpulse ( const btVector3 & impulse , const btVector3 & rel_pos )
{
if ( m_inverseMass ! = btScalar ( 0. ) )
{
applyCentralPushImpulse ( impulse ) ;
if ( m_angularFactor )
{
applyTorqueTurnImpulse ( rel_pos . cross ( impulse * m_linearFactor ) ) ;
}
}
}
2020-12-17 13:51:12 +01:00
btVector3 getPushVelocity ( ) const
2020-01-08 18:05:43 +01:00
{
return m_pushVelocity ;
}
2020-12-17 13:51:12 +01:00
btVector3 getTurnVelocity ( ) const
2020-01-08 18:05:43 +01:00
{
return m_turnVelocity ;
}
void setPushVelocity ( const btVector3 & v )
{
m_pushVelocity = v ;
}
2020-04-27 10:15:23 +02:00
# if defined(BT_CLAMP_VELOCITY_TO) && BT_CLAMP_VELOCITY_TO > 0
void clampVelocity ( btVector3 & v ) const {
v . setX (
fmax ( - BT_CLAMP_VELOCITY_TO ,
fmin ( BT_CLAMP_VELOCITY_TO , v . getX ( ) ) )
) ;
v . setY (
fmax ( - BT_CLAMP_VELOCITY_TO ,
fmin ( BT_CLAMP_VELOCITY_TO , v . getY ( ) ) )
) ;
v . setZ (
fmax ( - BT_CLAMP_VELOCITY_TO ,
fmin ( BT_CLAMP_VELOCITY_TO , v . getZ ( ) ) )
) ;
}
# endif
2020-01-08 18:05:43 +01:00
void setTurnVelocity ( const btVector3 & v )
{
m_turnVelocity = v ;
2020-04-27 10:15:23 +02:00
# if defined(BT_CLAMP_VELOCITY_TO) && BT_CLAMP_VELOCITY_TO > 0
clampVelocity ( m_turnVelocity ) ;
# endif
2020-01-08 18:05:43 +01:00
}
void applyCentralPushImpulse ( const btVector3 & impulse )
{
m_pushVelocity + = impulse * m_linearFactor * m_inverseMass ;
2020-04-27 10:15:23 +02:00
# if defined(BT_CLAMP_VELOCITY_TO) && BT_CLAMP_VELOCITY_TO > 0
clampVelocity ( m_pushVelocity ) ;
# endif
2020-01-08 18:05:43 +01:00
}
void applyTorqueTurnImpulse ( const btVector3 & torque )
{
m_turnVelocity + = m_invInertiaTensorWorld * torque * m_angularFactor ;
2020-04-27 10:15:23 +02:00
# if defined(BT_CLAMP_VELOCITY_TO) && BT_CLAMP_VELOCITY_TO > 0
clampVelocity ( m_turnVelocity ) ;
# endif
2020-01-08 18:05:43 +01:00
}
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
void clearForces ( )
2017-08-01 14:30:58 +02:00
{
m_totalForce . setValue ( btScalar ( 0.0 ) , btScalar ( 0.0 ) , btScalar ( 0.0 ) ) ;
m_totalTorque . setValue ( btScalar ( 0.0 ) , btScalar ( 0.0 ) , btScalar ( 0.0 ) ) ;
}
2019-01-03 14:26:51 +01:00
void updateInertiaTensor ( ) ;
const btVector3 & getCenterOfMassPosition ( ) const
{
return m_worldTransform . getOrigin ( ) ;
2017-08-01 14:30:58 +02:00
}
btQuaternion getOrientation ( ) const ;
2019-01-03 14:26:51 +01:00
const btTransform & getCenterOfMassTransform ( ) const
{
return m_worldTransform ;
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
const btVector3 & getLinearVelocity ( ) const
{
return m_linearVelocity ;
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
const btVector3 & getAngularVelocity ( ) const
{
return m_angularVelocity ;
2017-08-01 14:30:58 +02:00
}
inline void setLinearVelocity ( const btVector3 & lin_vel )
2019-01-03 14:26:51 +01:00
{
2017-08-01 14:30:58 +02:00
m_updateRevision + + ;
2019-01-03 14:26:51 +01:00
m_linearVelocity = lin_vel ;
2020-04-27 10:15:23 +02:00
# if defined(BT_CLAMP_VELOCITY_TO) && BT_CLAMP_VELOCITY_TO > 0
clampVelocity ( m_linearVelocity ) ;
# endif
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
inline void setAngularVelocity ( const btVector3 & ang_vel )
{
2017-08-01 14:30:58 +02:00
m_updateRevision + + ;
2019-01-03 14:26:51 +01:00
m_angularVelocity = ang_vel ;
2020-04-27 10:15:23 +02:00
# if defined(BT_CLAMP_VELOCITY_TO) && BT_CLAMP_VELOCITY_TO > 0
clampVelocity ( m_angularVelocity ) ;
# endif
2017-08-01 14:30:58 +02:00
}
btVector3 getVelocityInLocalPoint ( const btVector3 & rel_pos ) const
{
//we also calculate lin/ang velocity for kinematic objects
return m_linearVelocity + m_angularVelocity . cross ( rel_pos ) ;
//for kinematic objects, we could also use use:
// return (m_worldTransform(rel_pos) - m_interpolationWorldTransform(rel_pos)) / m_kinematicTimeStep;
}
2020-12-17 13:51:12 +01:00
btVector3 getPushVelocityInLocalPoint ( const btVector3 & rel_pos ) const
{
//we also calculate lin/ang velocity for kinematic objects
return m_pushVelocity + m_turnVelocity . cross ( rel_pos ) ;
}
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
void translate ( const btVector3 & v )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
m_worldTransform . getOrigin ( ) + = v ;
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
void getAabb ( btVector3 & aabbMin , btVector3 & aabbMax ) const ;
2017-08-01 14:30:58 +02:00
SIMD_FORCE_INLINE btScalar computeImpulseDenominator ( const btVector3 & pos , const btVector3 & normal ) const
{
btVector3 r0 = pos - getCenterOfMassPosition ( ) ;
btVector3 c0 = ( r0 ) . cross ( normal ) ;
btVector3 vec = ( c0 * getInvInertiaTensorWorld ( ) ) . cross ( r0 ) ;
return m_inverseMass + normal . dot ( vec ) ;
}
SIMD_FORCE_INLINE btScalar computeAngularImpulseDenominator ( const btVector3 & axis ) const
{
btVector3 vec = axis * getInvInertiaTensorWorld ( ) ;
return axis . dot ( vec ) ;
}
2019-01-03 14:26:51 +01:00
SIMD_FORCE_INLINE void updateDeactivation ( btScalar timeStep )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
if ( ( getActivationState ( ) = = ISLAND_SLEEPING ) | | ( getActivationState ( ) = = DISABLE_DEACTIVATION ) )
2017-08-01 14:30:58 +02:00
return ;
2019-01-03 14:26:51 +01:00
if ( ( getLinearVelocity ( ) . length2 ( ) < m_linearSleepingThreshold * m_linearSleepingThreshold ) & &
( getAngularVelocity ( ) . length2 ( ) < m_angularSleepingThreshold * m_angularSleepingThreshold ) )
2017-08-01 14:30:58 +02:00
{
m_deactivationTime + = timeStep ;
2019-01-03 14:26:51 +01:00
}
else
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
m_deactivationTime = btScalar ( 0. ) ;
2017-08-01 14:30:58 +02:00
setActivationState ( 0 ) ;
}
}
2019-01-03 14:26:51 +01:00
SIMD_FORCE_INLINE bool wantsSleeping ( )
2017-08-01 14:30:58 +02:00
{
if ( getActivationState ( ) = = DISABLE_DEACTIVATION )
return false ;
//disable deactivation
if ( gDisableDeactivation | | ( gDeactivationTime = = btScalar ( 0. ) ) )
return false ;
2019-01-03 14:26:51 +01:00
if ( ( getActivationState ( ) = = ISLAND_SLEEPING ) | | ( getActivationState ( ) = = WANTS_DEACTIVATION ) )
2017-08-01 14:30:58 +02:00
return true ;
2019-01-03 14:26:51 +01:00
if ( m_deactivationTime > gDeactivationTime )
2017-08-01 14:30:58 +02:00
{
return true ;
}
return false ;
}
2019-01-03 14:26:51 +01:00
const btBroadphaseProxy * getBroadphaseProxy ( ) const
2017-08-01 14:30:58 +02:00
{
return m_broadphaseHandle ;
}
2019-01-03 14:26:51 +01:00
btBroadphaseProxy * getBroadphaseProxy ( )
2017-08-01 14:30:58 +02:00
{
return m_broadphaseHandle ;
}
2019-01-03 14:26:51 +01:00
void setNewBroadphaseProxy ( btBroadphaseProxy * broadphaseProxy )
2017-08-01 14:30:58 +02:00
{
m_broadphaseHandle = broadphaseProxy ;
}
//btMotionState allows to automatic synchronize the world transform for active objects
2019-01-03 14:26:51 +01:00
btMotionState * getMotionState ( )
2017-08-01 14:30:58 +02:00
{
return m_optionalMotionState ;
}
2019-01-03 14:26:51 +01:00
const btMotionState * getMotionState ( ) const
2017-08-01 14:30:58 +02:00
{
return m_optionalMotionState ;
}
2019-01-03 14:26:51 +01:00
void setMotionState ( btMotionState * motionState )
2017-08-01 14:30:58 +02:00
{
m_optionalMotionState = motionState ;
if ( m_optionalMotionState )
motionState - > getWorldTransform ( m_worldTransform ) ;
}
//for experimental overriding of friction/contact solver func
2019-01-03 14:26:51 +01:00
int m_contactSolverType ;
int m_frictionSolverType ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
void setAngularFactor ( const btVector3 & angFac )
2017-08-01 14:30:58 +02:00
{
m_updateRevision + + ;
m_angularFactor = angFac ;
}
2019-01-03 14:26:51 +01:00
void setAngularFactor ( btScalar angFac )
2017-08-01 14:30:58 +02:00
{
m_updateRevision + + ;
2019-01-03 14:26:51 +01:00
m_angularFactor . setValue ( angFac , angFac , angFac ) ;
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
const btVector3 & getAngularFactor ( ) const
2017-08-01 14:30:58 +02:00
{
return m_angularFactor ;
}
//is this rigidbody added to a btCollisionWorld/btDynamicsWorld/btBroadphase?
2019-01-03 14:26:51 +01:00
bool isInWorld ( ) const
2017-08-01 14:30:58 +02:00
{
return ( getBroadphaseProxy ( ) ! = 0 ) ;
}
void addConstraintRef ( btTypedConstraint * c ) ;
void removeConstraintRef ( btTypedConstraint * c ) ;
btTypedConstraint * getConstraintRef ( int index )
{
return m_constraintRefs [ index ] ;
}
int getNumConstraintRefs ( ) const
{
return m_constraintRefs . size ( ) ;
}
2019-01-03 14:26:51 +01:00
void setFlags ( int flags )
2017-08-01 14:30:58 +02:00
{
m_rigidbodyFlags = flags ;
}
int getFlags ( ) const
{
return m_rigidbodyFlags ;
}
///perform implicit force computation in world space
btVector3 computeGyroscopicImpulseImplicit_World ( btScalar dt ) const ;
2019-01-03 14:26:51 +01:00
2017-08-01 14:30:58 +02:00
///perform implicit force computation in body space (inertial frame)
btVector3 computeGyroscopicImpulseImplicit_Body ( btScalar step ) const ;
///explicit version is best avoided, it gains energy
btVector3 computeGyroscopicForceExplicit ( btScalar maxGyroscopicForce ) const ;
btVector3 getLocalInertia ( ) const ;
///////////////////////////////////////////////
2019-01-03 14:26:51 +01:00
virtual int calculateSerializeBufferSize ( ) const ;
2017-08-01 14:30:58 +02:00
///fills the dataBuffer and returns the struct name (and 0 on failure)
2019-01-03 14:26:51 +01:00
virtual const char * serialize ( void * dataBuffer , class btSerializer * serializer ) const ;
2017-08-01 14:30:58 +02:00
virtual void serializeSingleObject ( class btSerializer * serializer ) const ;
} ;
//@todo add m_optionalMotionState and m_constraintRefs to btRigidBodyData
///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
2019-01-03 14:26:51 +01:00
struct btRigidBodyFloatData
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
btCollisionObjectFloatData m_collisionObjectData ;
btMatrix3x3FloatData m_invInertiaTensorWorld ;
btVector3FloatData m_linearVelocity ;
btVector3FloatData m_angularVelocity ;
btVector3FloatData m_angularFactor ;
btVector3FloatData m_linearFactor ;
btVector3FloatData m_gravity ;
btVector3FloatData m_gravity_acceleration ;
btVector3FloatData m_invInertiaLocal ;
btVector3FloatData m_totalForce ;
btVector3FloatData m_totalTorque ;
float m_inverseMass ;
float m_linearDamping ;
float m_angularDamping ;
float m_additionalDampingFactor ;
float m_additionalLinearDampingThresholdSqr ;
float m_additionalAngularDampingThresholdSqr ;
float m_additionalAngularDampingFactor ;
float m_linearSleepingThreshold ;
float m_angularSleepingThreshold ;
int m_additionalDamping ;
2017-08-01 14:30:58 +02:00
} ;
///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
2019-01-03 14:26:51 +01:00
struct btRigidBodyDoubleData
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
btCollisionObjectDoubleData m_collisionObjectData ;
btMatrix3x3DoubleData m_invInertiaTensorWorld ;
btVector3DoubleData m_linearVelocity ;
btVector3DoubleData m_angularVelocity ;
btVector3DoubleData m_angularFactor ;
btVector3DoubleData m_linearFactor ;
btVector3DoubleData m_gravity ;
btVector3DoubleData m_gravity_acceleration ;
btVector3DoubleData m_invInertiaLocal ;
btVector3DoubleData m_totalForce ;
btVector3DoubleData m_totalTorque ;
double m_inverseMass ;
double m_linearDamping ;
double m_angularDamping ;
double m_additionalDampingFactor ;
double m_additionalLinearDampingThresholdSqr ;
double m_additionalAngularDampingThresholdSqr ;
double m_additionalAngularDampingFactor ;
double m_linearSleepingThreshold ;
double m_angularSleepingThreshold ;
int m_additionalDamping ;
char m_padding [ 4 ] ;
2017-08-01 14:30:58 +02:00
} ;
2019-01-03 14:26:51 +01:00
# endif //BT_RIGIDBODY_H