PhysX 3.2 – Getting Started

This tutorial covers up how to initialize and successfuly shutdown PhysX in a console application.
After some struggle on starting using physx, I have decided to create this small and simple tutorial, so you can start working with it right away.
To start a PxPhysics object with the 3.2 version one must first create a PxFoundation object filled with an error and allocator callbacks.

static PxDefaultErrorCallback gDefaultErrorCallback;
static PxDefaultAllocator gDefaultAllocatorCallback;
PxFoundation *mFoundation = NULL;
// create foundation object with default error and allocator callbacks.
mFoundation = PxCreateFoundation(
PX_PHYSICS_VERSION,
gDefaultAllocatorCallback,
gDefaultErrorCallback);
// create Physics object with the created foundation and with a 'default' scale tolerance.
mPhysics = PxCreatePhysics( PX_PHYSICS_VERSION, *mFoundation, PxTolerancesScale());

Ok, from this point your can start creating and filling up your PxScene object.
Lastly, don’t forget to release everything


mPhysics->release();
 mFoundation->release();

Here is the full code for the app:

 #include <stdio.h>
 #include <PxPhysicsAPI.h>

using namespace std;
 using namespace physx;

// link libraries
 #pragma comment(lib, "PhysX3CHECKED_x86.lib")
 #pragma comment(lib, "PhysX3CommonCHECKED_x86.lib")
 #pragma comment(lib, "PhysX3ExtensionsCHECKED.lib")

// Physx Physics
 PxPhysics *mPhysics = NULL;

void initPhysX() {
 static PxDefaultErrorCallback gDefaultErrorCallback;
 static PxDefaultAllocator gDefaultAllocatorCallback;
 PxFoundation *mFoundation = NULL;

printf("initializing PhysX\n");

printf("creating Foundation\n");
 // create foundation object with default error and allocator callbacks.
 mFoundation = PxCreateFoundation(
 PX_PHYSICS_VERSION,
 gDefaultAllocatorCallback,
 gDefaultErrorCallback);

printf("creating Physics\n");
 // create Physics object with the created foundation and with a 'default' scale tolerance.
 mPhysics = PxCreatePhysics(
 PX_PHYSICS_VERSION,
 *mFoundation,
 PxTolerancesScale());

printf("PhysX initialized\n");

printf("shutting down\n");
 mPhysics->release();
 mFoundation->release();
 }

int main() {
 initPhysX();

return 0;
 }

Don’t forget to add the PhysX \include and Lib\win32\ directories to the project.
Also, you need to add the following dlls to the project directory:

  • PhysX3CHECKED_x86.dll
  • PhysX3CommonCHECKED_x86.dll

9 thoughts on “PhysX 3.2 – Getting Started

  1. Márcio Felipe Laubstein January 9, 2013 / 17:39

    Thanks friend I was linking against PhysX3_x86.lib instead of PhysX3CHECKED_x86.lib and I was getting a lot of unresolve external symbols…

    I’m not an experienced C++ programmer so I don’t know why when I try to create the PhysX object with a profile zone manager to enable the performance profiling capabilities of the PhysX Visual Debugger:

    mProfileZoneManager = &PxProfileZoneManager::createProfileZoneManager(mFoundation);
    if(!mProfileZoneManager) {
    fatalError(“PxProfileZoneManager::createProfileZoneManager failed!”);
    }
    bool recordMemoryAllocations = true;
    PxCreatePhysics(PX_PHYSICS_VERSION, *mFoundation, PxTolerancesScale(), recordMemoryAllocations, mProfileZoneManager)

    I am getting the following error:
    Error 1128 error LNK2019: unresolved external symbol “public: static class physx::PxProfileZoneManager & __cdecl physx::PxProfileZoneManager::createProfileZoneManager(class physx::PxFoundation *)” (?createProfileZoneManager@PxProfileZoneManager@physx@@SAAAV12@PAVPxFoundation@2@@Z) referenced in function “public: void __thiscall StartUp::Go(void)” (?Go@StartUp@@QAEXXZ) D:\Projetos\Laubstein\Game\Game\StartUp.obj Game

    Any clue about it?

    Thanks! 🙂

    • hruivo January 9, 2013 / 21:50

      try adding this additional dependency to the linker’s input
      PhysXProfileSDKCHECKED.lib

      Here’s the full list of dependencies I have used, in case you need any additional ones:
      PhysX3CHECKED_x86.lib
      PhysX3CommonCHECKED_x86.lib
      PhysX3ExtensionsCHECKED.lib
      PhysXVisualDebuggerSDKCHECKED.lib
      PhysXProfileSDKCHECKED.lib
      PxTaskCHECKED.lib

      Cheers.

      • Márcio Felipe Laubstein January 10, 2013 / 15:04

        It worked.. Just link against all the libs that you mentioned. After spending a few hours I finally integrate PhysX with OGRE3D. I created a plane and throw some boxes into it and they collided right. Now I will try to do the character controller to work in my project.

        Cheers.

  2. hruivo January 10, 2013 / 22:38

    nice to hear mate. And good luck with your character controller.

    Cheers.

  3. Sunrise January 20, 2013 / 11:18

    Hey,

    I was trying for the last day, to resolve this problem but I couldn’t since I’ve no experience in VS2010 nor PhysX, I’m trying to learn it, for a project but I’m stuck in this basic :p .

    I keep getting this error :

    PhysX3ExtensionsCHECKED.lib(ExtDefaultErrorCallback.obj) : error LNK2038: mismatch detected for ‘_ITERATOR_DEBUG_LEVEL’: value ‘0’ doesn’t match value ‘2’ in Example.obj

    the Example is like the one you’re suggesting, with an OpenGL window.

    Have you any idea, please !!

    • hruivo January 20, 2013 / 22:07

      hm, don’t know if this is the problem but… are you compiling in DEBUG mode?

      • Sunrise January 20, 2013 / 22:33

        yes, when I’m building solution !!

      • Sunrise January 20, 2013 / 22:35

        I’m using the last PhysX 3.2.2

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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