2017-08-01 14:30:58 +02:00
/*
Bullet Continuous Collision Detection and Physics Library
Copyright ( c ) 2003 - 2009 Erwin Coumans http : //bulletphysics.org
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 .
*/
# include "btDiscreteDynamicsWorld.h"
//collision detection
# include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
# include "BulletCollision/BroadphaseCollision/btSimpleBroadphase.h"
# include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h"
# include "BulletCollision/CollisionShapes/btCollisionShape.h"
# include "BulletCollision/CollisionDispatch/btSimulationIslandManager.h"
# include "LinearMath/btTransformUtil.h"
# include "LinearMath/btQuickprof.h"
//rigidbody & constraints
# include "BulletDynamics/Dynamics/btRigidBody.h"
# include "BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"
# include "BulletDynamics/ConstraintSolver/btContactSolverInfo.h"
# include "BulletDynamics/ConstraintSolver/btTypedConstraint.h"
# include "BulletDynamics/ConstraintSolver/btPoint2PointConstraint.h"
# include "BulletDynamics/ConstraintSolver/btHingeConstraint.h"
# include "BulletDynamics/ConstraintSolver/btConeTwistConstraint.h"
# include "BulletDynamics/ConstraintSolver/btGeneric6DofConstraint.h"
# include "BulletDynamics/ConstraintSolver/btGeneric6DofSpring2Constraint.h"
# include "BulletDynamics/ConstraintSolver/btSliderConstraint.h"
# include "BulletDynamics/ConstraintSolver/btContactConstraint.h"
# include "LinearMath/btIDebugDraw.h"
# include "BulletCollision/CollisionShapes/btSphereShape.h"
# include "BulletDynamics/Dynamics/btActionInterface.h"
# include "LinearMath/btQuickprof.h"
# include "LinearMath/btMotionState.h"
# include "LinearMath/btSerializer.h"
#if 0
btAlignedObjectArray < btVector3 > debugContacts ;
btAlignedObjectArray < btVector3 > debugNormals ;
int startHit = 2 ;
int firstHit = startHit ;
# endif
2019-01-03 14:26:51 +01:00
SIMD_FORCE_INLINE int btGetConstraintIslandId ( const btTypedConstraint * lhs )
2017-08-01 14:30:58 +02:00
{
int islandId ;
const btCollisionObject & rcolObj0 = lhs - > getRigidBodyA ( ) ;
const btCollisionObject & rcolObj1 = lhs - > getRigidBodyB ( ) ;
2019-01-03 14:26:51 +01:00
islandId = rcolObj0 . getIslandTag ( ) > = 0 ? rcolObj0 . getIslandTag ( ) : rcolObj1 . getIslandTag ( ) ;
2017-08-01 14:30:58 +02:00
return islandId ;
}
class btSortConstraintOnIslandPredicate
{
2019-01-03 14:26:51 +01:00
public :
bool operator ( ) ( const btTypedConstraint * lhs , const btTypedConstraint * rhs ) const
{
int rIslandId0 , lIslandId0 ;
rIslandId0 = btGetConstraintIslandId ( rhs ) ;
lIslandId0 = btGetConstraintIslandId ( lhs ) ;
return lIslandId0 < rIslandId0 ;
}
2017-08-01 14:30:58 +02:00
} ;
struct InplaceSolverIslandCallback : public btSimulationIslandManager : : IslandCallback
{
2019-01-03 14:26:51 +01:00
btContactSolverInfo * m_solverInfo ;
btConstraintSolver * m_solver ;
btTypedConstraint * * m_sortedConstraints ;
int m_numConstraints ;
btIDebugDraw * m_debugDrawer ;
btDispatcher * m_dispatcher ;
2017-08-01 14:30:58 +02:00
btAlignedObjectArray < btCollisionObject * > m_bodies ;
btAlignedObjectArray < btPersistentManifold * > m_manifolds ;
btAlignedObjectArray < btTypedConstraint * > m_constraints ;
InplaceSolverIslandCallback (
2019-01-03 14:26:51 +01:00
btConstraintSolver * solver ,
2017-08-01 14:30:58 +02:00
btStackAlloc * stackAlloc ,
btDispatcher * dispatcher )
2019-01-03 14:26:51 +01:00
: m_solverInfo ( NULL ) ,
m_solver ( solver ) ,
m_sortedConstraints ( NULL ) ,
m_numConstraints ( 0 ) ,
m_debugDrawer ( NULL ) ,
m_dispatcher ( dispatcher )
2017-08-01 14:30:58 +02:00
{
}
InplaceSolverIslandCallback & operator = ( InplaceSolverIslandCallback & other )
{
btAssert ( 0 ) ;
( void ) other ;
return * this ;
}
2019-01-03 14:26:51 +01:00
SIMD_FORCE_INLINE void setup ( btContactSolverInfo * solverInfo , btTypedConstraint * * sortedConstraints , int numConstraints , btIDebugDraw * debugDrawer )
2017-08-01 14:30:58 +02:00
{
btAssert ( solverInfo ) ;
m_solverInfo = solverInfo ;
m_sortedConstraints = sortedConstraints ;
m_numConstraints = numConstraints ;
m_debugDrawer = debugDrawer ;
2019-01-03 14:26:51 +01:00
m_bodies . resize ( 0 ) ;
m_manifolds . resize ( 0 ) ;
m_constraints . resize ( 0 ) ;
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
virtual void processIsland ( btCollisionObject * * bodies , int numBodies , btPersistentManifold * * manifolds , int numManifolds , int islandId )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
if ( islandId < 0 )
2017-08-01 14:30:58 +02:00
{
///we don't split islands, so all constraints/contact manifolds/bodies are passed into the solver regardless the island id
2019-01-03 14:26:51 +01:00
m_solver - > solveGroup ( bodies , numBodies , manifolds , numManifolds , & m_sortedConstraints [ 0 ] , m_numConstraints , * m_solverInfo , m_debugDrawer , m_dispatcher ) ;
}
else
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
//also add all non-contact constraints/joints for this island
2017-08-01 14:30:58 +02:00
btTypedConstraint * * startConstraint = 0 ;
int numCurConstraints = 0 ;
int i ;
//find the first constraint for this island
2019-01-03 14:26:51 +01:00
for ( i = 0 ; i < m_numConstraints ; i + + )
2017-08-01 14:30:58 +02:00
{
if ( btGetConstraintIslandId ( m_sortedConstraints [ i ] ) = = islandId )
{
startConstraint = & m_sortedConstraints [ i ] ;
break ;
}
}
//count the number of constraints in this island
2019-01-03 14:26:51 +01:00
for ( ; i < m_numConstraints ; i + + )
2017-08-01 14:30:58 +02:00
{
if ( btGetConstraintIslandId ( m_sortedConstraints [ i ] ) = = islandId )
{
numCurConstraints + + ;
}
}
2019-01-03 14:26:51 +01:00
if ( m_solverInfo - > m_minimumSolverBatchSize < = 1 )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
m_solver - > solveGroup ( bodies , numBodies , manifolds , numManifolds , startConstraint , numCurConstraints , * m_solverInfo , m_debugDrawer , m_dispatcher ) ;
}
else
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
for ( i = 0 ; i < numBodies ; i + + )
2017-08-01 14:30:58 +02:00
m_bodies . push_back ( bodies [ i ] ) ;
2019-01-03 14:26:51 +01:00
for ( i = 0 ; i < numManifolds ; i + + )
2017-08-01 14:30:58 +02:00
m_manifolds . push_back ( manifolds [ i ] ) ;
2019-01-03 14:26:51 +01:00
for ( i = 0 ; i < numCurConstraints ; i + + )
2017-08-01 14:30:58 +02:00
m_constraints . push_back ( startConstraint [ i ] ) ;
2019-01-03 14:26:51 +01:00
if ( ( m_constraints . size ( ) + m_manifolds . size ( ) ) > m_solverInfo - > m_minimumSolverBatchSize )
2017-08-01 14:30:58 +02:00
{
processConstraints ( ) ;
2019-01-03 14:26:51 +01:00
}
else
2017-08-01 14:30:58 +02:00
{
//printf("deferred\n");
}
}
}
}
2019-01-03 14:26:51 +01:00
void processConstraints ( )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
btCollisionObject * * bodies = m_bodies . size ( ) ? & m_bodies [ 0 ] : 0 ;
btPersistentManifold * * manifold = m_manifolds . size ( ) ? & m_manifolds [ 0 ] : 0 ;
btTypedConstraint * * constraints = m_constraints . size ( ) ? & m_constraints [ 0 ] : 0 ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
m_solver - > solveGroup ( bodies , m_bodies . size ( ) , manifold , m_manifolds . size ( ) , constraints , m_constraints . size ( ) , * m_solverInfo , m_debugDrawer , m_dispatcher ) ;
2017-08-01 14:30:58 +02:00
m_bodies . resize ( 0 ) ;
m_manifolds . resize ( 0 ) ;
m_constraints . resize ( 0 ) ;
}
} ;
2019-01-03 14:26:51 +01:00
btDiscreteDynamicsWorld : : btDiscreteDynamicsWorld ( btDispatcher * dispatcher , btBroadphaseInterface * pairCache , btConstraintSolver * constraintSolver , btCollisionConfiguration * collisionConfiguration )
: btDynamicsWorld ( dispatcher , pairCache , collisionConfiguration ) ,
m_sortedConstraints ( ) ,
m_solverIslandCallback ( NULL ) ,
m_constraintSolver ( constraintSolver ) ,
m_gravity ( 0 , - 10 , 0 ) ,
m_localTime ( 0 ) ,
m_fixedTimeStep ( 0 ) ,
m_synchronizeAllMotionStates ( false ) ,
m_applySpeculativeContactRestitution ( false ) ,
m_profileTimings ( 0 ) ,
m_latencyMotionStateInterpolation ( true )
2017-08-01 14:30:58 +02:00
{
if ( ! m_constraintSolver )
{
2019-01-03 14:26:51 +01:00
void * mem = btAlignedAlloc ( sizeof ( btSequentialImpulseConstraintSolver ) , 16 ) ;
2017-08-01 14:30:58 +02:00
m_constraintSolver = new ( mem ) btSequentialImpulseConstraintSolver ;
m_ownsConstraintSolver = true ;
2019-01-03 14:26:51 +01:00
}
else
2017-08-01 14:30:58 +02:00
{
m_ownsConstraintSolver = false ;
}
{
2019-01-03 14:26:51 +01:00
void * mem = btAlignedAlloc ( sizeof ( btSimulationIslandManager ) , 16 ) ;
2017-08-01 14:30:58 +02:00
m_islandManager = new ( mem ) btSimulationIslandManager ( ) ;
}
m_ownsIslandManager = true ;
{
2019-01-03 14:26:51 +01:00
void * mem = btAlignedAlloc ( sizeof ( InplaceSolverIslandCallback ) , 16 ) ;
m_solverIslandCallback = new ( mem ) InplaceSolverIslandCallback ( m_constraintSolver , 0 , dispatcher ) ;
2017-08-01 14:30:58 +02:00
}
}
btDiscreteDynamicsWorld : : ~ btDiscreteDynamicsWorld ( )
{
//only delete it when we created it
if ( m_ownsIslandManager )
{
m_islandManager - > ~ btSimulationIslandManager ( ) ;
2019-01-03 14:26:51 +01:00
btAlignedFree ( m_islandManager ) ;
2017-08-01 14:30:58 +02:00
}
if ( m_solverIslandCallback )
{
m_solverIslandCallback - > ~ InplaceSolverIslandCallback ( ) ;
btAlignedFree ( m_solverIslandCallback ) ;
}
if ( m_ownsConstraintSolver )
{
m_constraintSolver - > ~ btConstraintSolver ( ) ;
btAlignedFree ( m_constraintSolver ) ;
}
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : saveKinematicState ( btScalar timeStep )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
///would like to iterate over m_nonStaticRigidBodies, but unfortunately old API allows
///to switch status _after_ adding kinematic objects to the world
///fix it for Bullet 3.x release
for ( int i = 0 ; i < m_collisionObjects . size ( ) ; i + + )
2017-08-01 14:30:58 +02:00
{
btCollisionObject * colObj = m_collisionObjects [ i ] ;
btRigidBody * body = btRigidBody : : upcast ( colObj ) ;
if ( body & & body - > getActivationState ( ) ! = ISLAND_SLEEPING )
{
if ( body - > isKinematicObject ( ) )
{
//to calculate velocities next frame
body - > saveKinematicState ( timeStep ) ;
}
}
}
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : debugDrawWorld ( )
2017-08-01 14:30:58 +02:00
{
BT_PROFILE ( " debugDrawWorld " ) ;
btCollisionWorld : : debugDrawWorld ( ) ;
bool drawConstraints = false ;
if ( getDebugDrawer ( ) )
{
int mode = getDebugDrawer ( ) - > getDebugMode ( ) ;
2019-01-03 14:26:51 +01:00
if ( mode & ( btIDebugDraw : : DBG_DrawConstraints | btIDebugDraw : : DBG_DrawConstraintLimits ) )
2017-08-01 14:30:58 +02:00
{
drawConstraints = true ;
}
}
2019-01-03 14:26:51 +01:00
if ( drawConstraints )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
for ( int i = getNumConstraints ( ) - 1 ; i > = 0 ; i - - )
2017-08-01 14:30:58 +02:00
{
btTypedConstraint * constraint = getConstraint ( i ) ;
debugDrawConstraint ( constraint ) ;
}
}
2019-01-03 14:26:51 +01:00
if ( getDebugDrawer ( ) & & ( getDebugDrawer ( ) - > getDebugMode ( ) & ( btIDebugDraw : : DBG_DrawWireframe | btIDebugDraw : : DBG_DrawAabb | btIDebugDraw : : DBG_DrawNormals ) ) )
2017-08-01 14:30:58 +02:00
{
int i ;
if ( getDebugDrawer ( ) & & getDebugDrawer ( ) - > getDebugMode ( ) )
{
2019-01-03 14:26:51 +01:00
for ( i = 0 ; i < m_actions . size ( ) ; i + + )
2017-08-01 14:30:58 +02:00
{
m_actions [ i ] - > debugDraw ( m_debugDrawer ) ;
}
}
}
2019-01-03 14:26:51 +01:00
if ( getDebugDrawer ( ) )
getDebugDrawer ( ) - > flushLines ( ) ;
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : clearForces ( )
2017-08-01 14:30:58 +02:00
{
///@todo: iterate over awake simulation islands!
2019-01-03 14:26:51 +01:00
for ( int i = 0 ; i < m_nonStaticRigidBodies . size ( ) ; i + + )
2017-08-01 14:30:58 +02:00
{
btRigidBody * body = m_nonStaticRigidBodies [ i ] ;
//need to check if next line is ok
//it might break backward compatibility (people applying forces on sleeping objects get never cleared and accumulate on wake-up
body - > clearForces ( ) ;
}
}
///apply gravity, call this once per timestep
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : applyGravity ( )
2017-08-01 14:30:58 +02:00
{
///@todo: iterate over awake simulation islands!
2019-01-03 14:26:51 +01:00
for ( int i = 0 ; i < m_nonStaticRigidBodies . size ( ) ; i + + )
2017-08-01 14:30:58 +02:00
{
btRigidBody * body = m_nonStaticRigidBodies [ i ] ;
if ( body - > isActive ( ) )
{
body - > applyGravity ( ) ;
}
}
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : synchronizeSingleMotionState ( btRigidBody * body )
2017-08-01 14:30:58 +02:00
{
btAssert ( body ) ;
if ( body - > getMotionState ( ) & & ! body - > isStaticOrKinematicObject ( ) )
{
//we need to call the update at least once, even for sleeping objects
//otherwise the 'graphics' transform never updates properly
///@todo: add 'dirty' flag
//if (body->getActivationState() != ISLAND_SLEEPING)
{
btTransform interpolatedTransform ;
btTransformUtil : : integrateTransform ( body - > getInterpolationWorldTransform ( ) ,
2019-01-03 14:26:51 +01:00
body - > getInterpolationLinearVelocity ( ) , body - > getInterpolationAngularVelocity ( ) ,
( m_latencyMotionStateInterpolation & & m_fixedTimeStep ) ? m_localTime - m_fixedTimeStep : m_localTime * body - > getHitFraction ( ) ,
interpolatedTransform ) ;
2017-08-01 14:30:58 +02:00
body - > getMotionState ( ) - > setWorldTransform ( interpolatedTransform ) ;
}
}
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : synchronizeMotionStates ( )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
// BT_PROFILE("synchronizeMotionStates");
2017-08-01 14:30:58 +02:00
if ( m_synchronizeAllMotionStates )
{
//iterate over all collision objects
2019-01-03 14:26:51 +01:00
for ( int i = 0 ; i < m_collisionObjects . size ( ) ; i + + )
2017-08-01 14:30:58 +02:00
{
btCollisionObject * colObj = m_collisionObjects [ i ] ;
btRigidBody * body = btRigidBody : : upcast ( colObj ) ;
if ( body )
synchronizeSingleMotionState ( body ) ;
}
2019-01-03 14:26:51 +01:00
}
else
2017-08-01 14:30:58 +02:00
{
//iterate over all active rigid bodies
2019-01-03 14:26:51 +01:00
for ( int i = 0 ; i < m_nonStaticRigidBodies . size ( ) ; i + + )
2017-08-01 14:30:58 +02:00
{
btRigidBody * body = m_nonStaticRigidBodies [ i ] ;
if ( body - > isActive ( ) )
synchronizeSingleMotionState ( body ) ;
}
}
}
2019-01-03 14:26:51 +01:00
int btDiscreteDynamicsWorld : : stepSimulation ( btScalar timeStep , int maxSubSteps , btScalar fixedTimeStep )
2017-08-01 14:30:58 +02:00
{
startProfiling ( timeStep ) ;
int numSimulationSubSteps = 0 ;
if ( maxSubSteps )
{
//fixed timestep with interpolation
m_fixedTimeStep = fixedTimeStep ;
m_localTime + = timeStep ;
if ( m_localTime > = fixedTimeStep )
{
2019-01-03 14:26:51 +01:00
numSimulationSubSteps = int ( m_localTime / fixedTimeStep ) ;
2017-08-01 14:30:58 +02:00
m_localTime - = numSimulationSubSteps * fixedTimeStep ;
}
2019-01-03 14:26:51 +01:00
}
else
2017-08-01 14:30:58 +02:00
{
//variable timestep
fixedTimeStep = timeStep ;
m_localTime = m_latencyMotionStateInterpolation ? 0 : timeStep ;
m_fixedTimeStep = 0 ;
if ( btFuzzyZero ( timeStep ) )
{
numSimulationSubSteps = 0 ;
maxSubSteps = 0 ;
2019-01-03 14:26:51 +01:00
}
else
2017-08-01 14:30:58 +02:00
{
numSimulationSubSteps = 1 ;
maxSubSteps = 1 ;
}
}
//process some debugging flags
if ( getDebugDrawer ( ) )
{
2019-01-03 14:26:51 +01:00
btIDebugDraw * debugDrawer = getDebugDrawer ( ) ;
2017-08-01 14:30:58 +02:00
gDisableDeactivation = ( debugDrawer - > getDebugMode ( ) & btIDebugDraw : : DBG_NoDeactivation ) ! = 0 ;
}
if ( numSimulationSubSteps )
{
//clamp the number of substeps, to prevent simulation grinding spiralling down to a halt
2019-01-03 14:26:51 +01:00
int clampedSimulationSteps = ( numSimulationSubSteps > maxSubSteps ) ? maxSubSteps : numSimulationSubSteps ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
saveKinematicState ( fixedTimeStep * clampedSimulationSteps ) ;
2017-08-01 14:30:58 +02:00
applyGravity ( ) ;
2019-01-03 14:26:51 +01:00
for ( int i = 0 ; i < clampedSimulationSteps ; i + + )
2017-08-01 14:30:58 +02:00
{
internalSingleStepSimulation ( fixedTimeStep ) ;
synchronizeMotionStates ( ) ;
}
2019-01-03 14:26:51 +01:00
}
else
2017-08-01 14:30:58 +02:00
{
synchronizeMotionStates ( ) ;
}
clearForces ( ) ;
# ifndef BT_NO_PROFILE
CProfileManager : : Increment_Frame_Counter ( ) ;
2019-01-03 14:26:51 +01:00
# endif //BT_NO_PROFILE
2017-08-01 14:30:58 +02:00
return numSimulationSubSteps ;
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : internalSingleStepSimulation ( btScalar timeStep )
2017-08-01 14:30:58 +02:00
{
BT_PROFILE ( " internalSingleStepSimulation " ) ;
2019-01-03 14:26:51 +01:00
if ( 0 ! = m_internalPreTickCallback )
{
2017-08-01 14:30:58 +02:00
( * m_internalPreTickCallback ) ( this , timeStep ) ;
}
///apply gravity, predict motion
predictUnconstraintMotion ( timeStep ) ;
btDispatcherInfo & dispatchInfo = getDispatchInfo ( ) ;
dispatchInfo . m_timeStep = timeStep ;
dispatchInfo . m_stepCount = 0 ;
dispatchInfo . m_debugDraw = getDebugDrawer ( ) ;
2019-01-03 14:26:51 +01:00
createPredictiveContacts ( timeStep ) ;
2017-08-01 14:30:58 +02:00
///perform collision detection
performDiscreteCollisionDetection ( ) ;
calculateSimulationIslands ( ) ;
getSolverInfo ( ) . m_timeStep = timeStep ;
///solve contact and other joint constraints
solveConstraints ( getSolverInfo ( ) ) ;
///CallbackTriggers();
///integrate transforms
integrateTransforms ( timeStep ) ;
///update vehicle simulation
updateActions ( timeStep ) ;
2019-01-03 14:26:51 +01:00
updateActivationState ( timeStep ) ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
if ( 0 ! = m_internalTickCallback )
{
2017-08-01 14:30:58 +02:00
( * m_internalTickCallback ) ( this , timeStep ) ;
}
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : setGravity ( const btVector3 & gravity )
2017-08-01 14:30:58 +02:00
{
m_gravity = gravity ;
2019-01-03 14:26:51 +01:00
for ( int i = 0 ; i < m_nonStaticRigidBodies . size ( ) ; i + + )
2017-08-01 14:30:58 +02:00
{
btRigidBody * body = m_nonStaticRigidBodies [ i ] ;
2019-01-03 14:26:51 +01:00
if ( body - > isActive ( ) & & ! ( body - > getFlags ( ) & BT_DISABLE_WORLD_GRAVITY ) )
2017-08-01 14:30:58 +02:00
{
body - > setGravity ( gravity ) ;
}
}
}
2019-01-03 14:26:51 +01:00
btVector3 btDiscreteDynamicsWorld : : getGravity ( ) const
2017-08-01 14:30:58 +02:00
{
return m_gravity ;
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : addCollisionObject ( btCollisionObject * collisionObject , int collisionFilterGroup , int collisionFilterMask )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
btCollisionWorld : : addCollisionObject ( collisionObject , collisionFilterGroup , collisionFilterMask ) ;
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : removeCollisionObject ( btCollisionObject * collisionObject )
2017-08-01 14:30:58 +02:00
{
btRigidBody * body = btRigidBody : : upcast ( collisionObject ) ;
if ( body )
removeRigidBody ( body ) ;
else
btCollisionWorld : : removeCollisionObject ( collisionObject ) ;
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : removeRigidBody ( btRigidBody * body )
2017-08-01 14:30:58 +02:00
{
m_nonStaticRigidBodies . remove ( body ) ;
btCollisionWorld : : removeCollisionObject ( body ) ;
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : addRigidBody ( btRigidBody * body )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
if ( ! body - > isStaticOrKinematicObject ( ) & & ! ( body - > getFlags ( ) & BT_DISABLE_WORLD_GRAVITY ) )
2017-08-01 14:30:58 +02:00
{
body - > setGravity ( m_gravity ) ;
}
if ( body - > getCollisionShape ( ) )
{
if ( ! body - > isStaticObject ( ) )
{
m_nonStaticRigidBodies . push_back ( body ) ;
2019-01-03 14:26:51 +01:00
}
else
2017-08-01 14:30:58 +02:00
{
body - > setActivationState ( ISLAND_SLEEPING ) ;
}
bool isDynamic = ! ( body - > isStaticObject ( ) | | body - > isKinematicObject ( ) ) ;
2019-01-03 14:26:51 +01:00
int collisionFilterGroup = isDynamic ? int ( btBroadphaseProxy : : DefaultFilter ) : int ( btBroadphaseProxy : : StaticFilter ) ;
int collisionFilterMask = isDynamic ? int ( btBroadphaseProxy : : AllFilter ) : int ( btBroadphaseProxy : : AllFilter ^ btBroadphaseProxy : : StaticFilter ) ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
addCollisionObject ( body , collisionFilterGroup , collisionFilterMask ) ;
2017-08-01 14:30:58 +02:00
}
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : addRigidBody ( btRigidBody * body , int group , int mask )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
if ( ! body - > isStaticOrKinematicObject ( ) & & ! ( body - > getFlags ( ) & BT_DISABLE_WORLD_GRAVITY ) )
2017-08-01 14:30:58 +02:00
{
body - > setGravity ( m_gravity ) ;
}
if ( body - > getCollisionShape ( ) )
{
if ( ! body - > isStaticObject ( ) )
{
m_nonStaticRigidBodies . push_back ( body ) ;
}
2019-01-03 14:26:51 +01:00
else
2017-08-01 14:30:58 +02:00
{
body - > setActivationState ( ISLAND_SLEEPING ) ;
}
2019-01-03 14:26:51 +01:00
addCollisionObject ( body , group , mask ) ;
2017-08-01 14:30:58 +02:00
}
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : updateActions ( btScalar timeStep )
2017-08-01 14:30:58 +02:00
{
BT_PROFILE ( " updateActions " ) ;
2019-01-03 14:26:51 +01:00
for ( int i = 0 ; i < m_actions . size ( ) ; i + + )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
m_actions [ i ] - > updateAction ( this , timeStep ) ;
2017-08-01 14:30:58 +02:00
}
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : updateActivationState ( btScalar timeStep )
2017-08-01 14:30:58 +02:00
{
BT_PROFILE ( " updateActivationState " ) ;
2019-01-03 14:26:51 +01:00
for ( int i = 0 ; i < m_nonStaticRigidBodies . size ( ) ; i + + )
2017-08-01 14:30:58 +02:00
{
btRigidBody * body = m_nonStaticRigidBodies [ i ] ;
if ( body )
{
body - > updateDeactivation ( timeStep ) ;
if ( body - > wantsSleeping ( ) )
{
if ( body - > isStaticOrKinematicObject ( ) )
{
body - > setActivationState ( ISLAND_SLEEPING ) ;
2019-01-03 14:26:51 +01:00
}
else
2017-08-01 14:30:58 +02:00
{
if ( body - > getActivationState ( ) = = ACTIVE_TAG )
2019-01-03 14:26:51 +01:00
body - > setActivationState ( WANTS_DEACTIVATION ) ;
2017-08-01 14:30:58 +02:00
if ( body - > getActivationState ( ) = = ISLAND_SLEEPING )
{
2019-01-03 14:26:51 +01:00
body - > setAngularVelocity ( btVector3 ( 0 , 0 , 0 ) ) ;
body - > setLinearVelocity ( btVector3 ( 0 , 0 , 0 ) ) ;
2017-08-01 14:30:58 +02:00
}
}
2019-01-03 14:26:51 +01:00
}
else
2017-08-01 14:30:58 +02:00
{
if ( body - > getActivationState ( ) ! = DISABLE_DEACTIVATION )
2019-01-03 14:26:51 +01:00
body - > setActivationState ( ACTIVE_TAG ) ;
2017-08-01 14:30:58 +02:00
}
}
}
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : addConstraint ( btTypedConstraint * constraint , bool disableCollisionsBetweenLinkedBodies )
2017-08-01 14:30:58 +02:00
{
m_constraints . push_back ( constraint ) ;
2019-01-03 14:26:51 +01:00
//Make sure the two bodies of a type constraint are different (possibly add this to the btTypedConstraint constructor?)
btAssert ( & constraint - > getRigidBodyA ( ) ! = & constraint - > getRigidBodyB ( ) ) ;
2017-08-01 14:30:58 +02:00
if ( disableCollisionsBetweenLinkedBodies )
{
constraint - > getRigidBodyA ( ) . addConstraintRef ( constraint ) ;
constraint - > getRigidBodyB ( ) . addConstraintRef ( constraint ) ;
}
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : removeConstraint ( btTypedConstraint * constraint )
2017-08-01 14:30:58 +02:00
{
m_constraints . remove ( constraint ) ;
constraint - > getRigidBodyA ( ) . removeConstraintRef ( constraint ) ;
constraint - > getRigidBodyB ( ) . removeConstraintRef ( constraint ) ;
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : addAction ( btActionInterface * action )
2017-08-01 14:30:58 +02:00
{
m_actions . push_back ( action ) ;
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : removeAction ( btActionInterface * action )
2017-08-01 14:30:58 +02:00
{
m_actions . remove ( action ) ;
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : addVehicle ( btActionInterface * vehicle )
2017-08-01 14:30:58 +02:00
{
addAction ( vehicle ) ;
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : removeVehicle ( btActionInterface * vehicle )
2017-08-01 14:30:58 +02:00
{
removeAction ( vehicle ) ;
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : addCharacter ( btActionInterface * character )
2017-08-01 14:30:58 +02:00
{
addAction ( character ) ;
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : removeCharacter ( btActionInterface * character )
2017-08-01 14:30:58 +02:00
{
removeAction ( character ) ;
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : solveConstraints ( btContactSolverInfo & solverInfo )
2017-08-01 14:30:58 +02:00
{
BT_PROFILE ( " solveConstraints " ) ;
2019-01-03 14:26:51 +01:00
m_sortedConstraints . resize ( m_constraints . size ( ) ) ;
2017-08-01 14:30:58 +02:00
int i ;
2019-01-03 14:26:51 +01:00
for ( i = 0 ; i < getNumConstraints ( ) ; i + + )
2017-08-01 14:30:58 +02:00
{
m_sortedConstraints [ i ] = m_constraints [ i ] ;
}
2019-01-03 14:26:51 +01:00
// btAssert(0);
2017-08-01 14:30:58 +02:00
m_sortedConstraints . quickSort ( btSortConstraintOnIslandPredicate ( ) ) ;
btTypedConstraint * * constraintsPtr = getNumConstraints ( ) ? & m_sortedConstraints [ 0 ] : 0 ;
2019-01-03 14:26:51 +01:00
m_solverIslandCallback - > setup ( & solverInfo , constraintsPtr , m_sortedConstraints . size ( ) , getDebugDrawer ( ) ) ;
2017-08-01 14:30:58 +02:00
m_constraintSolver - > prepareSolve ( getCollisionWorld ( ) - > getNumCollisionObjects ( ) , getCollisionWorld ( ) - > getDispatcher ( ) - > getNumManifolds ( ) ) ;
/// solve all the constraints for this island
2019-01-03 14:26:51 +01:00
m_islandManager - > buildAndProcessIslands ( getCollisionWorld ( ) - > getDispatcher ( ) , getCollisionWorld ( ) , m_solverIslandCallback ) ;
2017-08-01 14:30:58 +02:00
m_solverIslandCallback - > processConstraints ( ) ;
m_constraintSolver - > allSolved ( solverInfo , m_debugDrawer ) ;
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : calculateSimulationIslands ( )
2017-08-01 14:30:58 +02:00
{
BT_PROFILE ( " calculateSimulationIslands " ) ;
2019-01-03 14:26:51 +01:00
getSimulationIslandManager ( ) - > updateActivationState ( getCollisionWorld ( ) , getCollisionWorld ( ) - > getDispatcher ( ) ) ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
{
//merge islands based on speculative contact manifolds too
for ( int i = 0 ; i < this - > m_predictiveManifolds . size ( ) ; i + + )
{
btPersistentManifold * manifold = m_predictiveManifolds [ i ] ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
const btCollisionObject * colObj0 = manifold - > getBody0 ( ) ;
const btCollisionObject * colObj1 = manifold - > getBody1 ( ) ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
if ( ( ( colObj0 ) & & ( ! ( colObj0 ) - > isStaticOrKinematicObject ( ) ) ) & &
( ( colObj1 ) & & ( ! ( colObj1 ) - > isStaticOrKinematicObject ( ) ) ) )
{
getSimulationIslandManager ( ) - > getUnionFind ( ) . unite ( ( colObj0 ) - > getIslandTag ( ) , ( colObj1 ) - > getIslandTag ( ) ) ;
}
}
}
2017-08-01 14:30:58 +02:00
{
int i ;
int numConstraints = int ( m_constraints . size ( ) ) ;
2019-01-03 14:26:51 +01:00
for ( i = 0 ; i < numConstraints ; i + + )
2017-08-01 14:30:58 +02:00
{
btTypedConstraint * constraint = m_constraints [ i ] ;
if ( constraint - > isEnabled ( ) )
{
const btRigidBody * colObj0 = & constraint - > getRigidBodyA ( ) ;
const btRigidBody * colObj1 = & constraint - > getRigidBodyB ( ) ;
if ( ( ( colObj0 ) & & ( ! ( colObj0 ) - > isStaticOrKinematicObject ( ) ) ) & &
( ( colObj1 ) & & ( ! ( colObj1 ) - > isStaticOrKinematicObject ( ) ) ) )
{
2019-01-03 14:26:51 +01:00
getSimulationIslandManager ( ) - > getUnionFind ( ) . unite ( ( colObj0 ) - > getIslandTag ( ) , ( colObj1 ) - > getIslandTag ( ) ) ;
2017-08-01 14:30:58 +02:00
}
}
}
}
//Store the island id in each body
getSimulationIslandManager ( ) - > storeIslandActivationState ( getCollisionWorld ( ) ) ;
}
class btClosestNotMeConvexResultCallback : public btCollisionWorld : : ClosestConvexResultCallback
{
public :
btCollisionObject * m_me ;
btScalar m_allowedPenetration ;
btOverlappingPairCache * m_pairCache ;
btDispatcher * m_dispatcher ;
public :
2019-01-03 14:26:51 +01:00
btClosestNotMeConvexResultCallback ( btCollisionObject * me , const btVector3 & fromA , const btVector3 & toA , btOverlappingPairCache * pairCache , btDispatcher * dispatcher ) : btCollisionWorld : : ClosestConvexResultCallback ( fromA , toA ) ,
m_me ( me ) ,
m_allowedPenetration ( 0.0f ) ,
m_pairCache ( pairCache ) ,
m_dispatcher ( dispatcher )
2017-08-01 14:30:58 +02:00
{
}
2019-01-03 14:26:51 +01:00
virtual btScalar addSingleResult ( btCollisionWorld : : LocalConvexResult & convexResult , bool normalInWorldSpace )
2017-08-01 14:30:58 +02:00
{
if ( convexResult . m_hitCollisionObject = = m_me )
return 1.0f ;
//ignore result if there is no contact response
2019-01-03 14:26:51 +01:00
if ( ! convexResult . m_hitCollisionObject - > hasContactResponse ( ) )
2017-08-01 14:30:58 +02:00
return 1.0f ;
2019-01-03 14:26:51 +01:00
btVector3 linVelA , linVelB ;
linVelA = m_convexToWorld - m_convexFromWorld ;
linVelB = btVector3 ( 0 , 0 , 0 ) ; //toB.getOrigin()-fromB.getOrigin();
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
btVector3 relativeVelocity = ( linVelA - linVelB ) ;
2017-08-01 14:30:58 +02:00
//don't report time of impact for motion away from the contact normal (or causes minor penetration)
2019-01-03 14:26:51 +01:00
if ( convexResult . m_hitNormalLocal . dot ( relativeVelocity ) > = - m_allowedPenetration )
2017-08-01 14:30:58 +02:00
return 1.f ;
2019-01-03 14:26:51 +01:00
return ClosestConvexResultCallback : : addSingleResult ( convexResult , normalInWorldSpace ) ;
2017-08-01 14:30:58 +02:00
}
virtual bool needsCollision ( btBroadphaseProxy * proxy0 ) const
{
//don't collide with itself
if ( proxy0 - > m_clientObject = = m_me )
return false ;
///don't do CCD when the collision filters are not matching
if ( ! ClosestConvexResultCallback : : needsCollision ( proxy0 ) )
return false ;
2020-04-27 10:15:23 +02:00
if ( m_pairCache - > getOverlapFilterCallback ( ) ) {
btBroadphaseProxy * proxy1 = m_me - > getBroadphaseHandle ( ) ;
bool collides = m_pairCache - > needsBroadphaseCollision ( proxy0 , proxy1 ) ;
if ( ! collides )
{
return false ;
}
}
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
btCollisionObject * otherObj = ( btCollisionObject * ) proxy0 - > m_clientObject ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
if ( ! m_dispatcher - > needsCollision ( m_me , otherObj ) )
2018-09-07 16:11:04 +02:00
return false ;
2017-08-01 14:30:58 +02:00
//call needsResponse, see http://code.google.com/p/bullet/issues/detail?id=179
2019-01-03 14:26:51 +01:00
if ( m_dispatcher - > needsResponse ( m_me , otherObj ) )
2017-08-01 14:30:58 +02:00
{
#if 0
///don't do CCD when there are already contact points (touching contact/penetration)
btAlignedObjectArray < btPersistentManifold * > manifoldArray ;
btBroadphasePair * collisionPair = m_pairCache - > findPair ( m_me - > getBroadphaseHandle ( ) , proxy0 ) ;
if ( collisionPair )
{
if ( collisionPair - > m_algorithm )
{
manifoldArray . resize ( 0 ) ;
collisionPair - > m_algorithm - > getAllContactManifolds ( manifoldArray ) ;
for ( int j = 0 ; j < manifoldArray . size ( ) ; j + + )
{
btPersistentManifold * manifold = manifoldArray [ j ] ;
if ( manifold - > getNumContacts ( ) > 0 )
return false ;
}
}
}
# endif
return true ;
}
return false ;
}
} ;
///internal debugging variable. this value shouldn't be too high
2019-01-03 14:26:51 +01:00
int gNumClampedCcdMotions = 0 ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : createPredictiveContactsInternal ( btRigidBody * * bodies , int numBodies , btScalar timeStep )
2017-08-01 14:30:58 +02:00
{
btTransform predictedTrans ;
2019-01-03 14:26:51 +01:00
for ( int i = 0 ; i < numBodies ; i + + )
2017-08-01 14:30:58 +02:00
{
btRigidBody * body = bodies [ i ] ;
body - > setHitFraction ( 1.f ) ;
if ( body - > isActive ( ) & & ( ! body - > isStaticOrKinematicObject ( ) ) )
{
body - > predictIntegratedTransform ( timeStep , predictedTrans ) ;
2019-01-03 14:26:51 +01:00
btScalar squareMotion = ( predictedTrans . getOrigin ( ) - body - > getWorldTransform ( ) . getOrigin ( ) ) . length2 ( ) ;
2017-08-01 14:30:58 +02:00
if ( getDispatchInfo ( ) . m_useContinuous & & body - > getCcdSquareMotionThreshold ( ) & & body - > getCcdSquareMotionThreshold ( ) < squareMotion )
{
BT_PROFILE ( " predictive convexSweepTest " ) ;
if ( body - > getCollisionShape ( ) - > isConvex ( ) )
{
gNumClampedCcdMotions + + ;
# ifdef PREDICTIVE_CONTACT_USE_STATIC_ONLY
class StaticOnlyCallback : public btClosestNotMeConvexResultCallback
{
public :
2019-01-03 14:26:51 +01:00
StaticOnlyCallback ( btCollisionObject * me , const btVector3 & fromA , const btVector3 & toA , btOverlappingPairCache * pairCache , btDispatcher * dispatcher ) : btClosestNotMeConvexResultCallback ( me , fromA , toA , pairCache , dispatcher )
2017-08-01 14:30:58 +02:00
{
}
2019-01-03 14:26:51 +01:00
virtual bool needsCollision ( btBroadphaseProxy * proxy0 ) const
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
btCollisionObject * otherObj = ( btCollisionObject * ) proxy0 - > m_clientObject ;
2017-08-01 14:30:58 +02:00
if ( ! otherObj - > isStaticOrKinematicObject ( ) )
return false ;
return btClosestNotMeConvexResultCallback : : needsCollision ( proxy0 ) ;
}
} ;
2019-01-03 14:26:51 +01:00
StaticOnlyCallback sweepResults ( body , body - > getWorldTransform ( ) . getOrigin ( ) , predictedTrans . getOrigin ( ) , getBroadphase ( ) - > getOverlappingPairCache ( ) , getDispatcher ( ) ) ;
2017-08-01 14:30:58 +02:00
# else
2019-01-03 14:26:51 +01:00
btClosestNotMeConvexResultCallback sweepResults ( body , body - > getWorldTransform ( ) . getOrigin ( ) , predictedTrans . getOrigin ( ) , getBroadphase ( ) - > getOverlappingPairCache ( ) , getDispatcher ( ) ) ;
2017-08-01 14:30:58 +02:00
# endif
//btConvexShape* convexShape = static_cast<btConvexShape*>(body->getCollisionShape());
2019-01-03 14:26:51 +01:00
btSphereShape tmpSphere ( body - > getCcdSweptSphereRadius ( ) ) ; //btConvexShape* convexShape = static_cast<btConvexShape*>(body->getCollisionShape());
sweepResults . m_allowedPenetration = getDispatchInfo ( ) . m_allowedCcdPenetration ;
2017-08-01 14:30:58 +02:00
sweepResults . m_collisionFilterGroup = body - > getBroadphaseProxy ( ) - > m_collisionFilterGroup ;
2019-01-03 14:26:51 +01:00
sweepResults . m_collisionFilterMask = body - > getBroadphaseProxy ( ) - > m_collisionFilterMask ;
2017-08-01 14:30:58 +02:00
btTransform modifiedPredictedTrans = predictedTrans ;
modifiedPredictedTrans . setBasis ( body - > getWorldTransform ( ) . getBasis ( ) ) ;
2019-01-03 14:26:51 +01:00
convexSweepTest ( & tmpSphere , body - > getWorldTransform ( ) , modifiedPredictedTrans , sweepResults ) ;
2017-08-01 14:30:58 +02:00
if ( sweepResults . hasHit ( ) & & ( sweepResults . m_closestHitFraction < 1.f ) )
{
2019-01-03 14:26:51 +01:00
btVector3 distVec = ( predictedTrans . getOrigin ( ) - body - > getWorldTransform ( ) . getOrigin ( ) ) * sweepResults . m_closestHitFraction ;
2017-08-01 14:30:58 +02:00
btScalar distance = distVec . dot ( - sweepResults . m_hitNormalWorld ) ;
2019-01-03 14:26:51 +01:00
btPersistentManifold * manifold = m_dispatcher1 - > getNewManifold ( body , sweepResults . m_hitCollisionObject ) ;
btMutexLock ( & m_predictiveManifoldsMutex ) ;
2017-08-01 14:30:58 +02:00
m_predictiveManifolds . push_back ( manifold ) ;
2019-01-03 14:26:51 +01:00
btMutexUnlock ( & m_predictiveManifoldsMutex ) ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
btVector3 worldPointB = body - > getWorldTransform ( ) . getOrigin ( ) + distVec ;
btVector3 localPointB = sweepResults . m_hitCollisionObject - > getWorldTransform ( ) . inverse ( ) * worldPointB ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
btManifoldPoint newPoint ( btVector3 ( 0 , 0 , 0 ) , localPointB , sweepResults . m_hitNormalWorld , distance ) ;
2017-08-01 14:30:58 +02:00
bool isPredictive = true ;
int index = manifold - > addManifoldPoint ( newPoint , isPredictive ) ;
btManifoldPoint & pt = manifold - > getContactPoint ( index ) ;
pt . m_combinedRestitution = 0 ;
2019-01-03 14:26:51 +01:00
pt . m_combinedFriction = gCalculateCombinedFrictionCallback ( body , sweepResults . m_hitCollisionObject ) ;
2017-08-01 14:30:58 +02:00
pt . m_positionWorldOnA = body - > getWorldTransform ( ) . getOrigin ( ) ;
pt . m_positionWorldOnB = worldPointB ;
}
}
}
}
}
}
void btDiscreteDynamicsWorld : : releasePredictiveContacts ( )
{
2019-01-03 14:26:51 +01:00
BT_PROFILE ( " release predictive contact manifolds " ) ;
for ( int i = 0 ; i < m_predictiveManifolds . size ( ) ; i + + )
{
btPersistentManifold * manifold = m_predictiveManifolds [ i ] ;
this - > m_dispatcher1 - > releaseManifold ( manifold ) ;
}
m_predictiveManifolds . clear ( ) ;
2017-08-01 14:30:58 +02:00
}
void btDiscreteDynamicsWorld : : createPredictiveContacts ( btScalar timeStep )
{
BT_PROFILE ( " createPredictiveContacts " ) ;
2019-01-03 14:26:51 +01:00
releasePredictiveContacts ( ) ;
if ( m_nonStaticRigidBodies . size ( ) > 0 )
{
createPredictiveContactsInternal ( & m_nonStaticRigidBodies [ 0 ] , m_nonStaticRigidBodies . size ( ) , timeStep ) ;
}
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : integrateTransformsInternal ( btRigidBody * * bodies , int numBodies , btScalar timeStep )
2017-08-01 14:30:58 +02:00
{
btTransform predictedTrans ;
2019-01-03 14:26:51 +01:00
for ( int i = 0 ; i < numBodies ; i + + )
2017-08-01 14:30:58 +02:00
{
btRigidBody * body = bodies [ i ] ;
body - > setHitFraction ( 1.f ) ;
if ( body - > isActive ( ) & & ( ! body - > isStaticOrKinematicObject ( ) ) )
{
body - > predictIntegratedTransform ( timeStep , predictedTrans ) ;
2019-01-03 14:26:51 +01:00
btScalar squareMotion = ( predictedTrans . getOrigin ( ) - body - > getWorldTransform ( ) . getOrigin ( ) ) . length2 ( ) ;
2017-08-01 14:30:58 +02:00
if ( getDispatchInfo ( ) . m_useContinuous & & body - > getCcdSquareMotionThreshold ( ) & & body - > getCcdSquareMotionThreshold ( ) < squareMotion )
{
BT_PROFILE ( " CCD motion clamping " ) ;
if ( body - > getCollisionShape ( ) - > isConvex ( ) )
{
gNumClampedCcdMotions + + ;
# ifdef USE_STATIC_ONLY
class StaticOnlyCallback : public btClosestNotMeConvexResultCallback
{
public :
2019-01-03 14:26:51 +01:00
StaticOnlyCallback ( btCollisionObject * me , const btVector3 & fromA , const btVector3 & toA , btOverlappingPairCache * pairCache , btDispatcher * dispatcher ) : btClosestNotMeConvexResultCallback ( me , fromA , toA , pairCache , dispatcher )
2017-08-01 14:30:58 +02:00
{
}
2019-01-03 14:26:51 +01:00
virtual bool needsCollision ( btBroadphaseProxy * proxy0 ) const
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
btCollisionObject * otherObj = ( btCollisionObject * ) proxy0 - > m_clientObject ;
2017-08-01 14:30:58 +02:00
if ( ! otherObj - > isStaticOrKinematicObject ( ) )
return false ;
return btClosestNotMeConvexResultCallback : : needsCollision ( proxy0 ) ;
}
} ;
2019-01-03 14:26:51 +01:00
StaticOnlyCallback sweepResults ( body , body - > getWorldTransform ( ) . getOrigin ( ) , predictedTrans . getOrigin ( ) , getBroadphase ( ) - > getOverlappingPairCache ( ) , getDispatcher ( ) ) ;
2017-08-01 14:30:58 +02:00
# else
2019-01-03 14:26:51 +01:00
btClosestNotMeConvexResultCallback sweepResults ( body , body - > getWorldTransform ( ) . getOrigin ( ) , predictedTrans . getOrigin ( ) , getBroadphase ( ) - > getOverlappingPairCache ( ) , getDispatcher ( ) ) ;
2017-08-01 14:30:58 +02:00
# endif
//btConvexShape* convexShape = static_cast<btConvexShape*>(body->getCollisionShape());
2019-01-03 14:26:51 +01:00
btSphereShape tmpSphere ( body - > getCcdSweptSphereRadius ( ) ) ; //btConvexShape* convexShape = static_cast<btConvexShape*>(body->getCollisionShape());
sweepResults . m_allowedPenetration = getDispatchInfo ( ) . m_allowedCcdPenetration ;
2017-08-01 14:30:58 +02:00
sweepResults . m_collisionFilterGroup = body - > getBroadphaseProxy ( ) - > m_collisionFilterGroup ;
2019-01-03 14:26:51 +01:00
sweepResults . m_collisionFilterMask = body - > getBroadphaseProxy ( ) - > m_collisionFilterMask ;
2017-08-01 14:30:58 +02:00
btTransform modifiedPredictedTrans = predictedTrans ;
modifiedPredictedTrans . setBasis ( body - > getWorldTransform ( ) . getBasis ( ) ) ;
2019-01-03 14:26:51 +01:00
convexSweepTest ( & tmpSphere , body - > getWorldTransform ( ) , modifiedPredictedTrans , sweepResults ) ;
2017-08-01 14:30:58 +02:00
if ( sweepResults . hasHit ( ) & & ( sweepResults . m_closestHitFraction < 1.f ) )
{
//printf("clamped integration to hit fraction = %f\n",fraction);
body - > setHitFraction ( sweepResults . m_closestHitFraction ) ;
2019-01-03 14:26:51 +01:00
body - > predictIntegratedTransform ( timeStep * body - > getHitFraction ( ) , predictedTrans ) ;
2017-08-01 14:30:58 +02:00
body - > setHitFraction ( 0.f ) ;
2019-01-03 14:26:51 +01:00
body - > proceedToTransform ( predictedTrans ) ;
2017-08-01 14:30:58 +02:00
#if 0
btVector3 linVel = body - > getLinearVelocity ( ) ;
btScalar maxSpeed = body - > getCcdMotionThreshold ( ) / getSolverInfo ( ) . m_timeStep ;
btScalar maxSpeedSqr = maxSpeed * maxSpeed ;
if ( linVel . length2 ( ) > maxSpeedSqr )
{
linVel . normalize ( ) ;
linVel * = maxSpeed ;
body - > setLinearVelocity ( linVel ) ;
btScalar ms2 = body - > getLinearVelocity ( ) . length2 ( ) ;
body - > predictIntegratedTransform ( timeStep , predictedTrans ) ;
btScalar sm2 = ( predictedTrans . getOrigin ( ) - body - > getWorldTransform ( ) . getOrigin ( ) ) . length2 ( ) ;
btScalar smt = body - > getCcdSquareMotionThreshold ( ) ;
printf ( " sm2=%f \n " , sm2 ) ;
}
# else
//don't apply the collision response right now, it will happen next frame
//if you really need to, you can uncomment next 3 lines. Note that is uses zero restitution.
//btScalar appliedImpulse = 0.f;
//btScalar depth = 0.f;
//appliedImpulse = resolveSingleCollision(body,(btCollisionObject*)sweepResults.m_hitCollisionObject,sweepResults.m_hitPointWorld,sweepResults.m_hitNormalWorld,getSolverInfo(), depth);
# endif
2019-01-03 14:26:51 +01:00
continue ;
2017-08-01 14:30:58 +02:00
}
}
}
2019-01-03 14:26:51 +01:00
body - > proceedToTransform ( predictedTrans ) ;
2017-08-01 14:30:58 +02:00
}
}
}
void btDiscreteDynamicsWorld : : integrateTransforms ( btScalar timeStep )
{
BT_PROFILE ( " integrateTransforms " ) ;
2019-01-03 14:26:51 +01:00
if ( m_nonStaticRigidBodies . size ( ) > 0 )
{
integrateTransformsInternal ( & m_nonStaticRigidBodies [ 0 ] , m_nonStaticRigidBodies . size ( ) , timeStep ) ;
}
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
///this should probably be switched on by default, but it is not well tested yet
2017-08-01 14:30:58 +02:00
if ( m_applySpeculativeContactRestitution )
{
BT_PROFILE ( " apply speculative contact restitution " ) ;
2019-01-03 14:26:51 +01:00
for ( int i = 0 ; i < m_predictiveManifolds . size ( ) ; i + + )
2017-08-01 14:30:58 +02:00
{
btPersistentManifold * manifold = m_predictiveManifolds [ i ] ;
btRigidBody * body0 = btRigidBody : : upcast ( ( btCollisionObject * ) manifold - > getBody0 ( ) ) ;
btRigidBody * body1 = btRigidBody : : upcast ( ( btCollisionObject * ) manifold - > getBody1 ( ) ) ;
2019-01-03 14:26:51 +01:00
for ( int p = 0 ; p < manifold - > getNumContacts ( ) ; p + + )
2017-08-01 14:30:58 +02:00
{
const btManifoldPoint & pt = manifold - > getContactPoint ( p ) ;
btScalar combinedRestitution = gCalculateCombinedRestitutionCallback ( body0 , body1 ) ;
2019-01-03 14:26:51 +01:00
if ( combinedRestitution > 0 & & pt . m_appliedImpulse ! = 0.f )
2017-08-01 14:30:58 +02:00
//if (pt.getDistance()>0 && combinedRestitution>0 && pt.m_appliedImpulse != 0.f)
{
2019-01-03 14:26:51 +01:00
btVector3 imp = - pt . m_normalWorldOnB * pt . m_appliedImpulse * combinedRestitution ;
2017-08-01 14:30:58 +02:00
const btVector3 & pos1 = pt . getPositionWorldOnA ( ) ;
const btVector3 & pos2 = pt . getPositionWorldOnB ( ) ;
btVector3 rel_pos0 = pos1 - body0 - > getWorldTransform ( ) . getOrigin ( ) ;
btVector3 rel_pos1 = pos2 - body1 - > getWorldTransform ( ) . getOrigin ( ) ;
if ( body0 )
2019-01-03 14:26:51 +01:00
body0 - > applyImpulse ( imp , rel_pos0 ) ;
2017-08-01 14:30:58 +02:00
if ( body1 )
2019-01-03 14:26:51 +01:00
body1 - > applyImpulse ( - imp , rel_pos1 ) ;
2017-08-01 14:30:58 +02:00
}
}
}
}
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : predictUnconstraintMotion ( btScalar timeStep )
2017-08-01 14:30:58 +02:00
{
BT_PROFILE ( " predictUnconstraintMotion " ) ;
2019-01-03 14:26:51 +01:00
for ( int i = 0 ; i < m_nonStaticRigidBodies . size ( ) ; i + + )
2017-08-01 14:30:58 +02:00
{
btRigidBody * body = m_nonStaticRigidBodies [ i ] ;
if ( ! body - > isStaticOrKinematicObject ( ) )
{
//don't integrate/update velocities here, it happens in the constraint solver
body - > applyDamping ( timeStep ) ;
2019-01-03 14:26:51 +01:00
body - > predictIntegratedTransform ( timeStep , body - > getInterpolationWorldTransform ( ) ) ;
2017-08-01 14:30:58 +02:00
}
}
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : startProfiling ( btScalar timeStep )
2017-08-01 14:30:58 +02:00
{
( void ) timeStep ;
# ifndef BT_NO_PROFILE
CProfileManager : : Reset ( ) ;
2019-01-03 14:26:51 +01:00
# endif //BT_NO_PROFILE
2017-08-01 14:30:58 +02:00
}
void btDiscreteDynamicsWorld : : debugDrawConstraint ( btTypedConstraint * constraint )
{
bool drawFrames = ( getDebugDrawer ( ) - > getDebugMode ( ) & btIDebugDraw : : DBG_DrawConstraints ) ! = 0 ;
bool drawLimits = ( getDebugDrawer ( ) - > getDebugMode ( ) & btIDebugDraw : : DBG_DrawConstraintLimits ) ! = 0 ;
btScalar dbgDrawSize = constraint - > getDbgDrawSize ( ) ;
2019-01-03 14:26:51 +01:00
if ( dbgDrawSize < = btScalar ( 0.f ) )
2017-08-01 14:30:58 +02:00
{
return ;
}
2019-01-03 14:26:51 +01:00
switch ( constraint - > getConstraintType ( ) )
2017-08-01 14:30:58 +02:00
{
case POINT2POINT_CONSTRAINT_TYPE :
2019-01-03 14:26:51 +01:00
{
btPoint2PointConstraint * p2pC = ( btPoint2PointConstraint * ) constraint ;
btTransform tr ;
tr . setIdentity ( ) ;
btVector3 pivot = p2pC - > getPivotInA ( ) ;
pivot = p2pC - > getRigidBodyA ( ) . getCenterOfMassTransform ( ) * pivot ;
tr . setOrigin ( pivot ) ;
getDebugDrawer ( ) - > drawTransform ( tr , dbgDrawSize ) ;
// that ideally should draw the same frame
pivot = p2pC - > getPivotInB ( ) ;
pivot = p2pC - > getRigidBodyB ( ) . getCenterOfMassTransform ( ) * pivot ;
tr . setOrigin ( pivot ) ;
if ( drawFrames ) getDebugDrawer ( ) - > drawTransform ( tr , dbgDrawSize ) ;
}
break ;
case HINGE_CONSTRAINT_TYPE :
{
btHingeConstraint * pHinge = ( btHingeConstraint * ) constraint ;
btTransform tr = pHinge - > getRigidBodyA ( ) . getCenterOfMassTransform ( ) * pHinge - > getAFrame ( ) ;
if ( drawFrames ) getDebugDrawer ( ) - > drawTransform ( tr , dbgDrawSize ) ;
tr = pHinge - > getRigidBodyB ( ) . getCenterOfMassTransform ( ) * pHinge - > getBFrame ( ) ;
if ( drawFrames ) getDebugDrawer ( ) - > drawTransform ( tr , dbgDrawSize ) ;
btScalar minAng = pHinge - > getLowerLimit ( ) ;
btScalar maxAng = pHinge - > getUpperLimit ( ) ;
if ( minAng = = maxAng )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
break ;
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
bool drawSect = true ;
if ( ! pHinge - > hasLimit ( ) )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
minAng = btScalar ( 0.f ) ;
maxAng = SIMD_2_PI ;
drawSect = false ;
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
if ( drawLimits )
{
btVector3 & center = tr . getOrigin ( ) ;
btVector3 normal = tr . getBasis ( ) . getColumn ( 2 ) ;
btVector3 axis = tr . getBasis ( ) . getColumn ( 0 ) ;
getDebugDrawer ( ) - > drawArc ( center , normal , axis , dbgDrawSize , dbgDrawSize , minAng , maxAng , btVector3 ( 0 , 0 , 0 ) , drawSect ) ;
}
}
break ;
2017-08-01 14:30:58 +02:00
case CONETWIST_CONSTRAINT_TYPE :
2019-01-03 14:26:51 +01:00
{
btConeTwistConstraint * pCT = ( btConeTwistConstraint * ) constraint ;
btTransform tr = pCT - > getRigidBodyA ( ) . getCenterOfMassTransform ( ) * pCT - > getAFrame ( ) ;
if ( drawFrames ) getDebugDrawer ( ) - > drawTransform ( tr , dbgDrawSize ) ;
tr = pCT - > getRigidBodyB ( ) . getCenterOfMassTransform ( ) * pCT - > getBFrame ( ) ;
if ( drawFrames ) getDebugDrawer ( ) - > drawTransform ( tr , dbgDrawSize ) ;
if ( drawLimits )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
//const btScalar length = btScalar(5);
const btScalar length = dbgDrawSize ;
static int nSegments = 8 * 4 ;
btScalar fAngleInRadians = btScalar ( 2. * 3.1415926 ) * ( btScalar ) ( nSegments - 1 ) / btScalar ( nSegments ) ;
btVector3 pPrev = pCT - > GetPointForAngle ( fAngleInRadians , length ) ;
pPrev = tr * pPrev ;
for ( int i = 0 ; i < nSegments ; i + + )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
fAngleInRadians = btScalar ( 2. * 3.1415926 ) * ( btScalar ) i / btScalar ( nSegments ) ;
btVector3 pCur = pCT - > GetPointForAngle ( fAngleInRadians , length ) ;
pCur = tr * pCur ;
getDebugDrawer ( ) - > drawLine ( pPrev , pCur , btVector3 ( 0 , 0 , 0 ) ) ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
if ( i % ( nSegments / 8 ) = = 0 )
getDebugDrawer ( ) - > drawLine ( tr . getOrigin ( ) , pCur , btVector3 ( 0 , 0 , 0 ) ) ;
2017-08-01 14:30:58 +02:00
2019-01-03 14:26:51 +01:00
pPrev = pCur ;
}
btScalar tws = pCT - > getTwistSpan ( ) ;
btScalar twa = pCT - > getTwistAngle ( ) ;
bool useFrameB = ( pCT - > getRigidBodyB ( ) . getInvMass ( ) > btScalar ( 0.f ) ) ;
if ( useFrameB )
{
tr = pCT - > getRigidBodyB ( ) . getCenterOfMassTransform ( ) * pCT - > getBFrame ( ) ;
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
else
{
tr = pCT - > getRigidBodyA ( ) . getCenterOfMassTransform ( ) * pCT - > getAFrame ( ) ;
}
btVector3 pivot = tr . getOrigin ( ) ;
btVector3 normal = tr . getBasis ( ) . getColumn ( 0 ) ;
btVector3 axis1 = tr . getBasis ( ) . getColumn ( 1 ) ;
getDebugDrawer ( ) - > drawArc ( pivot , normal , axis1 , dbgDrawSize , dbgDrawSize , - twa - tws , - twa + tws , btVector3 ( 0 , 0 , 0 ) , true ) ;
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
}
break ;
2017-08-01 14:30:58 +02:00
case D6_SPRING_CONSTRAINT_TYPE :
case D6_CONSTRAINT_TYPE :
2019-01-03 14:26:51 +01:00
{
btGeneric6DofConstraint * p6DOF = ( btGeneric6DofConstraint * ) constraint ;
btTransform tr = p6DOF - > getCalculatedTransformA ( ) ;
if ( drawFrames ) getDebugDrawer ( ) - > drawTransform ( tr , dbgDrawSize ) ;
tr = p6DOF - > getCalculatedTransformB ( ) ;
if ( drawFrames ) getDebugDrawer ( ) - > drawTransform ( tr , dbgDrawSize ) ;
if ( drawLimits )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
tr = p6DOF - > getCalculatedTransformA ( ) ;
const btVector3 & center = p6DOF - > getCalculatedTransformB ( ) . getOrigin ( ) ;
btVector3 up = tr . getBasis ( ) . getColumn ( 2 ) ;
btVector3 axis = tr . getBasis ( ) . getColumn ( 0 ) ;
btScalar minTh = p6DOF - > getRotationalLimitMotor ( 1 ) - > m_loLimit ;
btScalar maxTh = p6DOF - > getRotationalLimitMotor ( 1 ) - > m_hiLimit ;
btScalar minPs = p6DOF - > getRotationalLimitMotor ( 2 ) - > m_loLimit ;
btScalar maxPs = p6DOF - > getRotationalLimitMotor ( 2 ) - > m_hiLimit ;
getDebugDrawer ( ) - > drawSpherePatch ( center , up , axis , dbgDrawSize * btScalar ( .9f ) , minTh , maxTh , minPs , maxPs , btVector3 ( 0 , 0 , 0 ) ) ;
axis = tr . getBasis ( ) . getColumn ( 1 ) ;
btScalar ay = p6DOF - > getAngle ( 1 ) ;
btScalar az = p6DOF - > getAngle ( 2 ) ;
btScalar cy = btCos ( ay ) ;
btScalar sy = btSin ( ay ) ;
btScalar cz = btCos ( az ) ;
btScalar sz = btSin ( az ) ;
btVector3 ref ;
ref [ 0 ] = cy * cz * axis [ 0 ] + cy * sz * axis [ 1 ] - sy * axis [ 2 ] ;
ref [ 1 ] = - sz * axis [ 0 ] + cz * axis [ 1 ] ;
ref [ 2 ] = cz * sy * axis [ 0 ] + sz * sy * axis [ 1 ] + cy * axis [ 2 ] ;
2017-08-01 14:30:58 +02:00
tr = p6DOF - > getCalculatedTransformB ( ) ;
2019-01-03 14:26:51 +01:00
btVector3 normal = - tr . getBasis ( ) . getColumn ( 0 ) ;
btScalar minFi = p6DOF - > getRotationalLimitMotor ( 0 ) - > m_loLimit ;
btScalar maxFi = p6DOF - > getRotationalLimitMotor ( 0 ) - > m_hiLimit ;
if ( minFi > maxFi )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
getDebugDrawer ( ) - > drawArc ( center , normal , ref , dbgDrawSize , dbgDrawSize , - SIMD_PI , SIMD_PI , btVector3 ( 0 , 0 , 0 ) , false ) ;
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
else if ( minFi < maxFi )
{
getDebugDrawer ( ) - > drawArc ( center , normal , ref , dbgDrawSize , dbgDrawSize , minFi , maxFi , btVector3 ( 0 , 0 , 0 ) , true ) ;
}
tr = p6DOF - > getCalculatedTransformA ( ) ;
btVector3 bbMin = p6DOF - > getTranslationalLimitMotor ( ) - > m_lowerLimit ;
btVector3 bbMax = p6DOF - > getTranslationalLimitMotor ( ) - > m_upperLimit ;
getDebugDrawer ( ) - > drawBox ( bbMin , bbMax , tr , btVector3 ( 0 , 0 , 0 ) ) ;
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
}
break ;
2017-08-01 14:30:58 +02:00
///note: the code for D6_SPRING_2_CONSTRAINT_TYPE is identical to D6_CONSTRAINT_TYPE, the D6_CONSTRAINT_TYPE+D6_SPRING_CONSTRAINT_TYPE will likely become obsolete/deprecated at some stage
case D6_SPRING_2_CONSTRAINT_TYPE :
{
{
btGeneric6DofSpring2Constraint * p6DOF = ( btGeneric6DofSpring2Constraint * ) constraint ;
btTransform tr = p6DOF - > getCalculatedTransformA ( ) ;
if ( drawFrames ) getDebugDrawer ( ) - > drawTransform ( tr , dbgDrawSize ) ;
tr = p6DOF - > getCalculatedTransformB ( ) ;
if ( drawFrames ) getDebugDrawer ( ) - > drawTransform ( tr , dbgDrawSize ) ;
if ( drawLimits )
{
tr = p6DOF - > getCalculatedTransformA ( ) ;
const btVector3 & center = p6DOF - > getCalculatedTransformB ( ) . getOrigin ( ) ;
btVector3 up = tr . getBasis ( ) . getColumn ( 2 ) ;
btVector3 axis = tr . getBasis ( ) . getColumn ( 0 ) ;
btScalar minTh = p6DOF - > getRotationalLimitMotor ( 1 ) - > m_loLimit ;
btScalar maxTh = p6DOF - > getRotationalLimitMotor ( 1 ) - > m_hiLimit ;
2018-09-07 16:11:04 +02:00
if ( minTh < = maxTh )
{
btScalar minPs = p6DOF - > getRotationalLimitMotor ( 2 ) - > m_loLimit ;
btScalar maxPs = p6DOF - > getRotationalLimitMotor ( 2 ) - > m_hiLimit ;
getDebugDrawer ( ) - > drawSpherePatch ( center , up , axis , dbgDrawSize * btScalar ( .9f ) , minTh , maxTh , minPs , maxPs , btVector3 ( 0 , 0 , 0 ) ) ;
}
2017-08-01 14:30:58 +02:00
axis = tr . getBasis ( ) . getColumn ( 1 ) ;
btScalar ay = p6DOF - > getAngle ( 1 ) ;
btScalar az = p6DOF - > getAngle ( 2 ) ;
btScalar cy = btCos ( ay ) ;
btScalar sy = btSin ( ay ) ;
btScalar cz = btCos ( az ) ;
btScalar sz = btSin ( az ) ;
btVector3 ref ;
2019-01-03 14:26:51 +01:00
ref [ 0 ] = cy * cz * axis [ 0 ] + cy * sz * axis [ 1 ] - sy * axis [ 2 ] ;
ref [ 1 ] = - sz * axis [ 0 ] + cz * axis [ 1 ] ;
ref [ 2 ] = cz * sy * axis [ 0 ] + sz * sy * axis [ 1 ] + cy * axis [ 2 ] ;
2017-08-01 14:30:58 +02:00
tr = p6DOF - > getCalculatedTransformB ( ) ;
btVector3 normal = - tr . getBasis ( ) . getColumn ( 0 ) ;
btScalar minFi = p6DOF - > getRotationalLimitMotor ( 0 ) - > m_loLimit ;
btScalar maxFi = p6DOF - > getRotationalLimitMotor ( 0 ) - > m_hiLimit ;
if ( minFi > maxFi )
{
getDebugDrawer ( ) - > drawArc ( center , normal , ref , dbgDrawSize , dbgDrawSize , - SIMD_PI , SIMD_PI , btVector3 ( 0 , 0 , 0 ) , false ) ;
}
else if ( minFi < maxFi )
{
getDebugDrawer ( ) - > drawArc ( center , normal , ref , dbgDrawSize , dbgDrawSize , minFi , maxFi , btVector3 ( 0 , 0 , 0 ) , true ) ;
}
tr = p6DOF - > getCalculatedTransformA ( ) ;
btVector3 bbMin = p6DOF - > getTranslationalLimitMotor ( ) - > m_lowerLimit ;
btVector3 bbMax = p6DOF - > getTranslationalLimitMotor ( ) - > m_upperLimit ;
getDebugDrawer ( ) - > drawBox ( bbMin , bbMax , tr , btVector3 ( 0 , 0 , 0 ) ) ;
}
}
break ;
}
case SLIDER_CONSTRAINT_TYPE :
2019-01-03 14:26:51 +01:00
{
btSliderConstraint * pSlider = ( btSliderConstraint * ) constraint ;
btTransform tr = pSlider - > getCalculatedTransformA ( ) ;
if ( drawFrames ) getDebugDrawer ( ) - > drawTransform ( tr , dbgDrawSize ) ;
tr = pSlider - > getCalculatedTransformB ( ) ;
if ( drawFrames ) getDebugDrawer ( ) - > drawTransform ( tr , dbgDrawSize ) ;
if ( drawLimits )
2017-08-01 14:30:58 +02:00
{
2019-01-03 14:26:51 +01:00
btTransform tr = pSlider - > getUseLinearReferenceFrameA ( ) ? pSlider - > getCalculatedTransformA ( ) : pSlider - > getCalculatedTransformB ( ) ;
btVector3 li_min = tr * btVector3 ( pSlider - > getLowerLinLimit ( ) , 0.f , 0.f ) ;
btVector3 li_max = tr * btVector3 ( pSlider - > getUpperLinLimit ( ) , 0.f , 0.f ) ;
getDebugDrawer ( ) - > drawLine ( li_min , li_max , btVector3 ( 0 , 0 , 0 ) ) ;
btVector3 normal = tr . getBasis ( ) . getColumn ( 0 ) ;
btVector3 axis = tr . getBasis ( ) . getColumn ( 1 ) ;
btScalar a_min = pSlider - > getLowerAngLimit ( ) ;
btScalar a_max = pSlider - > getUpperAngLimit ( ) ;
const btVector3 & center = pSlider - > getCalculatedTransformB ( ) . getOrigin ( ) ;
getDebugDrawer ( ) - > drawArc ( center , normal , axis , dbgDrawSize , dbgDrawSize , a_min , a_max , btVector3 ( 0 , 0 , 0 ) , true ) ;
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
}
break ;
default :
2017-08-01 14:30:58 +02:00
break ;
}
return ;
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : setConstraintSolver ( btConstraintSolver * solver )
2017-08-01 14:30:58 +02:00
{
if ( m_ownsConstraintSolver )
{
2019-01-03 14:26:51 +01:00
btAlignedFree ( m_constraintSolver ) ;
2017-08-01 14:30:58 +02:00
}
m_ownsConstraintSolver = false ;
m_constraintSolver = solver ;
m_solverIslandCallback - > m_solver = solver ;
}
btConstraintSolver * btDiscreteDynamicsWorld : : getConstraintSolver ( )
{
return m_constraintSolver ;
}
2019-01-03 14:26:51 +01:00
int btDiscreteDynamicsWorld : : getNumConstraints ( ) const
2017-08-01 14:30:58 +02:00
{
return int ( m_constraints . size ( ) ) ;
}
btTypedConstraint * btDiscreteDynamicsWorld : : getConstraint ( int index )
{
return m_constraints [ index ] ;
}
const btTypedConstraint * btDiscreteDynamicsWorld : : getConstraint ( int index ) const
{
return m_constraints [ index ] ;
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : serializeRigidBodies ( btSerializer * serializer )
2017-08-01 14:30:58 +02:00
{
int i ;
//serialize all collision objects
2019-01-03 14:26:51 +01:00
for ( i = 0 ; i < m_collisionObjects . size ( ) ; i + + )
2017-08-01 14:30:58 +02:00
{
btCollisionObject * colObj = m_collisionObjects [ i ] ;
if ( colObj - > getInternalType ( ) & btCollisionObject : : CO_RIGID_BODY )
{
int len = colObj - > calculateSerializeBufferSize ( ) ;
2019-01-03 14:26:51 +01:00
btChunk * chunk = serializer - > allocate ( len , 1 ) ;
2017-08-01 14:30:58 +02:00
const char * structType = colObj - > serialize ( chunk - > m_oldPtr , serializer ) ;
2019-01-03 14:26:51 +01:00
serializer - > finalizeChunk ( chunk , structType , BT_RIGIDBODY_CODE , colObj ) ;
2017-08-01 14:30:58 +02:00
}
}
2019-01-03 14:26:51 +01:00
for ( i = 0 ; i < m_constraints . size ( ) ; i + + )
2017-08-01 14:30:58 +02:00
{
btTypedConstraint * constraint = m_constraints [ i ] ;
int size = constraint - > calculateSerializeBufferSize ( ) ;
2019-01-03 14:26:51 +01:00
btChunk * chunk = serializer - > allocate ( size , 1 ) ;
const char * structType = constraint - > serialize ( chunk - > m_oldPtr , serializer ) ;
serializer - > finalizeChunk ( chunk , structType , BT_CONSTRAINT_CODE , constraint ) ;
2017-08-01 14:30:58 +02:00
}
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : serializeDynamicsWorldInfo ( btSerializer * serializer )
2017-08-01 14:30:58 +02:00
{
# ifdef BT_USE_DOUBLE_PRECISION
2019-01-03 14:26:51 +01:00
int len = sizeof ( btDynamicsWorldDoubleData ) ;
btChunk * chunk = serializer - > allocate ( len , 1 ) ;
btDynamicsWorldDoubleData * worldInfo = ( btDynamicsWorldDoubleData * ) chunk - > m_oldPtr ;
# else //BT_USE_DOUBLE_PRECISION
int len = sizeof ( btDynamicsWorldFloatData ) ;
btChunk * chunk = serializer - > allocate ( len , 1 ) ;
btDynamicsWorldFloatData * worldInfo = ( btDynamicsWorldFloatData * ) chunk - > m_oldPtr ;
# endif //BT_USE_DOUBLE_PRECISION
memset ( worldInfo , 0x00 , len ) ;
m_gravity . serialize ( worldInfo - > m_gravity ) ;
worldInfo - > m_solverInfo . m_tau = getSolverInfo ( ) . m_tau ;
worldInfo - > m_solverInfo . m_damping = getSolverInfo ( ) . m_damping ;
worldInfo - > m_solverInfo . m_friction = getSolverInfo ( ) . m_friction ;
worldInfo - > m_solverInfo . m_timeStep = getSolverInfo ( ) . m_timeStep ;
worldInfo - > m_solverInfo . m_restitution = getSolverInfo ( ) . m_restitution ;
worldInfo - > m_solverInfo . m_maxErrorReduction = getSolverInfo ( ) . m_maxErrorReduction ;
worldInfo - > m_solverInfo . m_sor = getSolverInfo ( ) . m_sor ;
worldInfo - > m_solverInfo . m_erp = getSolverInfo ( ) . m_erp ;
worldInfo - > m_solverInfo . m_erp2 = getSolverInfo ( ) . m_erp2 ;
worldInfo - > m_solverInfo . m_globalCfm = getSolverInfo ( ) . m_globalCfm ;
worldInfo - > m_solverInfo . m_splitImpulsePenetrationThreshold = getSolverInfo ( ) . m_splitImpulsePenetrationThreshold ;
worldInfo - > m_solverInfo . m_splitImpulseTurnErp = getSolverInfo ( ) . m_splitImpulseTurnErp ;
worldInfo - > m_solverInfo . m_linearSlop = getSolverInfo ( ) . m_linearSlop ;
worldInfo - > m_solverInfo . m_warmstartingFactor = getSolverInfo ( ) . m_warmstartingFactor ;
worldInfo - > m_solverInfo . m_maxGyroscopicForce = getSolverInfo ( ) . m_maxGyroscopicForce ;
worldInfo - > m_solverInfo . m_singleAxisRollingFrictionThreshold = getSolverInfo ( ) . m_singleAxisRollingFrictionThreshold ;
worldInfo - > m_solverInfo . m_numIterations = getSolverInfo ( ) . m_numIterations ;
worldInfo - > m_solverInfo . m_solverMode = getSolverInfo ( ) . m_solverMode ;
worldInfo - > m_solverInfo . m_restingContactRestitutionThreshold = getSolverInfo ( ) . m_restingContactRestitutionThreshold ;
worldInfo - > m_solverInfo . m_minimumSolverBatchSize = getSolverInfo ( ) . m_minimumSolverBatchSize ;
worldInfo - > m_solverInfo . m_splitImpulse = getSolverInfo ( ) . m_splitImpulse ;
2020-01-08 18:05:43 +01:00
2017-08-01 14:30:58 +02:00
# ifdef BT_USE_DOUBLE_PRECISION
2019-01-03 14:26:51 +01:00
const char * structType = " btDynamicsWorldDoubleData " ;
# else //BT_USE_DOUBLE_PRECISION
const char * structType = " btDynamicsWorldFloatData " ;
# endif //BT_USE_DOUBLE_PRECISION
serializer - > finalizeChunk ( chunk , structType , BT_DYNAMICSWORLD_CODE , worldInfo ) ;
2017-08-01 14:30:58 +02:00
}
2019-01-03 14:26:51 +01:00
void btDiscreteDynamicsWorld : : serialize ( btSerializer * serializer )
2017-08-01 14:30:58 +02:00
{
serializer - > startSerialization ( ) ;
serializeDynamicsWorldInfo ( serializer ) ;
serializeCollisionObjects ( serializer ) ;
serializeRigidBodies ( serializer ) ;
2018-09-07 16:11:04 +02:00
serializeContactManifolds ( serializer ) ;
2017-08-01 14:30:58 +02:00
serializer - > finishSerialization ( ) ;
}