2017-08-01 14:30:58 +02:00
|
|
|
/*
|
|
|
|
Bullet Continuous Collision Detection and Physics Library
|
2022-01-06 23:37:49 +01:00
|
|
|
Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
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 "btSimpleDynamicsWorld.h"
|
|
|
|
#include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
|
|
|
|
#include "BulletCollision/BroadphaseCollision/btSimpleBroadphase.h"
|
|
|
|
#include "BulletCollision/CollisionShapes/btCollisionShape.h"
|
|
|
|
#include "BulletDynamics/Dynamics/btRigidBody.h"
|
|
|
|
#include "BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"
|
|
|
|
#include "BulletDynamics/ConstraintSolver/btContactSolverInfo.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
Make sure this dummy function never changes so that it
|
|
|
|
can be used by probes that are checking whether the
|
|
|
|
library is actually installed.
|
|
|
|
*/
|
2019-01-03 14:26:51 +01:00
|
|
|
extern "C"
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
void btBulletDynamicsProbe();
|
|
|
|
void btBulletDynamicsProbe() {}
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btSimpleDynamicsWorld::btSimpleDynamicsWorld(btDispatcher* dispatcher, btBroadphaseInterface* pairCache, btConstraintSolver* constraintSolver, btCollisionConfiguration* collisionConfiguration)
|
|
|
|
: btDynamicsWorld(dispatcher, pairCache, collisionConfiguration),
|
|
|
|
m_constraintSolver(constraintSolver),
|
|
|
|
m_ownsConstraintSolver(false),
|
|
|
|
m_gravity(0, 0, -10)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
btSimpleDynamicsWorld::~btSimpleDynamicsWorld()
|
|
|
|
{
|
|
|
|
if (m_ownsConstraintSolver)
|
2019-01-03 14:26:51 +01:00
|
|
|
btAlignedFree(m_constraintSolver);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
int btSimpleDynamicsWorld::stepSimulation(btScalar timeStep, int maxSubSteps, btScalar fixedTimeStep)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
(void)fixedTimeStep;
|
|
|
|
(void)maxSubSteps;
|
|
|
|
|
|
|
|
///apply gravity, predict motion
|
|
|
|
predictUnconstraintMotion(timeStep);
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btDispatcherInfo& dispatchInfo = getDispatchInfo();
|
2017-08-01 14:30:58 +02:00
|
|
|
dispatchInfo.m_timeStep = timeStep;
|
|
|
|
dispatchInfo.m_stepCount = 0;
|
|
|
|
dispatchInfo.m_debugDraw = getDebugDrawer();
|
|
|
|
|
|
|
|
///perform collision detection
|
|
|
|
performDiscreteCollisionDetection();
|
|
|
|
|
|
|
|
///solve contact constraints
|
|
|
|
int numManifolds = m_dispatcher1->getNumManifolds();
|
|
|
|
if (numManifolds)
|
|
|
|
{
|
|
|
|
btPersistentManifold** manifoldPtr = ((btCollisionDispatcher*)m_dispatcher1)->getInternalManifoldPointer();
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
btContactSolverInfo infoGlobal;
|
|
|
|
infoGlobal.m_timeStep = timeStep;
|
2019-01-03 14:26:51 +01:00
|
|
|
m_constraintSolver->prepareSolve(0, numManifolds);
|
|
|
|
m_constraintSolver->solveGroup(&getCollisionObjectArray()[0], getNumCollisionObjects(), manifoldPtr, numManifolds, 0, 0, infoGlobal, m_debugDrawer, m_dispatcher1);
|
|
|
|
m_constraintSolver->allSolved(infoGlobal, m_debugDrawer);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
///integrate transforms
|
|
|
|
integrateTransforms(timeStep);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
updateAabbs();
|
|
|
|
|
|
|
|
synchronizeMotionStates();
|
|
|
|
|
|
|
|
clearForces();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btSimpleDynamicsWorld::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_collisionObjects.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
btCollisionObject* colObj = m_collisionObjects[i];
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
btRigidBody* body = btRigidBody::upcast(colObj);
|
|
|
|
if (body)
|
|
|
|
{
|
|
|
|
body->clearForces();
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
}
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btSimpleDynamicsWorld::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_collisionObjects.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
btCollisionObject* colObj = m_collisionObjects[i];
|
|
|
|
btRigidBody* body = btRigidBody::upcast(colObj);
|
|
|
|
if (body)
|
|
|
|
{
|
|
|
|
body->setGravity(gravity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 btSimpleDynamicsWorld::getGravity() const
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
return m_gravity;
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btSimpleDynamicsWorld::removeRigidBody(btRigidBody* body)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
btCollisionWorld::removeCollisionObject(body);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btSimpleDynamicsWorld::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 btSimpleDynamicsWorld::addRigidBody(btRigidBody* body)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
body->setGravity(m_gravity);
|
|
|
|
|
|
|
|
if (body->getCollisionShape())
|
|
|
|
{
|
|
|
|
addCollisionObject(body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btSimpleDynamicsWorld::addRigidBody(btRigidBody* body, int group, int mask)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
body->setGravity(m_gravity);
|
|
|
|
|
|
|
|
if (body->getCollisionShape())
|
|
|
|
{
|
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 btSimpleDynamicsWorld::debugDrawWorld()
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btSimpleDynamicsWorld::addAction(btActionInterface* action)
|
|
|
|
{
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btSimpleDynamicsWorld::removeAction(btActionInterface* action)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btSimpleDynamicsWorld::updateAabbs()
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
btTransform predictedTrans;
|
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)
|
|
|
|
{
|
|
|
|
if (body->isActive() && (!body->isStaticObject()))
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
btVector3 minAabb, maxAabb;
|
|
|
|
colObj->getCollisionShape()->getAabb(colObj->getWorldTransform(), minAabb, maxAabb);
|
2017-08-01 14:30:58 +02:00
|
|
|
btBroadphaseInterface* bp = getBroadphase();
|
2019-01-03 14:26:51 +01:00
|
|
|
bp->setAabb(body->getBroadphaseHandle(), minAabb, maxAabb, m_dispatcher1);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btSimpleDynamicsWorld::integrateTransforms(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 < m_collisionObjects.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
btCollisionObject* colObj = m_collisionObjects[i];
|
|
|
|
btRigidBody* body = btRigidBody::upcast(colObj);
|
|
|
|
if (body)
|
|
|
|
{
|
|
|
|
if (body->isActive() && (!body->isStaticObject()))
|
|
|
|
{
|
|
|
|
body->predictIntegratedTransform(timeStep, predictedTrans);
|
2019-01-03 14:26:51 +01:00
|
|
|
body->proceedToTransform(predictedTrans);
|
2017-08-01 14:30:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btSimpleDynamicsWorld::predictUnconstraintMotion(btScalar timeStep)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
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)
|
|
|
|
{
|
|
|
|
if (!body->isStaticObject())
|
|
|
|
{
|
|
|
|
if (body->isActive())
|
|
|
|
{
|
|
|
|
body->applyGravity();
|
2019-01-03 14:26:51 +01:00
|
|
|
body->integrateVelocities(timeStep);
|
2017-08-01 14:30:58 +02:00
|
|
|
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 btSimpleDynamicsWorld::synchronizeMotionStates()
|
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_collisionObjects.size(); i++)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
btCollisionObject* colObj = m_collisionObjects[i];
|
|
|
|
btRigidBody* body = btRigidBody::upcast(colObj);
|
|
|
|
if (body && body->getMotionState())
|
|
|
|
{
|
|
|
|
if (body->getActivationState() != ISLAND_SLEEPING)
|
|
|
|
{
|
|
|
|
body->getMotionState()->setWorldTransform(body->getWorldTransform());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void btSimpleDynamicsWorld::setConstraintSolver(btConstraintSolver* solver)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
if (m_ownsConstraintSolver)
|
|
|
|
{
|
|
|
|
btAlignedFree(m_constraintSolver);
|
|
|
|
}
|
|
|
|
m_ownsConstraintSolver = false;
|
|
|
|
m_constraintSolver = solver;
|
|
|
|
}
|
|
|
|
|
|
|
|
btConstraintSolver* btSimpleDynamicsWorld::getConstraintSolver()
|
|
|
|
{
|
|
|
|
return m_constraintSolver;
|
|
|
|
}
|