2. PhysX API
본 게시글은 NVIDIA PhysX 4.1 SDK Guide를 참고하여 정리한 글입니다.
https://gameworksdocs.nvidia.com/PhysX/4.1/documentation/physxguide/Manual/API.html#determinism
The PhysX API — NVIDIA PhysX SDK 4.1 Documentation
Introduction This chapter covers the basic patterns common to the PhysX application programming interface (API.) We are committed to keeping this API stable and backwards-compatible from one minor release to the next, to protect the investment you make in
gameworksdocs.nvidia.com
1. Type Casting
- PhysX API 인터페이스 클래스는 PxBase에서 상속된다.
- 이렇게 함으로써 유형 간의 다운캐스팅을 안전하게 해준다.
- 만약 PxActor에서 PxRigidDynamic으로 캐스팅하려 한다면 아래와 같이 작성해야 한다.
PxActor * Actor = < ... >
PxRigidDynamic * myActor = Actor -> is < PxRigidDynamic > ();
const PxActor * Actor = < ... >
const PxRigidDynamic * myActor = actor -> is < PxRigidDynamic > ();
- 아마 자신의 타입을 반환하는 가상 함수가 존재하는 듯 하다.
- getConcreteTypeName() 멤버 함수를 사용하면 문자열로 된 이름을 리턴받을 수 있다.
2. Reference Counting
- 일부 PhysX 객체는 Scene에서 여러 번 공유 및 참조되도록 설계되었다.
- 예를 들면 PxConvexMesh는 각각 동일한 지오메트리를 공유하지만 다른 액터와 연결된 여러 PxShape에서 참조할 수 있다.
- (아마 게임에서 하나의 Material로 여러 종류의 오브젝트에서 사용하는 개념과 비슷한 듯 하다)
* 객체가 PxPhysics에서 생성될 때 참조 계수는 1이 된다. (0이 되면 소멸)
* 누군가 그 객체를 참조하면 참조 계수가 증가한다.
- PxShape가 PxConvexMesh, PxHeightfield를 참조하는 경우 등등
- PxShape가 PxMaterial을 참조하는 경우
- PxRigidActor가 PxShape를 참조하는 경우
* 카운트된 참조가 소멸하거나 release() 메서트 호출 시 참조 계수가 1 감소한다
ex)
- PxPhysics::createShape()로 도형을 만든 후(참조계수 1),
- PxRigidActor::attachShape()를 통해 액터에 연결(참조계수 2)
3. Using Different Units
- PhysX에서는 일관된 길이, 질량 단위에 대해 올바른 결과를 생성하도록 설계됨
- 그러나 단위에 따라 기본값을 조작해야 하는 공차 값이 있다.
- 이 오차를 적절한 값으로 기본 설정되게 하려면 PxPhysics, PxCooking 인터페이스를 생성할 때,
PxTolerancesScale 값을 조절하면 된다.
예를 들어, 1m인 객체를 cm 단위로 작업해야 하는 경우 배율을 아래와 같이 조절해야 한다.
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, ...);
4. Memory Management
- PhysX는 PxAllocatorCallback 인터페이스를 통해 모든 할당을 수행한다.
- PhysX를 초기화하려면 이 클래스를 구현해야 함
- 요청된 메모리는 16바이트 크기로 정렬되도록 요구한다.
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;
};
* 윈도우에서는 _aligned_malloc() 함수가 정렬된 바이트 수를 반환하는 기능을 제공한다