Irrlicht + PhysX example

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
kornerr
Posts: 245
Joined: Thu Jul 06, 2006 9:57 am
Location: Russia, Siberia, Kemerovo
Contact:

Irrlicht + PhysX example

Post by kornerr »

Irrlicht 1.2 + PhysX 2.6 example [screenshot]
The example compiles under Linux. It should work under Windows too.

Makefile for Linux:

Code: Select all

NOVODEX_DIR = /home/other/apps/PhysX_2.6.2/novodex
SDK_DIR = $(NOVODEX_DIR)/SDKs
F_DIR = $(SDK_DIR)/Foundation
F_LINUX_DIR = $(F_DIR)/compiler/linux32
P_DIR = $(SDK_DIR)/Physics
P_LINUX_DIR = $(P_DIR)/compiler/linux32
PL_DIR = $(SDK_DIR)/PhysXLoader
COOK_DIR = $(SDK_DIR)/Cooking
CHAR_DIR = $(SDK_DIR)/NxCharacter

CFLAGS  += -w -m32 -DNDEBUG -DLINUX -DCORELIB -DNX32 -DNX_DISABLE_FLUIDS -DNX_DISABLE_HARDWARE
CFLAGS  += -I$(F_LINUX_DIR) -I$(F_DIR)/include -I$(F_DIR)/src -I$(F_DIR)/src/include 
CFLAGS  += -I$(P_LINUX_DIR) -I$(P_DIR)/include -I$(P_DIR)/src -I$(P_DIR)/src/include
CFLAGS  += -I$(PL_DIR)/include -I$(COOK_DIR)/Include -I$(CHAR_DIR)/include
CFLAGS  += -I$(P_DIR)/src/core -I$(P_DIR)/src/opcode -I$(P_DIR)/src/opcode/Ice
CFLAGS  += -I$(NOVODEX_DIR)/Samples/SampleCommonCode/src
CFLAGS  += -I$(NOVODEX_DIR)/Tools/NxuStream2
CFLAGS  += -I/usr/local/include/irrlicht
LIBS = -L/usr/local/lib -L/usr/X11/lib -lPhysXCore -lPhysXLoader -lpthread -lX11 -lIrrlicht

test: main.cpp
        g++ -o test main.cpp $(CFLAGS) $(LIBS)

clean:
        rm -f test
main.cpp:

Code: Select all

// This is modified first Lesson from AGEIA called "Box on a plane"
// You need to register at http://devsupport.ageia.com to get PhysX SDK
// You will find many tutorials in the PhysX SDK
// If you want to know what this all means you need to:
// 1) Learn Irrlicht examples found in Irrlicht/examples/ directory;
// 2) Learn AGEIA Lesson 101 "Box on a plane" found in the PhysX SDK.

#include <NxPhysics.h>
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace video;
using namespace scene;

NxPhysicsSDK *gps = 0;
NxScene *scn = 0;
NxVec3 def_grav (0.0f, -9.81f, 0.0f);
NxActor *plane = 0,
                *box = 0;

IrrlichtDevice *dev = 0;
ISceneManager *smgr = 0;
IVideoDriver *drv = 0;
ISceneNode *box_node = 0;

vector3df QuaternionToEuler (const NxQuat q) {
        // Code has been taken from Bouncable example
        // (shows how to use ODE with Irrlicht)
        // I use single precision (floats) here
        // If you want double precision, use doubles instead
        NxReal w, x, y, z;
        w = q.w;
        x = q.x;
        y = q.y;
        z = q.z;
        float sqw = w * w;
        float sqx = x * x;
        float sqy = y * y;
        float sqz = z * z;
        vector3df euler;
        euler.Z = (f32)(atan2 (2.0f * (x * y + z * w), (sqx - sqy - sqz + sqw)) *
                        core::GRAD_PI);
        euler.X = (f32)(atan2 (2.0f * (y * z + x * w), (-sqx - sqy + sqz + sqw)) *
                        core::GRAD_PI);
        euler.Y = (f32)(asin (-2.0f * (x * z - y * w)) * core::GRAD_PI);
        return euler;
}

NxQuat EulerToQuaternion (const vector3df v) {
        // Code has been taken from Bouncable example
        // (shows how to use ODE with Irrlicht)
        // I use single precision (floats) here
        // If you want double precision, use doubles instead
        float heading = v.Z * core::GRAD_PI2 / 2.0f;
        float attitude = v.Y * core::GRAD_PI2 / 2.0f;
        float bank = v.X * core::GRAD_PI2 / 2.0f;
        float c1 = cos (heading);
        float s1 = sin (heading);
        float c2 = cos (attitude);
        float s2 = sin (attitude);
        float c3 = cos (bank);
        float s3 = sin (bank);
        NxQuat q;
        // w
        q.w = (NxReal)(c1 * c2 * c3 + s1 * s2 * s3);
        // x
        q.x = (NxReal)(c1 * c2 * s3 - s1 * s2 * c3);
        // y
        q.y = (NxReal)(c1 * s2 * c3 + s1 * c2 * s3);
        // z
        q.z = (NxReal)(s1 * c2 * c3 - c1 * s2 * s3);
        return q;
}

NxActor* CreateGroundPlane () {
        NxPlaneShapeDesc psd;
        NxActorDesc ad;
        ad.shapes.pushBack (&psd);
        return scn->createActor (ad);
}

