Posted on

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;
} 

About GPUToaster

https://gputoaster.wordpress.com/about

18 responses to “Setting Up Your PhysX Environment

  1. Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information.

    • hi,

      Thank you “potenzmittelshop”. Please do feel free to ask any queries if you have. Do read my tutorials too and optionally rate if you want to. Also you could suggest any corrections/changes.

      With Best regards,

      GPUToaster (PUNE, INDIA)

  2. sapa ya? ⋅

    great,this is what i’m looking for
    tqq

  3. hazem ⋅

    hi GPUToaster
    … it’s really great article it was really helpful to me ..

    would you mind inform me where i can found more tutorials of physx … um working on my graduation project .. and i’m so getting lost in physx

    I would be grateful if you could help me
    thx

  4. hazem ⋅

    hi GPUToaster
    … it’s really great article it was really helpful to me ..

    would you mind inform me where i can find more tutorials about physx … um working on my graduation project .. and i’m so confused in physx

    I would be grateful if you could help me
    thx

    • Hi Hazem,
      Thanks!!
      I would suggest you to go through the NVIDIA PhysX SDK. You’ll find lots of working examples and techniques to implement them. Also look for PhysX tutorials on http://www.swiftless.com, this is one of the great site I have encountered in past. (Keep an update).
      For now you can search my blog for some more basics in the Physics section. I’m trying to bring up more and more tutorials on the same front but designing a tutorial with the novice approach requires some time, hoping to release some in immediate future… Newz Happy Coding….!!!

  5. hazem ⋅

    hi GPUToaster

    thx for ur reply …i really appreciate it …
    i just want to ask a stupid question 🙂 … forgive me … but what the result should i except from running that piece of code :)… um really sorry for annoying u 🙂

    • Hey Hazem,

      Not at all. I would rather appreciate it. newz, that’s common, when you compile and run, you expect some output. But as you can read it in the post, it is expected to compile with 0 errors and 0 warnings only. The tutorial is only for setting up the environment and if it is properly set, then it means that the program has linked to the libraries and ready to run, so dnt wry about the output.

      “If you have any other queries feel free to ask!”
      Happy coding with PhysX!!

      Regards.

  6. hazem ⋅

    Hi GPUToaster
    it’s me again 🙂
    in my project … I need to work with physx and QT and my question is … can i use physx in qt platform or i use physx in VC++ and using with it QT “ADD In”

  7. hazem ⋅

    another request plz … could you give me your email , I really need you help and it takes along time waiting your answer through the blog

  8. hazem ⋅

    I have sent u an e-mail to ur gmail email .. and i don’t know if you have delivered it or not … anyway I ‘ll ask here again just in case

    can i load 3d models from 3ds max to physx , and what about loading clothes item from 3ds max also ” t-shirt for example ”
    and how can i use 3ds max plug in

    to recognize me 🙂 my email address is
    xzeemo90@gmail.com

    • Hi Xzeemo,

      About your Query: See PhysX has nothing to do with the models. In simple sense model is a collection of vertices and triangles (or simply a Mesh), and as long as they can be loaded to the memory physx can handle them to simulate.

      You can load a model though any API available, extract the vertices data from them, convert them to NxTriangleMesh or NxConvexMesh.

      In PhysX the important concept is shapes and actors. You load or create actors because physx acts on actors only and it acts on the vertices only to simulate the effect.

      As you understand now that PhysX natively doesnt support 3ds, however there is very helpful namespace NXU, these actually have stream classes that take XML files or COLLADA files. Use the PhysX plugin in your 3D max to convert your mesh to XML formats and finally let physx to “cook” them for you. Note that Cooking is a complex task performed by PhysX API to convert them into a Convex Hull to be used for simulation.

      Hope the explanation helps you.
      Happy to help you further on this.

      Take care.

      Regards,

  9. AL ⋅

    i have followed your step and still have some errors :
    IntelliSense: identifier “scene” is undefined
    IntelliSense: identifier “physicsSDK” is undefined
    IntelliSense: identifier “NxVec3” is undefined
    etc.

  10. PK ⋅

    Hi, is opengl necessary to run this program? I tried running the program but I got this error: [fatal error C1083: Cannot open include file: ‘NxPhysics’: No such file or directory]. Would be helpful if the opengl folder is uploaded here as there are alot of versions out there. Thank you in advance

    • Hi Annisha,
      I am sorry to say that the tutorials have aged and dead as of now. Please try following other tutorials as I am not maintaining this blog anymore. In future, there are no plans on the tutorials and they have been outdated for a long time. Sry to waste your time.

      Thanks and Regard,

      GPUToaster.

Leave a reply to sapa ya? Cancel reply