Posted on

Tutorial 1: Kick Start (NVIDIA PhysX)

Getting Started..

In this tutorial we are going to cover the following:
a) Creating PhysX context.
b) Initialize the Graphics Environment.
c) Destroy PhysX.

“This tutorial is designed and explained for absolute beginners who have no knowledge of API programming in C/C++ and flexible in general Physics laws.”

a) Initialize PhysX SDK.

Before you can use the PhysX API, it is obligatory to initialize the SDK. Initializing the SDK involves following tasks:

1. Create an object of type NxPhysicsSDK.

You need to create an object of NxPhysicsSDK. This is required because all the future object creation would depend on this object. If SDK is not initialized, then any further PhysX object creation would fail. Creating the NxPhysicsSDK also means that the simulation is a part of some unique context. If one or more PhysX simulation is launched then the NxPhysicsSDK object would decide the valid context in which the simulation is fired upon.

The following code snippet shows the initialization process.

 NxPhysicsSDK *pPhysics ; //declare globally

 pPhysics = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION);


2. Create a scene descriptor class to describe a scene where all PhysX simulation takes place.
A scene has following properties:
i. bodies,
ii. restraints and
iii. effectors.
These individual components can interact with each other. Scene is responsible for simulation of these objects with respect to time. Several scenes can exist at the same time but each component is restricted to a scene.

NxVec3 defaultGravity(0.f, -9.8f, 0.f); //Declare somewhere in the global scope

NxSceneDesc sceneDesc;
sceneDesc.simType = NX_SIMULATION_HW;
sceneDesc.gravity = defaultGravity;

Descriptors are structures that contain all the information required for the object you wish to create. These descriptors are then attached (or added) to the object description array using pushBack()/push_back() methods. PhysX descriptors defines the nature, behaviour and other parameters that could effect the overall PhysX calculation.

The descriptor field simType can be Hardware(NX_SIMULATION_HW) or Software(NX_SIMULATION_SW) accelerated. The hardware here represents a dedicated PPU (Physics Processing Unit) by NVIDIA which are available on Geforce 8800GT onwards. You can actually put an SLI to make PhysX work on other GPU’s. If software accelerated then CPU/GPU processes PhysX simulation. Remember that PhysX can be processed by an ATI Radeon GPU too. Hacks are available on the internet.

The next field is gravity which takes input as NxVec3 type. Its a vector type with x, y and z coordinates. We define y = -9.8f, signifies Earth’s gravity pointing/acting towards the center of the Earth i.e. an object will experience a 9.8m/s acceleration towards Earth. You can define your own gravity as per your requirement.

There are numerous scene descriptions you can describe. See PhysX documentation for details.

3. Finally create a scene consuming the descriptor.
Once the “descriptor has been described”, Scene object uses this descriptor to create a scene.  Consider following snippet:

 NxScene *pScene; //Declare Globally (Recommended)
 pScene = pPhysics->createScene(sceneDesc);

     sceneDesc.simType = NX_SIMULATION_SW;
     sceneDesc.gravity = defaultGravity;
     pScene = pPhysics->createScene(sceneDesc);
     if (!pScene) return;

Using the PhysX SDK pointer we create a scene object. We check whether the scene is created successfully using simType as Hardware. If not we try to re-initialize the scene object with Software and default gravity.

4. Start the simulation.
To simulate physics you require at least one scene in your program. As we have initiated a scene, you can now start physics simulation using NxScene::simulate() method. NxScene::simulate() method takes time slice as parameter. Time slice parameter guides the engine to simulate the physics for each quantum of time. After giving call to NxScene::simulate() method the Physics engine internally invokes several operations and calculates the necessary physics for you, as described in the scene, controlled by time quantum.
To simulate the scene call:

void StartPhysics(void)
NxReal currentTime = GetTime();

The StartPhysics() now starts simulates the PhysX for the current time quantum. The GetTime() returns current time of type NxReal(or float). NxScene::flushStream() flushes any commands in the buffer before presenting the processed results to the user. Actually it ensures that the PhysX has completed the current set of commands.

b) Initialize the Graphics Environment.

In the “Setting up PhysX Environment” post it was mentioned that PhysX is independent of the graphics environment. That means PhysX gives us only the calculated values. Plotting these calculated values to 3D environment takes considerable amount of task. Ironically it is also a repetitive task. It doesn’t matter you use which engine whether DirectX or OpenGL. I would also like to quickly mention here that PhysX can easily integrate into various game engines. Naming a few: Irrlicht, Ogre, etc.
Again its a personal choice and I have to admit that I am not aware of DirectX. So I will go with OpenGL as 3D renderer.

To initialize your rendering window follow “OpenGL Tutorials(Working on it…)”. Meanwhile you can visit Swiftless, a very nice and comprehensive set of tutorials designed for beginners.

c) Destroy PhysX.

Simulation over! Now what? When exiting your application deallocate all the memory portions you have used during the simulation i.e. release the SDK object.



If releasing resources, First release the scene object and then the SDK object. By convention first always release the object which was created recently and so on.

That’s it, we conclude explaining the basic structure of the PhysX program. Please leave comments or improvements thus required.


About GPUToaster

9 responses to “Tutorial 1: Kick Start (NVIDIA PhysX)

  1. Alexis ⋅


    Nice tutorial.
    You can use :

    if (gPhysicsSDK->getHWVersion() > 0)
    sceneDesc.simType = NX_SIMULATION_HW;
    sceneDesc.simType = NX_SIMULATION_SW;

    To detect hardware acceleration in a more suitable way.


  2. Dido ⋅

    That’s awesome, nice intro in normal language.

  3. Dido ⋅

    Dear GPUToaster,

    Is it possible to import a 3ds model from 3dMax or any and use it in PhysX SDK simulation. if possible, how can I deal with the imported meshes? Here, I want to use the imported meshes as a plane for physX actor/simulation in general.
    Also, do you have an idea of how I can integrate physX with AR?

    Your advice is highly appreciated.

    • I have no idea about AR. So wont be able to help you immediately. About the 3ds model, you can have a look at the “Sample Asset Export ” source code that’s given in the PhysX SDK. Also take a look at NXU library in the Tools Folder.

  4. CR

    that’s useful for me! thanks a lot.
    btw, where’s the followed tutorials?

  5. Julien ⋅

    Thank you. I used to be intimidated by PhysX. But your nice tutorial took most of the mythology out. Definitely worth checking this page.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s