4. PhysX Parameters

Jump To:
main.cpp Source
Download

Main.CPP File:

For our fourth PhysX tutorial, we are just going to set a couple of parameters for our SDK and our scene. These are all going to take place in our initNX function.

First off we are going to set a default skin width for objects in our scene. But first I will explain what this does. Each object has it’s own skin that goes around the object at the width of the skin depth we are going to set. This skin is used for interpenetration between objects to account for inaccuracy when stacking objects on top of each other. The skin allows the objects to penetrate each other, at the width of the skin. The width of the skin that should be needed depends on the size of the objects as it can lead to visible penetration. The default skin width is set to 0.025. To set the skin width we just use the following line after we set the Physics SDK:

gPhysicsSDK->setParameter(NX_SKIN_WIDTH, 0.01);

Next up, we are going to set the simulate mode for our program. PhysX 2.7 could run in software or use the old AGEIA PhysX Processing Unit (PPU). Those hardware/software flags are historical now, but for this tutorial we’ll explicitly choose the software simulation path:

sceneDesc.simType = NX_SIMULATION_SW;

For historical reference, PhysX 2.7 used the following flag for the AGEIA hardware path:

sceneDesc.simType = NX_SIMULATION_HW;

There are a couple of parameters covered (there are LOTS more). Check out the next tutorial for setting materials 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;
    }

    gPhysicsSDK->setParameter(NX_SKIN_WIDTH, 0.01f);

    NxSceneDesc sceneDesc;
    sceneDesc.gravity.set(0.0f, -9.8f, 0.0f);
    // PhysX 2.7-era software simulation mode.
    sceneDesc.simType = NX_SIMULATION_SW;
    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