How to use PhysX on Linux

PhysX is a 3D game physics engine provided by NVIDIA. They have released the source code of the engine on Github, though with restricted access. This library and its sample programs can be compiled from source easily.

Here are my notes on how to compile this library, run its sample tools and get started:

  • You need a Linux computer (I use Ubuntu), with a fairly modern NVIDIA graphics card.

  • Make sure you have installed recent NVIDIA graphics drivers and a recent version of CUDA on it. Ensure that these are working correctly before trying PhysX.

  • Go to this webpage and jump through their hoops to get access to the PhysX Github page. Essentially, NVIDIA requires you to create a login with them and after that they give your Github login the access to their PhysX source code.

  • Once you have access to PhysX source code, clone its repository to your computer:

$ git clone https://github.com/NVIDIAGameWorks/PhysX-3.3.git
  • The documentation in the source code is outdated and is misleading. This is how the source code is laid out: Source for source code, Snippets and Samples have small programs to try PhysX. Once you have compiled these Snippets and Samples, their binaries will be placed in Bin/linux64.

  • Each of the above three code directories has a compiler/linux64 directory which holds the Makefiles to build them. There are four build profiles available: release, profile, checked and debug. Just invoking make builds all four versions. To build just the release versions, I did make release in all the three code directories.

  • Once the library and its snippets and samples are built, you can try these programs from Bin/linux64. For example, the samples program allows you try many of the features of the engine in an interactive GUI.

Tried with: PhysX 3.3.4, NVIDIA GeForce GTX 750 Ti and Ubuntu 14.04

How to debug using PhysX Visual Debugger

The PhysX Visual Debugger is a great tool to visualize and debug your PhysX program. It helps you to view the bodies, velocities and constraints (joints) in your PhysX scene and how they change during the program execution.

You can use PhysX Visual Debugger in a few steps:

  1. Open PhysX Visual Debugger. It needs to be running in the background to listen to information sent by your PhysX program.

  2. In the Preferences sub-window of the debugger make sure that all the elements which you are interested to view are set to Everything. For example, center of mass, velocities or constraints. If you do not do this, you may find yourself staring at an empty DirectX Render window.

  3. In your PhysX code, connect to the debugger. You do this by connecting to its IP and port and informing it what elements should be visualized. A bit of self-explanatory code that does this can be found here.

  4. Compile your PhysX program and execute it. You must ensure that your program is able to connect to the debugger.

  5. Let your program run its physics. Close it once it is done.

  6. All the physics from the last execution of your program would be captured by the debugger. Replay it back by using the play buttons at the top of the debugger. Hopefully, this visualization helps you examine the physics of your program better.

Tried with: PhysX 3.2.4, PhysX Visual Debugger 2.0100.01.13105 and Windows 8 x64

How to enable gravity in PhysX

Gravity is disabled by default in PhysX. To enable it, add a gravity vector to the scene descriptor:

physx::PxSceneDesc* sceneDesc;
// Assuming scene descriptor is created here
sceneDesc->gravity = physx::PxVec3(0.0f, -9.81f, 0.0f);

You can also set the gravity vector in the scene itself:

physx::PxScene* scene;
// Assuming scene is created here
scene->setGravity(physx::PxVec3(0.0f, -9.81f, 0.0f));

To avoid buggy behavior, it is better to enable gravity at the beginning.

Tried with: PhysX 3.2.4, Visual Studio 2010 and Windows 8 x64

How to create shoulder joint in PhysX

The shoulder joint of the human body can be simulated in PhysX by using its spherical joint. The spherical joint is used to represent a ball-and-socket joint.

In the case of the shoulder joint, we assume that the ball-and-socket is placed at the shoulder location and it is connected by the upper arm to elbow. So, we use two PhysX bodies: one located at the shoulder itself and another at the elbow. Alternatively you could put the first body at the neck and connect it by the shoulder bone to the ball-and-socket position.

The code below creates the shoulder joint:

To create the joint we need to provide the position of the two bodies involved, the relative location of the joint to those bodies. In addition, we also need to provide the local orientation of the joint to those bodies.

The joint begins life with an upright orientation, that is, its axes are same as the global axes. We need to point the X-axis of the orientation to look at the second body. The above code does that by computing the Euler angles of rotation around the Y-axis and Z-axis of the joint that point the X-axis to look at the second body. The X-axis of the joint will also be the axis around which the cone of rotation is created to bound the rotational freedom of the joint.

Once we have the Euler angles, we convert that to a quaternion. We also compute the position of the joint relative to the first and second bodies. Each pair of position and orientation rotation forms a transform. Two pairs of these are used to define the joint.

Tried with: PhysX 3.2.4, Visual Studio 2010 and Windows 7 x64

How to get started with PhysX.Net

PhysX.Net is a .Net library that has been written as a wrapper for the PhysX library from NVIDIA. This enables you to use PhysX from C# or any other .Net language.

  1. Ensure that the PhysX SDK is installed and running correctly. This post explains how to get started with PhysX.
  2. Download the latest build or nightly build of PhysX.Net from here. Unzip the contents of the zip file. It has the PhysX.Net.dll .Net assembly file that contains the wrapper methods and classes for both x86 and x64 architectures.
  3. Download the latest source code of PhysX.Net from here. Unzip the contents of the zip file.
  4. Open the PhysX.Net-3.sln solution file in Visual Studio. Examine the Samples project, it has the code for sample programs, such as JointsSample. All of the Samples use the code from the Engine sample, which has the code common to all the Samples.
  5. Add the PhysX.Net.dll downloaded in Step 2 as a reference to all the Samples.
  6. Build the Engine sample. This produces a PhysX.Samples.Engine.dll .Net assembly file that will be used by the other Samples.
  7. Now build any of the Samples, for example, the JointsSample. This produces a PhysX.Samples.JointsSample.exe.
  8. The EXE files need the PhysX DLL files, from NVIDIA, to execute. These are the 5 files: PhysX3CharacterKinematicCHECKED_x86.dllPhysX3CHECKED_x86.dllPhysX3CommonCHECKED_x86.dllPhysX3CookingCHECKED_x86.dll and PhysX3GpuCHECKED_x86.dll. These files can be found in the NVIDIA PhysX SDK or in the Assemblies directory in the PhysX source code. Place these in the same directory as the EXE file.
  9. Now you should be able to run the EXE file directly or debug it from Visual Studio.

Tried with: PhysX.Net 102011, PhysX 3.2.4, Visual Studio Express 2012 and Windows 7 x64

How to get started with PhysX on Windows

It can be a bit difficult to get started developing with PhysX on Windows since there is not a lot of documentation. Here are a few steps to get started easily:

  1. Download the latest PhysX SDK. To do that you need go here. You need to register to login. Once inside, head over to Home > Downloads > PhysX SDK 3.2 > More. Grab the latest version, it will be a zip file.
  2. Unzip the downloaded PhysX SDK zip file. Go to Samples\compiler and choose the Visual Studio version that matches or is closest to the version you are using.
  3. Open the Samples.sln solution file. Compile the Samples project inside it.
  4. Hopefully, the compilation is successful. Note the last line in the Output window of Visual Studio. This shows the path and the name of the EXE file that it has produced.
  5. Open this EXE file and enjoy the PhysX demos.
  6. Now that you have PhysX samples running, have a look at the code and the settings of the solution to learn how to write your own PhysX code and compile it.

Tried with: PhysX SDK 3.2.4, Visual Studio Express 2012 and Windows 7 x64