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 "btDiscreteDynamicsWorldMt.h"
|
|
|
|
|
|
|
|
//collision detection
|
|
|
|
#include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
|
|
|
|
#include "BulletCollision/BroadphaseCollision/btSimpleBroadphase.h"
|
|
|
|
#include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h"
|
|
|
|
#include "BulletCollision/CollisionShapes/btCollisionShape.h"
|
|
|
|
#include "btSimulationIslandManagerMt.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"
|
|
|
|
|
|
|
|
///
|
|
|
|
/// btConstraintSolverPoolMt
|
|
|
|
///
|
|
|
|
|
|
|
|
btConstraintSolverPoolMt::ThreadSolver* btConstraintSolverPoolMt::getAndLockThreadSolver()
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
int i = 0;
|
2017-08-01 14:30:58 +02:00
|
|
|
#if BT_THREADSAFE
|
2019-01-03 14:26:51 +01:00
|
|
|
i = btGetCurrentThreadIndex() % m_solvers.size();
|
|
|
|
#endif // #if BT_THREADSAFE
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
ThreadSolver& solver = m_solvers[i];
|
|
|
|
if (solver.mutex.tryLock())
|
|
|
|
{
|
|
|
|
return &solver;
|
|
|
|
}
|
|
|
|
// failed, try the next one
|
|
|
|
i = (i + 1) % m_solvers.size();
|
|
|
|
}
|
|
|
|
return NULL;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btConstraintSolverPoolMt::init(btConstraintSolver** solvers, int numSolvers)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
m_solverType = BT_SEQUENTIAL_IMPULSE_SOLVER;
|
|
|
|
m_solvers.resize(numSolvers);
|
|
|
|
for (int i = 0; i < numSolvers; ++i)
|
|
|
|
{
|
|
|
|
m_solvers[i].solver = solvers[i];
|
|
|
|
}
|
|
|
|
if (numSolvers > 0)
|
|
|
|
{
|
|
|
|
m_solverType = solvers[0]->getSolverType();
|
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// create the solvers for me
|
2019-01-03 14:26:51 +01:00
|
|
|
btConstraintSolverPoolMt::btConstraintSolverPoolMt(int numSolvers)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btAlignedObjectArray<btConstraintSolver*> solvers;
|
|
|
|
solvers.reserve(numSolvers);
|
|
|
|
for (int i = 0; i < numSolvers; ++i)
|
|
|
|
{
|
|
|
|
btConstraintSolver* solver = new btSequentialImpulseConstraintSolver();
|
|
|
|
solvers.push_back(solver);
|
|
|
|
}
|
|
|
|
init(&solvers[0], numSolvers);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// pass in fully constructed solvers (destructor will delete them)
|
2019-01-03 14:26:51 +01:00
|
|
|
btConstraintSolverPoolMt::btConstraintSolverPoolMt(btConstraintSolver** solvers, int numSolvers)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
init(solvers, numSolvers);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
btConstraintSolverPoolMt::~btConstraintSolverPoolMt()
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
// delete all solvers
|
|
|
|
for (int i = 0; i < m_solvers.size(); ++i)
|
|
|
|
{
|
|
|
|
ThreadSolver& solver = m_solvers[i];
|
|
|
|
delete solver.solver;
|
|
|
|
solver.solver = NULL;
|
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///solve a group of constraints
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar btConstraintSolverPoolMt::solveGroup(btCollisionObject** bodies,
|
|
|
|
int numBodies,
|
|
|
|
btPersistentManifold** manifolds,
|
|
|
|
int numManifolds,
|
|
|
|
btTypedConstraint** constraints,
|
|
|
|
int numConstraints,
|
|
|
|
const btContactSolverInfo& info,
|
|
|
|
btIDebugDraw* debugDrawer,
|
|
|
|
btDispatcher* dispatcher)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
ThreadSolver* ts = getAndLockThreadSolver();
|
|
|
|
ts->solver->solveGroup(bodies, numBodies, manifolds, numManifolds, constraints, numConstraints, info, debugDrawer, dispatcher);
|
|
|
|
ts->mutex.unlock();
|
|
|
|
return 0.0f;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void btConstraintSolverPoolMt::reset()
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
for (int i = 0; i < m_solvers.size(); ++i)
|
|
|
|
{
|
|
|
|
ThreadSolver& solver = m_solvers[i];
|
|
|
|
solver.mutex.lock();
|
|
|
|
solver.solver->reset();
|
|
|
|
solver.mutex.unlock();
|
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
/// btDiscreteDynamicsWorldMt
|
|
|
|
///
|
|
|
|
|
2018-09-07 16:11:04 +02:00
|
|
|
btDiscreteDynamicsWorldMt::btDiscreteDynamicsWorldMt(btDispatcher* dispatcher,
|
2019-01-03 14:26:51 +01:00
|
|
|
btBroadphaseInterface* pairCache,
|
|
|
|
btConstraintSolverPoolMt* solverPool,
|
|
|
|
btConstraintSolver* constraintSolverMt,
|
|
|
|
btCollisionConfiguration* collisionConfiguration)
|
|
|
|
: btDiscreteDynamicsWorld(dispatcher, pairCache, solverPool, collisionConfiguration)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
void* mem = btAlignedAlloc(sizeof(btSimulationIslandManagerMt), 16);
|
2017-08-01 14:30:58 +02:00
|
|
|
btSimulationIslandManagerMt* im = new (mem) btSimulationIslandManagerMt();
|
2019-01-03 14:26:51 +01:00
|
|
|
im->setMinimumSolverBatchSize(m_solverInfo.m_minimumSolverBatchSize);
|
|
|
|
m_islandManager = im;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
m_constraintSolverMt = constraintSolverMt;
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
btDiscreteDynamicsWorldMt::~btDiscreteDynamicsWorldMt()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void btDiscreteDynamicsWorldMt::solveConstraints(btContactSolverInfo& solverInfo)
|
|
|
|
{
|
|
|
|
BT_PROFILE("solveConstraints");
|
|
|
|
|
|
|
|
m_constraintSolver->prepareSolve(getCollisionWorld()->getNumCollisionObjects(), getCollisionWorld()->getDispatcher()->getNumManifolds());
|
|
|
|
|
|
|
|
/// solve all the constraints for this island
|
2019-01-03 14:26:51 +01:00
|
|
|
btSimulationIslandManagerMt* im = static_cast<btSimulationIslandManagerMt*>(m_islandManager);
|
|
|
|
btSimulationIslandManagerMt::SolverParams solverParams;
|
|
|
|
solverParams.m_solverPool = m_constraintSolver;
|
|
|
|
solverParams.m_solverMt = m_constraintSolverMt;
|
|
|
|
solverParams.m_solverInfo = &solverInfo;
|
|
|
|
solverParams.m_debugDrawer = m_debugDrawer;
|
|
|
|
solverParams.m_dispatcher = getCollisionWorld()->getDispatcher();
|
|
|
|
im->buildAndProcessIslands(getCollisionWorld()->getDispatcher(), getCollisionWorld(), m_constraints, solverParams);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
m_constraintSolver->allSolved(solverInfo, m_debugDrawer);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct UpdaterUnconstrainedMotion : public btIParallelForBody
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btScalar timeStep;
|
|
|
|
btRigidBody** rigidBodies;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void forLoop(int iBegin, int iEnd) const BT_OVERRIDE
|
|
|
|
{
|
|
|
|
for (int i = iBegin; i < iEnd; ++i)
|
|
|
|
{
|
|
|
|
btRigidBody* body = rigidBodies[i];
|
|
|
|
if (!body->isStaticOrKinematicObject())
|
|
|
|
{
|
|
|
|
//don't integrate/update velocities here, it happens in the constraint solver
|
|
|
|
body->applyDamping(timeStep);
|
|
|
|
body->predictIntegratedTransform(timeStep, body->getInterpolationWorldTransform());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
};
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btDiscreteDynamicsWorldMt::predictUnconstraintMotion(btScalar timeStep)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
BT_PROFILE("predictUnconstraintMotion");
|
|
|
|
if (m_nonStaticRigidBodies.size() > 0)
|
|
|
|
{
|
|
|
|
UpdaterUnconstrainedMotion update;
|
|
|
|
update.timeStep = timeStep;
|
|
|
|
update.rigidBodies = &m_nonStaticRigidBodies[0];
|
|
|
|
int grainSize = 50; // num of iterations per task for task scheduler
|
|
|
|
btParallelFor(0, m_nonStaticRigidBodies.size(), grainSize, update);
|
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btDiscreteDynamicsWorldMt::createPredictiveContacts(btScalar timeStep)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
BT_PROFILE("createPredictiveContacts");
|
|
|
|
releasePredictiveContacts();
|
|
|
|
if (m_nonStaticRigidBodies.size() > 0)
|
|
|
|
{
|
|
|
|
UpdaterCreatePredictiveContacts update;
|
|
|
|
update.world = this;
|
|
|
|
update.timeStep = timeStep;
|
|
|
|
update.rigidBodies = &m_nonStaticRigidBodies[0];
|
|
|
|
int grainSize = 50; // num of iterations per task for task scheduler
|
|
|
|
btParallelFor(0, m_nonStaticRigidBodies.size(), grainSize, update);
|
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btDiscreteDynamicsWorldMt::integrateTransforms(btScalar timeStep)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
BT_PROFILE("integrateTransforms");
|
|
|
|
if (m_nonStaticRigidBodies.size() > 0)
|
|
|
|
{
|
|
|
|
UpdaterIntegrateTransforms update;
|
|
|
|
update.world = this;
|
|
|
|
update.timeStep = timeStep;
|
|
|
|
update.rigidBodies = &m_nonStaticRigidBodies[0];
|
|
|
|
int grainSize = 50; // num of iterations per task for task scheduler
|
|
|
|
btParallelFor(0, m_nonStaticRigidBodies.size(), grainSize, update);
|
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int btDiscreteDynamicsWorldMt::stepSimulation(btScalar timeStep, int maxSubSteps, btScalar fixedTimeStep)
|
2018-09-07 16:11:04 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
int numSubSteps = btDiscreteDynamicsWorld::stepSimulation(timeStep, maxSubSteps, fixedTimeStep);
|
|
|
|
if (btITaskScheduler* scheduler = btGetTaskScheduler())
|
|
|
|
{
|
|
|
|
// tell Bullet's threads to sleep, so other threads can run
|
|
|
|
scheduler->sleepWorkerThreadsHint();
|
|
|
|
}
|
|
|
|
return numSubSteps;
|
2018-09-07 16:11:04 +02:00
|
|
|
}
|