内存管理
PhysX通过PxAllocatorCallback接口执行所有的分配,用户可以继承这个借口,定制内存分配sdk的deamo实现PxDefaultAllocatorCallback
class PxAllocatorCallback
{
public:
virtual ~PxAllocatorCallback() {}
virtual void* allocate(size_t size, const char* typeName, const char* filename,
int line) = 0;
virtual void deallocate(void* ptr) = 0;
};
官方文档显示,内存分配是按16字节对齐的
Error Reporting
PhysX通过PxErrorCallback接口记录所有的错误消息,可以通过继承接口,实现定制,sdk的deamo实现PxDefaultErrorCallback
class UserErrorCallback : public PxErrorCallback
{
public:
virtual void reportError(PxErrorCode::Enum code, const char* message, const char* file,
int line)
{
// error processing implementation
...
}
};
数学
在PhysX中常用的数学课有PxVec2、PxVec3、PxVec4、PxMat33、PxMat44、PxTransform、PxPlane和PxQuat,需要注意的是:
1:PxTransform表示的是刚体变换,它是一个旋转四元数和位置向量,PhysX函数都使用这种类型。
2:PxPlane是一个齐次平面方程,即构造函数PxPlane(n, d)表示方程n.x + d = 0
绑定应用程序对象
通常,应用程序需要将PhysX对象与应用程序对象关联起来。连接单个用户应用程序对象和一个PhysX对象的一个简单方法是使用最重要的PhysX类(PxActor::userData, PxShape::userData, PxMaterial::userData,…)提供的userData成员。userData成员是一个void*指针,它保留给应用程序使用。每个类只有一个userData字段,因此为了管理多个关联,必须使用其他机制。
引用计数
类型PxTriangleMesh、PxHeightField、PxConvexMesh、PxMaterial、PxClothFabric和PxShape。这些类型的每个对象都有一个引用计数。引用计数的规则如下:
- 当从PxPhysics创建对象时,它的引用计数为1
- 当一个对象的引用计数为0时,该对象将被销毁
- 创建新的计数引用时,引用计数递增。计数参考如下:
*when a PxShape references a PxConvexMesh, PxHeightfield, or PxTriangleMesh.
*when a PxShape references a PxMaterial.
*when a PxRigidActor references a PxShape.
*when a PxCloth references a PxClothFabric - 当被计数的引用被销毁或对象的release()方法被调用时,引用计数将递减
- 当通过反序列化创建对象时,其引用计数为1,加上对该对象存在的已计数的引用数
类型转换
非常像ue4的类型转换
PxActor* actor = <…>
PxRigidDynamic* myActor = actor->is();
const PxActor* actor = <…>
const PxRigidDynamic* myActor = actor->is();
getConcreteType() provides an integer value which corresponds to the concrete type of an object
getConcreteTypeName() provides a string name of the concrete type
isKindOf() provides string-based testing of inheritance
缩放比例
PxFoundation* foundation = …;
PxTolerancesScale scale;
scale.length = 100; // typical length of an object
scale.speed = 981; // typical speed of an object, gravity*1s is a reasonable choice
PxPhysics *p = PxCreatePhysics(PX_PHYSICS_VERSION, *foundation, scale, …);