2005-07-16 09:58:01 +00:00
|
|
|
#include "ToiContactDispatcher.h"
|
|
|
|
#include "ConstraintSolver/ConstraintSolver.h"
|
|
|
|
#include "Dynamics/RigidBody.h"
|
|
|
|
#include "BroadphaseCollision/CollisionAlgorithm.h"
|
|
|
|
#include "ConstraintSolver/ContactSolverInfo.h"
|
|
|
|
#include "CollisionDispatch/ConvexConvexAlgorithm.h"
|
|
|
|
#include "CollisionDispatch/EmptyCollisionAlgorithm.h"
|
|
|
|
#include "CollisionDispatch/ConvexConcaveCollisionAlgorithm.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ToiContactDispatcher::FindUnions()
|
|
|
|
{
|
|
|
|
if (m_useIslands)
|
|
|
|
{
|
|
|
|
for (int i=0;i<GetNumManifolds();i++)
|
|
|
|
{
|
|
|
|
const PersistentManifold* manifold = this->GetManifoldByIndexInternal(i);
|
|
|
|
//static objects (invmass 0.f) don't merge !
|
|
|
|
if ((((RigidBody*)manifold->GetBody0()) && (((RigidBody*)manifold->GetBody0())->mergesSimulationIslands())) &&
|
|
|
|
(((RigidBody*)manifold->GetBody1()) && (((RigidBody*)manifold->GetBody1())->mergesSimulationIslands())))
|
|
|
|
{
|
|
|
|
|
|
|
|
m_unionFind.unite(((RigidBody*)manifold->GetBody0())->m_islandTag1,
|
|
|
|
((RigidBody*)manifold->GetBody1())->m_islandTag1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ToiContactDispatcher::ToiContactDispatcher (ConstraintSolver* solver):
|
|
|
|
m_useIslands(true),
|
|
|
|
m_unionFind(MAX_RIGIDBODIES),
|
|
|
|
m_solver(solver),
|
2005-08-03 18:22:30 +00:00
|
|
|
m_count(0),
|
|
|
|
m_sor(1.3f),
|
|
|
|
m_tau(0.4f),
|
|
|
|
m_damping(0.9f)
|
2005-07-16 09:58:01 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0;i<MAX_BROADPHASE_COLLISION_TYPES;i++)
|
|
|
|
{
|
|
|
|
for (int j=0;j<MAX_BROADPHASE_COLLISION_TYPES;j++)
|
|
|
|
{
|
|
|
|
m_doubleDispatch[i][j] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
PersistentManifold* ToiContactDispatcher::GetNewManifold(void* b0,void* b1)
|
|
|
|
{
|
|
|
|
|
|
|
|
RigidBody* body0 = (RigidBody*)b0;
|
|
|
|
RigidBody* body1 = (RigidBody*)b1;
|
|
|
|
|
|
|
|
PersistentManifold* manifold = new PersistentManifold (body0,body1);
|
|
|
|
m_manifoldsPtr.push_back(manifold);
|
|
|
|
|
|
|
|
return manifold;
|
|
|
|
}
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
void ToiContactDispatcher::ReleaseManifold(PersistentManifold* manifold)
|
|
|
|
{
|
|
|
|
manifold->ClearManifold();
|
|
|
|
|
|
|
|
std::vector<PersistentManifold*>::iterator i =
|
|
|
|
std::find(m_manifoldsPtr.begin(), m_manifoldsPtr.end(), manifold);
|
|
|
|
if (!(i == m_manifoldsPtr.end()))
|
|
|
|
{
|
|
|
|
std::swap(*i, m_manifoldsPtr.back());
|
|
|
|
m_manifoldsPtr.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// todo: this is random access, it can be walked 'cache friendly'!
|
|
|
|
//
|
2005-07-29 18:14:41 +00:00
|
|
|
void ToiContactDispatcher::SolveConstraints(float timeStep, int numIterations,int numRigidBodies,IDebugDraw* debugDrawer)
|
2005-07-16 09:58:01 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
|
|
for (int islandId=0;islandId<numRigidBodies;islandId++)
|
|
|
|
{
|
|
|
|
|
2005-07-29 18:14:41 +00:00
|
|
|
std::vector<PersistentManifold*> islandmanifold;
|
2005-07-16 09:58:01 +00:00
|
|
|
|
|
|
|
//int numSleeping = 0;
|
|
|
|
|
|
|
|
bool allSleeping = true;
|
|
|
|
|
|
|
|
for (i=0;i<GetNumManifolds();i++)
|
|
|
|
{
|
|
|
|
PersistentManifold* manifold = this->GetManifoldByIndexInternal(i);
|
|
|
|
if ((((RigidBody*)manifold->GetBody0()) && ((RigidBody*)manifold->GetBody0())->m_islandTag1 == (islandId)) ||
|
|
|
|
(((RigidBody*)manifold->GetBody1()) && ((RigidBody*)manifold->GetBody1())->m_islandTag1 == (islandId)))
|
|
|
|
{
|
|
|
|
|
|
|
|
if ((((RigidBody*)manifold->GetBody0()) && ((RigidBody*)manifold->GetBody0())->GetActivationState()== ACTIVE_TAG) ||
|
|
|
|
(((RigidBody*)manifold->GetBody1()) && ((RigidBody*)manifold->GetBody1())->GetActivationState() == ACTIVE_TAG))
|
|
|
|
{
|
|
|
|
allSleeping = false;
|
|
|
|
}
|
|
|
|
|
2005-07-29 18:14:41 +00:00
|
|
|
islandmanifold.push_back(manifold);
|
2005-07-16 09:58:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (allSleeping)
|
|
|
|
{
|
|
|
|
//tag all as 'ISLAND_SLEEPING'
|
2005-07-29 18:14:41 +00:00
|
|
|
for (i=0;i<islandmanifold.size();i++)
|
2005-07-16 09:58:01 +00:00
|
|
|
{
|
2005-07-29 18:14:41 +00:00
|
|
|
PersistentManifold* manifold = islandmanifold[i];
|
2005-07-16 09:58:01 +00:00
|
|
|
if (((RigidBody*)manifold->GetBody0()))
|
|
|
|
{
|
|
|
|
((RigidBody*)manifold->GetBody0())->SetActivationState( ISLAND_SLEEPING );
|
|
|
|
}
|
|
|
|
if (((RigidBody*)manifold->GetBody1()))
|
|
|
|
{
|
|
|
|
((RigidBody*)manifold->GetBody1())->SetActivationState( ISLAND_SLEEPING);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
|
|
|
|
//tag all as 'ISLAND_SLEEPING'
|
2005-07-29 18:14:41 +00:00
|
|
|
for (i=0;i<islandmanifold.size();i++)
|
2005-07-16 09:58:01 +00:00
|
|
|
{
|
2005-07-29 18:14:41 +00:00
|
|
|
PersistentManifold* manifold = islandmanifold[i];
|
2005-08-04 19:07:39 +00:00
|
|
|
RigidBody* body0 = (RigidBody*)manifold->GetBody0();
|
|
|
|
RigidBody* body1 = (RigidBody*)manifold->GetBody1();
|
|
|
|
|
|
|
|
if (body0)
|
2005-07-16 09:58:01 +00:00
|
|
|
{
|
2005-08-04 19:07:39 +00:00
|
|
|
if ( body0->GetActivationState() == ISLAND_SLEEPING)
|
2005-07-16 09:58:01 +00:00
|
|
|
{
|
2005-08-04 19:07:39 +00:00
|
|
|
body0->SetActivationState( WANTS_DEACTIVATION);
|
2005-07-16 09:58:01 +00:00
|
|
|
}
|
|
|
|
}
|
2005-08-04 19:07:39 +00:00
|
|
|
if (body1)
|
2005-07-16 09:58:01 +00:00
|
|
|
{
|
2005-08-04 19:07:39 +00:00
|
|
|
if ( body1->GetActivationState() == ISLAND_SLEEPING)
|
2005-07-16 09:58:01 +00:00
|
|
|
{
|
2005-08-04 19:07:39 +00:00
|
|
|
body1->SetActivationState(WANTS_DEACTIVATION);
|
2005-07-16 09:58:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
///This island solving can all be scheduled in parallel
|
|
|
|
ContactSolverInfo info;
|
|
|
|
info.m_friction = 0.9f;
|
|
|
|
info.m_numIterations = numIterations;
|
|
|
|
info.m_timeStep = timeStep;
|
2005-08-03 18:22:30 +00:00
|
|
|
|
2005-07-30 18:10:53 +00:00
|
|
|
info.m_restitution = 0.0f;//m_restitution;
|
2005-08-03 18:22:30 +00:00
|
|
|
|
|
|
|
info.m_sor = m_sor;
|
|
|
|
info.m_tau = m_tau;
|
|
|
|
info.m_damping = m_damping;
|
2005-07-16 09:58:01 +00:00
|
|
|
|
2005-07-29 18:14:41 +00:00
|
|
|
m_solver->SolveGroup( &islandmanifold[0], islandmanifold.size(),info,debugDrawer );
|
2005-07-16 09:58:01 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CollisionAlgorithm* ToiContactDispatcher::InternalFindAlgorithm(BroadphaseProxy& proxy0,BroadphaseProxy& proxy1)
|
|
|
|
{
|
|
|
|
m_count++;
|
|
|
|
|
|
|
|
CollisionAlgorithmConstructionInfo ci;
|
|
|
|
ci.m_dispatcher = this;
|
|
|
|
|
|
|
|
if (proxy0.IsConvexShape() && proxy1.IsConvexShape() )
|
|
|
|
{
|
|
|
|
return new ConvexConvexAlgorithm(0,ci,&proxy0,&proxy1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (proxy0.IsConvexShape() && proxy1.IsConcaveShape())
|
|
|
|
{
|
|
|
|
return new ConvexConcaveCollisionAlgorithm(ci,&proxy0,&proxy1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (proxy1.IsConvexShape() && proxy0.IsConcaveShape())
|
|
|
|
{
|
|
|
|
return new ConvexConcaveCollisionAlgorithm(ci,&proxy1,&proxy0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//failed to find an algorithm
|
|
|
|
return new EmptyAlgorithm(ci);
|
|
|
|
|
|
|
|
}
|