Bullet 学习笔记之实例 GraspDeformable
参照 GraspDeformable 实例,探究软体抓取的仿真流程。
1 仿真场景的构成
仿真场景类为 btDeformableMultiBodyDynamicsWorld
,派生关系为 btDiscreteDynamicsWorld
-> btMultiBodyDynamicsWorld
-> btDeformableMultiBodyDynamicsWorld
其中,从成员变量上看,btMultiBodyDynamicsWorld
类在前者(父类)的基础上增加了 multiBody 相关的项,如 m_multiBodies
m_multiBodyConstraints
m_multiBodyConstraintSolver
m_solverMultiBodyIslandCallback
等,其定义部分如下:
///The btMultiBodyDynamicsWorld adds Featherstone multi body dynamics to Bullet
///This implementation is still preliminary/experimental.
class btMultiBodyDynamicsWorld : public btDiscreteDynamicsWorld
{
protected:
btAlignedObjectArray<btMultiBody*> m_multiBodies;
btAlignedObjectArray<btMultiBodyConstraint*> m_multiBodyConstraints;
btAlignedObjectArray<btMultiBodyConstraint*> m_sortedMultiBodyConstraints;
btMultiBodyConstraintSolver* m_multiBodyConstraintSolver;
MultiBodyInplaceSolverIslandCallback* m_solverMultiBodyIslandCallback;
...
}
而 btDeformableMultiBodyDynamicsWorld
又在其父类的基础上增加了 SoftBody 相关的项,比如 m_softBodies
m_deformableBodySolver
等,其定义如下:
class btDeformableMultiBodyDynamicsWorld : public btMultiBodyDynamicsWorld
{
typedef btAlignedObjectArray<btVector3> TVStack;
///Solver classes that encapsulate multiple deformable bodies for solving
btDeformableBodySolver* m_deformableBodySolver;
btSoftBodyArray m_softBodies;
int m_drawFlags;
bool m_drawNodeTree;
bool m_drawFaceTree;
bool m_drawClusterTree;
btSoftBodyWorldInfo m_sbi;
btScalar m_internalTime;
int m_ccdIterations;
bool m_implicit;
bool m_lineSearch;
bool m_selfCollision;
DeformableBodyInplaceSolverIslandCallback* m_solverDeformableBodyIslandCallback;
...
}
从成员函数的角度看,所有的仿真步骤是在 btDeformableMultiBodyDynamicsWorld::stepSimulation(...)
中完成。
2 GraspDeformable 实例初始化
2.1 内部成员变量初始化
参照 GraspDeformable 实例,其中的初始化环节,分别给定了仿真场景中相关变量的初值机类型,具体来说,有:
m_collisionConfiguration = new btSoftBodyRigidBodyCollisionConfiguration();
m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
m_broadphase = new btDbvtBroadphase();
btDeformableBodySolver* deformableBodySolver = new btDeformableBodySolver();
btDeformableMultiBodyConstraintSolver* sol = new btDeformableMultiBodyConstraintSolver();
sol->setDeformableSolver(deformableBodySolver);
m_solver = sol;
m_dynamicsWorld = new btDeformableMultiBodyDynamicsWorld(m_dispatcher, m_broadphase, sol, m_collisionConfiguration, deformableBodySolver);
以及
btDeformableMultiBodyDynamicsWorld::btDeformableMultiBodyDynamicsWorld(...)
{
...
m_solverDeformableBodyIslandCallback = new DeformableBodyInplaceSolverIslandCallback(constraintSolver, dispatcher);
}
btMultiBodyDynamicsWorld::btMultiBodyDynamicsWorld(btDispatcher* dispatcher, btBroadphaseInterface* pairCache, btMultiBodyConstraintSolver* constraintSolver, btCollisionConfiguration* collisionConfiguration)
: btDiscreteDynamicsWorld(dispatcher, pairCache, constraintSolver, collisionConfiguration),
m_multiBodyConstraintSolver(constraintSolver)
{
//split impulse is not yet supported for Featherstone hierarchies
// getSolverInfo().m_splitImpulse = false;
getSolverInfo().m_solverMode |= SOLVER_USE_2_FRICTION_DIRECTIONS;
m_solverMultiBodyIslandCallback = new MultiBodyInplaceSolverIslandCallback(constraintSolver, dispatcher);
}
其中,碰撞检测相关的: narrow phase 和 Broad phase 还是用了 btCollisionDispatcher
和 btDbvtBroadphase
,而碰撞检测算法的分配(算法矩阵)用了 btSoftBodyRigidBodyCollisionConfiguration
进行分配。
软体仿真相关的用到了 btDeformableBodySolver
和 btDeformableMultiBodyConstraintSolver
DeformableBodyInplaceSolverIslandCallback
MutiBody 相关的用到了 MultiBodyInplaceSolverIslandCallback
2.2 仿真场景的构建
在函数 GraspDeformable::initPhysics()
中,还搭建了仿真场景中的物体。
(1)新建了 btMultiBody
类,加入到了场景中;(2)添加 ground shape;(3)新建了软体 btSoftBody
,并向 m_deformableBodySolver
中添加了 btDeformableGravityForce
和 btDeformableNeoHookeanForce
等作用力。
3 仿真计算流程 PipeLine
仿真主要步骤包含在了 btDeformableMultiBodyDynamicsWorld::stepSimulation(...)
中,具体拆分如下:
[1] btDiscreteDynamicsWorld::saveKinematicState(..);
[2] btDiscreteDynamicsWorld::applyGravity();
[3] btDeformableMultiBodyDynamicsWorld::internalSingleStepSimulation(..);
[3.1] btDeformableMultiBodyDynamicsWorld::reinitialize(t);
[3.2] btDeformableMultiBodyDynamicsWorld::applyRigidBodyGravity(t);
[3.3] btDeformableMultiBodyDynamicsWorld::predictUnconstraintMotion(..);
[3.3.1] btMultiBodyDynamicsWorld::predictUnconstraintMotion(timeStep);
[3.3.2] m_deformableBodySolver->predictMotion(timeStep);
[3.4] btMultiBodyDynamicsWorld::performDiscreteCollisionDetection();
[3.5] btDeformableMultiBodyDynamicsWorld::softBodySelfCollision()
即 遍历 btDeformableMultiBodyDynamicsWorld.m_softBodies
,执行 psb->defaultCollisionHandler(psb);
[3.6] btMultiBodyDynamicsWorld::calculateSimulationIslands();
[3.7] btDeformableMultiBodyDynamicsWorld::beforeSolverCallbacks(timeStep);
[3.8] btDeformableMultiBodyDynamicsWorld::solveConstraints(timeStep);
[3.9] btDeformableMultiBodyDynamicsWorld::afterSolverCallbacks(timeStep);
[3.10] btDeformableMultiBodyDynamicsWorld::applyRepulsionForce(timeStep);
[3.11] btDeformableMultiBodyDynamicsWorld::performGeometricCollisions(timeStep);
[3.12] btDeformableMultiBodyDynamicsWorld::integrateTransforms(timeStep);
[3.13] btMultiBodyDynamicsWorld::updateActions(timeStep);
[3.14] btDeformableMultiBodyDynamicsWorld::updateActivationState(timeStep);
[4] btDiscreteDynamicsWorld::synchronizeMotionStates();
[5] btMultiBodyDynamicsWorld::clearForces()
即 btDiscreteDynamicsWorld::clearForces();