Bullet 学习笔记之 CollisionShape 和 CollisionObject
先初步看一下碰撞相关的对象都有些什么。
(小疑惑,为什么对在 btDiscreteDynamicsWorld
外面单独有一个 CollisionShape
,好像是说,CollisionShape
是包含在 CollisionObject
里面的。难道是为了节省内存,对于不同的 CollisionObject
,如果他们的碰撞外形相同,那么就可以只建立一个 CollisionShape
,在不同的 CollisionObject
之间共享。如果是这样的话,那也太细了吧)
(TM 果真是这样的。作者在代码中解释道 Re-using the same collision is better for memory usage and performance
)
碰撞外形类 CollisionShape
基类定义在 bullet3/src/BulletCollision/CollisionShapes/btCollisionShape.h 中,为 btCollisionShape
,作者这样描述该类:///The btCollisionShape class provides an interface for collision shapes that can be shared among btCollisionObjects.
在 btCollisionShape
类中,成员变量也很简单:
int m_shapeType;
void* m_userPointer;
int m_userIndex;
从成员/接口函数分析它的主要功能,有
///getAabb returns the axis aligned bounding box in the coordinate frame of the given transform t.
virtual void getAabb(const btTransform& t, btVector3& aabbMin, btVector3& aabbMax) const = 0;
virtual void getBoundingSphere(btVector3 & center, btScalar & radius) const;
///getAngularMotionDisc returns the maximum radius needed for Conservative Advancement to handle time-of-impact with rotations.
virtual btScalar getAngularMotionDisc() const;
virtual btScalar getContactBreakingThreshold(btScalar defaultContactThresholdFactor) const;
///calculateTemporalAabb calculates the enclosing aabb for the moving object over interval [0..timeStep)
///result is conservative
void calculateTemporalAabb(const btTransform& curTrans, const btVector3& linvel, const btVector3& angvel, btScalar timeStep, btVector3& temporalAabbMin, btVector3& temporalAabbMax) const;
感觉,大概就 AABB,碰撞检测相关的一些东西吧。
从 btCollisionShape
的成员变量可知,对于碰撞形状,也分为不同的类型。具体有:(还挺多的 >_<)
/// btDispatcher uses these types
/// IMPORTANT NOTE:The types are ordered polyhedral, implicit convex and concave
/// to facilitate type checking
/// CUSTOM_POLYHEDRAL_SHAPE_TYPE,CUSTOM_CONVEX_SHAPE_TYPE and CUSTOM_CONCAVE_SHAPE_TYPE can be used to extend Bullet without modifying source code
enum BroadphaseNativeTypes
{
// polyhedral convex shapes
BOX_SHAPE_PROXYTYPE,
TRIANGLE_SHAPE_PROXYTYPE,
TETRAHEDRAL_SHAPE_PROXYTYPE,
CONVEX_TRIANGLEMESH_SHAPE_PROXYTYPE,
CONVEX_HULL_SHAPE_PROXYTYPE,
CONVEX_POINT_CLOUD_SHAPE_PROXYTYPE,
CUSTOM_POLYHEDRAL_SHAPE_TYPE,
//implicit convex shapes
IMPLICIT_CONVEX_SHAPES_START_HERE,
SPHERE_SHAPE_PROXYTYPE,
MULTI_SPHERE_SHAPE_PROXYTYPE,
CAPSULE_SHAPE_PROXYTYPE,
CONE_SHAPE_PROXYTYPE,
CONVEX_SHAPE_PROXYTYPE,
CYLINDER_SHAPE_PROXYTYPE,
UNIFORM_SCALING_SHAPE_PROXYTYPE,
MINKOWSKI_SUM_SHAPE_PROXYTYPE,
MINKOWSKI_DIFFERENCE_SHAPE_PROXYTYPE,
BOX_2D_SHAPE_PROXYTYPE,
CONVEX_2D_SHAPE_PROXYTYPE,
CUSTOM_CONVEX_SHAPE_TYPE,
//concave shapes
CONCAVE_SHAPES_START_HERE,
//keep all the convex shapetype below here, for the check IsConvexShape in broadphase proxy!
TRIANGLE_MESH_SHAPE_PROXYTYPE,
SCALED_TRIANGLE_MESH_SHAPE_PROXYTYPE,
///used for demo integration FAST/Swift collision library and Bullet
FAST_CONCAVE_MESH_PROXYTYPE,
//terrain
TERRAIN_SHAPE_PROXYTYPE,
///Used for GIMPACT Trimesh integration
GIMPACT_SHAPE_PROXYTYPE,
///Multimaterial mesh
MULTIMATERIAL_TRIANGLE_MESH_PROXYTYPE,
EMPTY_SHAPE_PROXYTYPE,
STATIC_PLANE_PROXYTYPE,
CUSTOM_CONCAVE_SHAPE_TYPE,
SDF_SHAPE_PROXYTYPE = CUSTOM_CONCAVE_SHAPE_TYPE,
CONCAVE_SHAPES_END_HERE,
COMPOUND_SHAPE_PROXYTYPE,
SOFTBODY_SHAPE_PROXYTYPE,
HFFLUID_SHAPE_PROXYTYPE,
HFFLUID_BUOYANT_CONVEX_SHAPE_PROXYTYPE,
INVALID_SHAPE_PROXYTYPE,
MAX_BROADPHASE_COLLISION_TYPES
};
感觉其他派生出的形状也都差不多,无非就是 AABB,碰撞间隙,形状信息等等。
碰撞对象 CollisionObject
该基类定义在 bullet3/src/BulletCollision/CollisionDispatch/btCollisionObject.h
作者对该类的描述为
/// btCollisionObject can be used to manage collision detection objects.
/// btCollisionObject maintains all information that is needed for a collision detection: Shape, Transform and AABB proxy.
/// They can be added to the btCollisionWorld.
CollisionObject
类就复杂多了,针对场景中的每一个对象的信息,包含了形状、位姿、摩擦等多种信息。可以说,每一个 CollisionObject
实例,都代表了仿真场景中的一个实体。