NxActor* CreateBox () {
        NxReal h = 20.0f; // meters high above the plane
        NxActorDesc ad;
        NxBodyDesc bd;
        NxBoxShapeDesc bsd;
        // Box, 1 x 1 x 1 units
        // If you don't believe this, read tutorials at devsupport.ageia.com
        bsd.dimensions.set (0.5f, 0.5f, 0.5f);
        ad.shapes.pushBack (&bsd);
        ad.body = &bd;
        ad.density = 10.0f;
        ad.globalPose.t = NxVec3 (0.0f, h, 0.0f);
        return scn->createActor (ad);
}

NxReal UpdateTime () {
        NxReal delta;
        static unsigned int cur_time, last_time;
        cur_time = dev->getTimer ()->getRealTime ();
        // elapsed time in milliseconds
        delta = (NxReal)((float)(cur_time - last_time) / 1000.0);
        last_time = cur_time;
        return delta;
}

void StartPhysics () {
        NxReal delta = UpdateTime ();
        scn->simulate (delta);
        scn->flushStream ();
}

void GetPhysicsResults () {
        while (!scn->fetchResults (NX_RIGID_BODY_FINISHED, false));
}

void InitNx () {
        gps = NxCreatePhysicsSDK (NX_PHYSICS_SDK_VERSION);
        if (gps == 0)
                return;
        gps->setParameter (NX_SKIN_WIDTH, 0.01f);

        NxSceneDesc scd;
        scd.gravity = def_grav;
        scn = gps->createScene (scd);

        NxMaterial *defmat = scn->getMaterialFromIndex (0);
        defmat->setRestitution (0.5f);
        defmat->setStaticFriction (0.5f);
        defmat->setDynamicFriction (0.5f);

        plane = CreateGroundPlane ();
        box = CreateBox ();

        UpdateTime ();
        if (scn)
                StartPhysics ();
}

void ExitNx () {
        if (gps != 0) {
                if (scn != 0)
                        gps->releaseScene (*scn);
                gps->release ();
        }
}

void UpdateBox () {
        NxReal *pos = box->getGlobalPosition ().get ();
        box_node->setPosition (vector3df (pos[0], pos[1], pos[2]));
        box_node->setRotation (QuaternionToEuler (
                                box->getGlobalOrientationQuat ()));
}

int main () {
        dev = createDevice (EDT_OPENGL, dimension2d<s32> (320, 240), 32, false,
                        true, false, 0);
        dev->setWindowCaption (L"Irrlicht 1.2 & PhysX 2.6");
        dev->getCursorControl ()->setVisible (false);
        drv = dev->getVideoDriver ();
        smgr = dev->getSceneManager ();
        ICameraSceneNode *cam = smgr->addCameraSceneNodeFPS (0, 50, 80);
        cam->setPosition (vector3df (12, 12, 12));
        cam->setTarget (vector3df (0, 10, 0));

        // Plane, 100 x 100 units
        IAnimatedMesh *plane_mesh = smgr->addHillPlaneMesh ("plane",
                        dimension2d<f32> (10, 10), dimension2d<s32> (10, 10));
        ISceneNode *plane_node = smgr->addAnimatedMeshSceneNode (plane_mesh);
        plane_node->setMaterialFlag (EMF_LIGHTING, false);
        plane_node->setMaterialTexture (0, drv->getTexture ("stones.jpg"));
        plane_node->setPosition (vector3df (0, 0, 0));

        // Box, 1 x 1 x 1 units
        box_node = smgr->addCubeSceneNode (1);
        box_node->setMaterialFlag (EMF_LIGHTING, false);
        box_node->setMaterialTexture (0, drv->getTexture ("wall.jpg"));

        InitNx ();
        while (dev->run ()) {
                drv->beginScene (true, true, SColor (255, 100, 100, 100));
                if (scn) {
                        GetPhysicsResults ();
                        //ProcessInputs ();
                        StartPhysics ();
                }
                UpdateBox ();
                smgr->drawAll ();
                drv->endScene ();
        }
        ExitNx ();

        return 0;
}
Open Source all the way, baby ;)
OSRPG
sio2
Competition winner
Posts: 1003
Joined: Thu Sep 21, 2006 5:33 pm
Location: UK

Post by sio2 »

It would be nice (very nice) if you converted my Irrlicht/Newton demo from my website to PhysX. :wink:

PhysX looks great. My only issue is that if I make a demo using it I have to point my users at a 28MB download in order to get the PhysX drivers. For a game that's not so much of an issue, though.
kornerr
Posts: 245
Joined: Thu Jul 06, 2006 9:57 am
Location: Russia, Siberia, Kemerovo
Contact:

Post by kornerr »

One guy had already converted some Newton demo to use PhysX :)
Open Source all the way, baby ;)
OSRPG
Slaine
Posts: 120
Joined: Fri May 04, 2007 12:28 pm

Post by Slaine »

Sorry to be a pest in bringing this oldish thread up, but didn't anyone notice the Unhandled exception in this code, on this line:

Code: Select all

NxReal *pos = box->getGlobalPosition().get();
The only reason why I've chosen this example is the fact that it doesn't have tons of programming errors in it like the tutorial on here has. Of which by the way has anyone fixed it?
Post Reply