Friday, May 27, 2011

PhysX3: Getting started

There has been a major revamp of the PhysX API from version 3. I will try to convert all of the existing tutorials into PhysX3 so here I go with the first getting started tutorial.
Visual studio2008 project include directories
To ensure that you can follow smoothly along with me in the rest of the tutorials, make sure that the visual studio project directories contain the following directories in the include path. (This option is accessible through Tools->Options->Projects & Solutions->VC++ Directories. Here YOUR_PHYSX_PATH is where the PhysX3 folder is stored on your harddisk.)
YOUR_PHYSX_PATH\SDKs\PhysXAPI
YOUR_PHYSX_PATH\SDKs\PhysXAPI\foundation
YOUR_PHYSX_PATH\SDKs\PhysXAPI\extensions
YOUR_PHYSX_PATH\SDKs\PhysXAPI\common
YOUR_PHYSX_PATH\SDKs\PhysXAPI\pxtask\include
YOUR_PHYSX_PATH\Samples\SampleFramework\framework\include

Lib path
This path is added in the lib paths.
YOUR_PHYSX_PATH\SDKs\lib\win32

Getting started with NVIDIA PhysX3
We need to include the headers first.

#include <iostream>
#include <gl/freeglut.h>
#include <pxphysicsapi.h>
#include <pxdefaulterrorcallback.h>
#include <pxdefaultallocator.h>

using namespace std;

#pragma comment(lib, "PhysX3_x86.lib")
#pragma comment(lib, "Foundation")
#pragma comment(lib, "PhysX3Extensions.lib")


The PhysX header is now PxPhysicsAPI.h. This header is needed for PhysX. The rest are for c++ output/input stuff and freeglut API stuff. I like to programmatically add the linker libraries. For this basic startup demo, we need PhysX3_x86.lib/dll, Foundation.lib/dll, PhysX3Extensions.lib/dll so we link to them.

const int WINDOW_WIDTH=1024,
WINDOW_HEIGHT=768;

static PxPhysics* gPhysicsSDK = NULL;
static PxDefaultErrorCallback gDefaultErrorCallback;
static PxDefaultAllocator gDefaultAllocatorCallback;

IN the above lines, we setup the window dimensions. Then we store the PhysX object pointer. In the previous sdk, only first parameter was needed now all of the parameters are needed. These include the allocator and error callback handlers. The sdk provides default handlers so we use them.


void InitializePhysX() {
gPhysicsSDK = PxCreatePhysics(PX_PHYSICS_VERSION, gDefaultAllocatorCallback, gDefaultErrorCallback, PxTolerancesScale() );
if(gPhysicsSDK == NULL) {
cerr<<"Error creating PhysX device."<<endl;
cerr<<"Exiting..."<<endl;
exit(1);
}
}
void ShutdownPhysX() {
gPhysicsSDK->release();
}

Like before, we call the PxCreatePhysics function and pass it all the parameters. If the call succeeds, we get the object otherwise we prompt an error and exit. For release, we need to call the release function of the sdk object reference.

Thats it. The code should not give u any errors. To make sure everything is fine and there are no problems, copy PhysX3_x86.dll to the current solution directory. Hope you will enjoy the rest of the tutorials.
Source code

16 comments:

blueskin said...

Hey,
A quick update: "MAX_PATH" is not defined and there's no info I can find on this variable.
-K

Mobeen said...

Hi bluskin,
The MAX_PATH constant refers to the max filepath on windows. If not defined on you machine, you can define it a globally like this
const int MAX_PATH = 256;

blueskin said...

Hey Mobeen,
Thanks for your samples, they work really well. I need your advice regarding an application am working on. I have a C++ app that loads a mesh from PLY file and I want to simulate it as a cloth using NVIDIA APEX. Where should I start? Looks like APEX1.0 is not compatible with PhysX 3.0 SDK? Please feel free to share any experience on cloth simulation using any physics engine. Thanks again
Best,
-K

Mobeen said...

Hi blueskin,
Thanks for your appreciation. I did cloth and soft bodies in PhysX 2.8 however, I have not seen enough support in PhysX 3 for cloth. I would need to see if physX3 supports cloth since the samples that ship with physX3 are pathetically done and a skim through the sdk docs did not give me anything related to cloth in pysx3.
As for your application, you can use the previous PhysX release (physx2.8.x) which has a pretty decent cloth support. I will see if i could do a sample on this.

blueskin said...

