Setting Up Your PhysX Environment


PhysX is designed to run on multi-platforms such as Windows, Linux and Mac OS. This post will however describe how to integrate PhysX into Visual Studio. I used NVIDIA PhysX 2.8.1 and Visual Studio 2010 Ultimate RTMRel Edition. However the process remains almost the same for VS2008.

Pre-Requisites:

Before starting PhysX, you must have

  1. Visual Studio 2008/2010.
  2. NVIDIA SDK 2.8.1 (Current Version 2.8.4.2 Beta, as of writing): Contains headers, libraries, binaries, docs, sample programs, example binaries, training documents, shared codes, source codes, remote debugger, etc.
  3. NVIDIA System Software 9.10.0513: Contains runtime binaries which are required redistribution (This is a must).

NOTE: a) You can download NVIDIA PhysX SDK from Download NVIDIA PhysX. (You need to register with NVIDIA). Alternatively you can download it from file-sharing servers such as Softpedia.

b) Get Latest System Software from NVIDIA Drivers.

New Project:

1. To work with PhysX you need to choose a new VC++ Project->Win32 Project (As you will need to display Graphics). See following screenshot.

2. Create a Windows Application -> Empty Project. (Follow as shown below.)

3. Create an Empty project to start from scratch.

4. Before starting to code, you first need to add Include files which will have all the declarations, set of Linkers that will link at compile time to DLL’s. But before that getting familiar with NVIDIA PhysX installation folder structure will make your life easy. NVIDIA PhysX’s default location is ‘Program Files (x86)’. Check the screen shot below.The Folder contains:

a) Bin: Contains pre-compiled example binaries, runtime dll’s and other exe’s. (Run these binaries to see PhysX Capabilities)

b) Graphics: This folder contains some libraries that are essential for displaying and rendering graphics such as Pictures and drawings. It also contains libraries for fast Maths, etc.

c) Low Level: Contains declarations related to the API.

d) Samples: Contains sample SDK programs, source codes.

e) SDKs: This directory is very important. It contains all the required headers, libraries and docs. The following shows the structure…

f) SysRelease : Contains AGEIA Technologies performance monitor hooking procedures.

g) Tools: Contains certain utilities for cooking a Mesh, Reading a Cooked Mesh, Creating Soft Bodies, etc. These may be required for advanced usage.

h) Training Programs: Very Important folder. NVIDIA provides very comprehensive set of tutorials that are targeted towards programmers. However for beginners this may be little confusing.

5. Its time to set programming environment now. Go to ‘Project->Project Properties’. In this you have to set up four things.

a) Include Directories: The same can be found under your SDK folder of PhysX installtion. You need to set path for all the PhysX Modules: ‘Cooking’, ‘Foundation’, ‘NxCharacter’, ‘Physics’ and ‘PhysXLoader’ (All modules and their purpose will be discussed when introducing PhysX API). See the following screenshot for details.

b) Path to Linkers: There’s only one ‘lib’ directory in SDK folder. Add the path as shown in the screenshot below.c) Dependencies: Write the linker names as shown below. (This is to inform the compiler that what all linkers are required at compile time. You may omit one or more dependencies but on the safer side include all the linker names.)

d) Startup Procedure Name: As you will be compiling programs for windows and all the window creation procedure will be Win32 based, your Startup Procedure Name/Entry point must point to ‘mainCRTStartup’. (Details about this can found by search engines, but right now this is your default entry point as ‘main’ is not recognized by VS unless the entry point is defined).6. When you are done, click Apply to Save and exit the project properties. Congrats! you have just set up your PhysX development environment.

“If you look at the Screen shots I have also included GLUT. What the heck is GLUT? PhysX simulations are independent of the rendering context whether it is a DirectX Based or OpenGL based. To simplify the rendering process, GLUT is used. As Creating Windows, Drawing objects and handling I/O Requires lots of efforts, GLUT is here to rescue. Then Why Not DirectX? Well, it all depends on with which rendering technology you are comfortable with. I’ll go with OpenGL as DirectX requires a lot of work to do.”

7. FInally, to check whether the environment is set up correctly, try compiling the following code snippet: (Just copy and paste)

Code Snippet:

#include<NxPhysics>
#include<stdio.h>

int main(int argc, char **argv)
{
	bool initialized = false;
	NxPhysicsSDK *physicsSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION, NULL, NULL);

	if(physicsSDK != NULL)
		physicsSDK->getFoundationSDK().getRemoteDebugger()->connect("localhost", 5425);

	initialized = true;
	bool sceneInit = false;
	NxSceneDesc sceneDesc;
	sceneDesc.gravity.set(0,0,-9.81f);
	NxScene *scene = physicsSDK->createScene(sceneDesc);

	if(scene != NULL)
		sceneInit = true;

	NxPlaneShapeDesc planeDesc;
	planeDesc.normal = NxVec3(0,0,1);
	planeDesc.d = 0.0f;

	NxMaterial *defaultMaterial = scene->getMaterialFromIndex(0);
	defaultMaterial->setRestitution(0.3f);
	defaultMaterial->setStaticFriction(0.5f);
	defaultMaterial->setDynamicFriction(0.5f);

	NxActorDesc actorDesc;
	NxBodyDesc bodyDesc;
	bodyDesc.angularDamping = 0.5;
	bodyDesc.linearVelocity = NxVec3(1,0,0);
	actorDesc.body = &bodyDesc;

	NxBoxShapeDesc boxDesc;
	boxDesc.dimensions = NxVec3(2.0f, 3.0f, 4.0f);
	actorDesc.shapes.pushBack(&boxDesc);
	actorDesc.density = 10.0f;
	actorDesc.globalPose.t= NxVec3(10.0f, 10.0f, 10.0f);
	scene->createActor(actorDesc)->userData=NULL;
	scene->simulate(1.0f/60.f);
	scene->flushStream();
	scene->fetchResults(NX_RIGID_BODY_FINISHED, true);
	if(physicsSDK != NULL)
	{
		if(scene != NULL)
			physicsSDK->releaseScene(*scene);
		scene = NULL;
		NxReleasePhysicsSDK(physicsSDK);
		physicsSDK = NULL;
	}
	return 0;
} 

First Blog


Welcome to GPUToaster’s Blog!!!

Please find my first introduction on Real-Time Physics and its requirement in industry. The introduction will be followed with introduction to popular physics engines. Stay tuned for tutorials on NVIDIA PhysX 2.8.1.

Tentative Publishing date: 20th August, 2010