1. PhysX Includes

1. PhysX Includes tutorial screenshot

Jump To:
main.cpp Source
Download

Main.CPP File:

Welcome to the first of MANY PhysX tutorials helping you to start off with Ageia’s PhysX SDK. If you haven’t got the SDK already, head to Ageia’s website and download it. You will need it for these tutorials.

The original setup for this tutorial copied the PhysX 2.7 headers and libraries into Visual Studio’s global VC98\Include and VC98\Lib folders. Don’t do that on a modern development machine. If you are deliberately reproducing the old PhysX 2.7 environment, keep the SDK in its own folder and add its include/library paths to the project instead. The API examples below remain the original PhysX 2.7 code. So lets test it out. The first lines of code we are going to use are:

#include <Physics\include\NxPhysics.h>
#define NOMINMAX

Note: you must #define NOMINMAX before including windows.h.

Next up we are going to attach our library files. These are:

#pragma comment(lib,"PhysXLoader.lib")
#pragma comment(lib,"NxCharacter.lib")
#pragma comment(lib,"NxCooking.lib")
#pragma comment(lib,"NxExtensions.lib")

Now you can test this out, by just having a simple int main() { return 0; } function to see if it will compile if you want, but I am going to go just one step further and create our SDK for use. We first need a NxPhysicsSDK type variable which is going to be called gPhysicsSDK (If you have played with the PhysX SDK before, you will notice that alot of my variables are named the same as theirs, simply because I learnt it that way 😛 So here’s our gPhysicsSDK variable:

static NxPhysicsSDK* gPhysicsSDK = NULL;

Now that we have something to create our PhysicsSDK with, we go to our int main() function which will start off like so:

int main(int argc, char** argv) {
return 1;
}

Now that we have a main function, we are going to set out gPhysicsSDK. we do this with the following line:

gPhysicsSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION);

This will create the Physics SDK using the current PhysX version you have installed on your computer. In my case, version 2.7.0 as of this tutorial.

If this creates properly, check that the pointer is not NULL. We also need to release our Physics SDK so we can clean up and close our program, this is done with the following line:

gPhysicsSDK->release();

So our main function will look like:

int main(int argc, char** argv) {
    gPhysicsSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION);
    if (gPhysicsSDK == NULL) {
        return 1;
    }
    gPhysicsSDK->release();
    gPhysicsSDK = NULL;
    return 0;
}

Now just compile and run this program. If it works, we are ready to start some serious coding. If it doesn’t compile, double-check the PhysX 2.7 include/library paths and make sure the SDK architecture matches the project you are building.

It may not seem like much, but once you get all this done, IMO you have just completed the hardest part in beginning with the SDK.

The next tutorial here is about creating a GLUT window to draw to for when we get to rendering our Physics

#include <cstdlib>
#include <stdio.h>
#include <GL/glut.h>
#include <GL/gl.h>
#define NOMINMAX
#include <windows.h>
#include <Physics/include/NxPhysics.h>

#pragma comment(lib, "PhysXLoader.lib")
#pragma comment(lib, "NxCharacter.lib")
#pragma comment(lib, "NxCooking.lib")
#pragma comment(lib, "NxExtensions.lib")

static NxPhysicsSDK *gPhysicsSDK = NULL;

int main(int argc, char **argv) {
    gPhysicsSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION);
    if (gPhysicsSDK == NULL) {
        return 1;
    }

    gPhysicsSDK->release();
    gPhysicsSDK = NULL;
    return 0;
}

Source & downloads

Questions or corrections? If something doesn’t work or you spot something I’ve got wrong, leave a comment below. These tutorials have been around a long time, so useful corrections are always welcome!

  • March 25, 2010
  • 7