3. PhysX Scenes

Jump To:
main.cpp Source
Download

Main.CPP File:

Here’s our third PhysX tutorial, and it doesn’t look like we are getting anywhere, but this tutorial is going to start getting into the PhysX SDK. We are going to create a scene which will calculate all our physics interactions. Just like OpenGL needs a window to draw to, PhysX needs a scene to simulate a world with. But don’t interperet this wrong, the PhysX SDK DOES NOT do any rendering for you.

So, continuing on from the previous tutorial, we need to create a new global variable that will be the scene for our PhysX world. This is going to be called gScene, and takes the type NxScene. We will set it like so:

NxScene* gScene = NULL;

So we have a scene, what do we do with it? First off we are going to set the most basic of properties, gravity. So we need to create a scene descriptor which will hold the properties for our scene. We are going to start off with creating a variable for our scene descriptor called sceneDesc like so:

NxSceneDesc sceneDesc;

Now that we have a scene descriptor, we need to set our gravity:

 sceneDesc.gravity.set(0.0f, -9.8f, 0.0f);

And now we are going to create our scene:

gScene = gPhysicsSDK->createScene(sceneDesc);

So we now have a scene that we can add objects to, and adjust its properties as needed. Eg: In the game Prey, we change the gravity direction throughout to allow for a new type of gameplay.

To close up our program, we need to go to our deinitNX function and release our scene:

gPhysicsSDK->releaseScene(*gScene);
gScene = 0;

So we now have a scene, in our next tutorial we are going to start playing with some different parameters. You can check it out here.

#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;
NxScene *gScene = NULL;

void deinitNX(void) {
    if (gScene != NULL && gPhysicsSDK != NULL) {
        gPhysicsSDK->releaseScene(*gScene);
        gScene = NULL;
    }
    if (gPhysicsSDK != NULL) {
        gPhysicsSDK->release();
        gPhysicsSDK = NULL;
    }
}

bool initNX(void) {
    gPhysicsSDK = NxCreatePhysicsSDK(NX_PHYSICS_SDK_VERSION);
    if (gPhysicsSDK == NULL) {
        return false;
    }

    NxSceneDesc sceneDesc;
    sceneDesc.gravity.set(0.0f, -9.8f, 0.0f);
    gScene = gPhysicsSDK->createScene(sceneDesc);
    if (gScene == NULL) {
        deinitNX();
        return false;
    }

    return true;
}

void display(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    glLoadIdentity();
    glutSwapBuffers();
}

void reshape(int w, int h) {
    if (h == 0) h = 1;
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 0.1, 100.0);
    glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Physics");
    glEnable(GL_DEPTH_TEST);

    if (!initNX()) {
        return 1;
    }
    std::atexit(deinitNX);

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    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
  • 1