Hi Mobeen,
Thanks for your prompt response. Ya, the documentation for these libraries is terrible. I created a VS project from samples given in APEX1.0 library and it took me almost 20 minutes to just find out all the required includes directories for the sample. Now I get linking errors not sure what libraries I should be linking these with. Either NVIDIA did a really bad job of documenting or I am not looking at the right place. By chance if you happen to have a Visual Studio project compiling APEX1.0 samples, could you share what libraries I should be including in the linker?
Thank you.
-K

blueskin said...

Okay, after going through some folders randomly in the APEX library, I found the following location contains a solution file for APEX samples:
APEX SDK\1.0\PhysX_2.8.4\win\samples\compiler\vc9WIN32-PhysX_2.8.4
Hope this is useful for someone

Mobeen said...

Thanks for the info blueskin

Shouwei Li said...

Hi,
Thanks for you excellent job for beginners.
Would you please introduce how to setup an environment based on PhysX 3.1 and Visual Studio 2010?
I have a lot of link errors in this build environment. I tried to get help from google, but no result. Nearly all the document are based on PhysX 2.8 and VS2010.
Thanks in advance,

Shouwei Li said...
This comment has been removed by the author.
Wabash said...

#pragma comment(lib, "Foundation")

This line returns a link error for me. Where is "Foundation" located, I can't find it.

Mobeen said...

Hi Shouwei Li,
I have already converted my tutorials to PhysX3.2.1. The libraries have changed so I think I would have to write a migration guide for those using the latest PhysX 3.2.1 library. If you are using PhysX 3.2.1, there is no Foundation library any more. For you to get started, here is the complete bare minimum startup code to init PhysX 3.2.1

#include
#include

#include
#include
#include

/*
//Includes
D:\Libraries\PhysX-3.2.1\Include;D:\Libraries\freeglut-2.8.0\include;$(IncludePath)
//libs
D:\Libraries\freeglut-2.8.0\lib\x86\Debug;D:\Libraries\PhysX-3.2.1\Lib\win32;$(LibraryPath)
*/
using namespace std;
using namespace physx;

#pragma comment(lib, "PhysX3_x86.lib")
#pragma comment(lib, "PhysX3Common_x86.lib")
#pragma comment(lib, "PhysX3Extensions.lib")

const int WINDOW_WIDTH=1024,
WINDOW_HEIGHT=768;


static PxPhysics* gPhysicsSDK = NULL;
static PxDefaultErrorCallback gDefaultErrorCallback;
static PxDefaultAllocator gDefaultAllocatorCallback;

void InitializePhysX() {
PxFoundation* foundation = PxCreateFoundation(PX_PHYSICS_VERSION, gDefaultAllocatorCallback, gDefaultErrorCallback);
if(!foundation)
cerr<<"PxCreateFoundation failed!"<release();
}

void InitGL() {
}

void OnReshape(int nw, int nh) {
glViewport(0,0,nw, nh);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)nw / (GLfloat)nh, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
}

void OnRender() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glutSwapBuffers();
}

void OnShutdown() {
ShutdownPhysX();
}
void main(int argc, char** argv) {

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("GLUT PhysX 3.2.1 Demo - Getting started");

glutDisplayFunc(OnRender);
glutReshapeFunc(OnReshape);
glutCloseFunc(OnShutdown);

InitGL();
InitializePhysX();

glutMainLoop();
}

Mobeen said...

blogger stripped my headers. The five headers are
#include < iostream >
#include < GL/freeglut.h >

#include < PxPhysicsAPI.h >
#include < extensions/PxDefaultErrorCallback.h >
#include < extensions/PxDefaultAllocator.h >

gisel88 said...

Hi, are there any of your tutorials available in version 3.2 of PhysX? Simple bouncing box or something simple like this, I would like to get started with PhysX, but im not entirely sure about how to do this with new version of this library

Muhammad Mobeen Movania said...

Hi gisel88,
Check this page http://mmmovania.blogspot.sg/2011/10/converting-existing-physx30-tutorials.html

This is conversion of all of the tutorials to PhysX3.1, v 3.2 is similar so i dont think it will be too difficult to follow.

alexsandrf1111 said...
This comment has been removed by the author.
Muhammad Mobeen Movania said...

Hi alexsandrf1111,
In opengl 3.3 you will have to maintain the matrix stack yourself. I think you can use glm library for matrix handling. See if this helps,
Mobeen

Popular Posts

Copyright (C) 2011 - Movania Muhammad Mobeen. Awesome Inc. template. Powered by Blogger.