EmPhysXUtil (Download) (scroll down to bottom of the page to see class definitions) Contact Me for bugs, comments and ideas
EmPhysXUtil is a collection of functions meant to help people get started in DirectX/PhysX programming.
Readme
All functions are in a class and declared as static with the exception of UpdatePhysics which is declared as purely virtual. The code is designed in a way to either be adapted to an existing codebase or act as its own, it does this by keeping internal pointers for both the PhysX SDK and scene. If PhysX is initialized through any one of the overloaded EmPhysXUtil::InitPhysX() functions then the internal pointers will be initialized too. If EmPhysXUtil::InitPhysX() is not called then when calling functions in the class which require either the physics scene or SDK the programmer must provide them via parameter inputs
If the physics scene is initialized completely internally then the the programmer should create a new class Inheriting from EmPhysXUtil to implement the UpdatePhysics function, otherwise there is no way to control how PhysX updates.
All paramters for arguments which recquire the scene or SDK will be used if the pointers are provided, otherwise they default to NULL and if NULL the code checks to see if the internal pointers have been declared. If they have not been declared the function will not complete executing.
For example if a function is declared as follows:
[sourcecode language='php']EmPhysXUtil::DoSomething(NxScene* scene = NULL);[/sourcecode]
and called like so:
[sourcecode language='cpp']DoSomething();[/sourcecode]
’scene’ will be substituted with the internal declaration of NxScene (EmPhysXUtil::gScene). If the internal declaration is also NULL the function will probably fail. In practice this is indicated either by returning ‘false’ or NULL if the function returns a pointer. Where this isn’t possible and a PhysX class is being returned then calling .isValid() should determine if the class is valid or not.
Class Definitions
[sourcecode language='cpp']
class EmPhysXUtil
{
public:
//The following two functions initalize the PhysX sdk and outputs both the Phys SDK and the scene created
static bool InitPhysX(NxPhysicsSDK* out_physicsSDK, NxScene* out_scene, const NxVec3& gravity,
NxSimulationType simType = NX_SIMULATION_SW, NxTimeStepMethod timeStep = NX_TIMESTEP_VARIABLE, float maxTimeStep = -1);
static bool InitPhysX(NxPhysicsSDK* out_physicsSDK, NxScene* out_scene, const NxSceneDesc& sceneDesc);
//this function creates the scene internally without giving return values and therefore no access to the physx sdk and scene
static bool InitPhysX();
//It is up to the programmer to inherit from EmPhysXUtil and write how the physics engine updates, this must be done if PhysX is initalized
//via InitPhysX() as there is no other way to update the physics scene
virtual void UpdatePhysics() = 0;
//Note for all functions which take in a PhysX scene or PhysX SDK or cooking interface (and basically any pointer which has a default preset of NULL)
//which are called, any parameter which remains as NULL will not be used and defaulted to the internally held instance of said pointer. Note however
//in a few exceptions (such as NxBodyDesc) if the pointer remains NULL it will affect the actor, usually by making it static
static NxActor* CreateActorFromActDesc(const NxActorDesc& actDesc, NxScene* in_scene = NULL);
static NxActor* CreateActorFromShapeDesc(NxShapeDesc in_shape, NxBodyDesc* body = NULL, NxScene* in_scene = NULL);
//Create body desc
static NxBodyDesc CreateSimpleBodyDesc(float mass);
//note category specific functions are in inner classes named after them, for example all functions dealing with convex related geometry are declared
//in the Convex class so calling Convex::CreateActorFromID3DXMesh means you’re going to create a convex actor from an ID3DXMesh or if you call
//TriMesh::CreateActorFromID3DXMesh you’re going to create a triangle mesh from an ID3DXMesh
//The following are forward declrations. Don’t question my methods damnit! In generel the definition of the classes should be found in files named
//in EmLib_
class Convex;
class TriMesh;
protected:
static NxScene* gScene;
static NxPhysicsSDK* gPhysicsSDK;
static NxCookingInterface *gCooking;
//internal helper functions
static std::vector
static std::vector
//check and synch stuff..
//CheckPhysicsSDK and CheckPhysicsScene take by reference pointers of nxphysicssdk and nxscene. If either are null they are assigned internal instances
//if the internal instances are also null then these functions return false, otherwise true
static bool CheckPhysicsSDK(NxPhysicsSDK*& in_physicsSDK);
static bool CheckPhysicsScene(NxScene*& in_physicsScene);
};[/sourcecode]
[sourcecode language='cpp']class EmPhysXUtil::TriMesh
{
public:
static NxActor* CreateActorFromID3DXMesh(ID3DXMesh* in_mesh, NxBodyDesc* body = NULL, NxScene* scene = NULL, NxPhysicsSDK* physicsSDK = NULL);
static NxTriangleMeshDesc CreateDescFromID3DXMesh(ID3DXMesh* model);
static bool GenShapeDescFromDesc(NxTriangleMeshDesc& in_cmd, NxTriangleMeshShapeDesc& out_convexShapeDesc, NxPhysicsSDK* in_physicsSDK = NULL);
static bool SaveToFile(const std::string in_dir, NxTriangleMeshDesc in_triMeshDesc);
static bool CreateShapeDescFromFile(::NxTriangleMeshShapeDesc&out_triShape, const std::string& in_dir, NxPhysicsSDK* in_physicsSDK);
static NxActorDesc CreateActorDescFromID3DXMesh(ID3DXMesh* in_mesh, NxBodyDesc* in_body = NULL, NxPhysicsSDK* in_physicsSDK = NULL);
};[/sourcecode]
[sourcecode language='cpp']class EmPhysXUtil::Convex
{
public:
static NxActor* CreateActorFromID3DXMesh(ID3DXMesh* in_mesh, NxBodyDesc* body = NULL, NxScene* scene = NULL, NxPhysicsSDK* physicsSDK = NULL);
//Creates an NxConvexMeshDesc from the provided ID3DXMesh
static NxConvexMeshDesc CreateDescFromID3DXMesh(ID3DXMesh* in_mesh);
//generates a convex shape desc from a convex desc ( can be used in conjunction with EmPhysXUtil::CreateConvexDescFromID3DXMesh() )
static bool GenShapeDescFromDesc(NxConvexMeshDesc& in_cmd, NxConvexShapeDesc& out_convexShapeDesc, NxPhysicsSDK* in_physicsSDK = NULL);
//saves convex desc (aka cooks to file to file and palces it at the given directory
static bool SaveToFile(const std::string in_dir, NxConvexMeshDesc in_cmd);
//create convex shape desc from file
static bool CreateShapeDescFromFile(NxConvexShapeDesc&out_convShape, const std::string&in_dir, NxPhysicsSDK* in_physicsSDK);
//create actor desc from ID3DXMesh
static NxActorDesc CreateActorDescFromID3DXMesh(ID3DXMesh* in_mesh, NxBodyDesc* in_body = NULL, NxPhysicsSDK* in_physicsSDK = NULL);
};[/sourcecode]