PhysX (v2.7.3) & Irrlicht (v1.4)

A forum to store posts deemed exceptionally wise and useful
Yustme
Posts: 107
Joined: Sat Dec 01, 2007 10:50 pm

PhysX (v2.7.3) & Irrlicht (v1.4)

Post by Yustme »

Hi,

I have managed to get the "PhysX integration" tutorial to work with the latest version of both engines (see title for version reference).

The link to the tutorial on the site:

http://irrlicht.sourceforge.net/tut_physx.html

I recommend you to read that tutorial first and download the project.

I have made a few modifications to the code.

There are still things that could be improved, but i don't have the time for it right now.

Feel free to contribute on the project for the community :)

Now that is said, lets move on to the tutorial.

You'll need to copy the "CAnimatedMeshSceneNode.h" and "CAnimatedMeshSceneNode.cpp" to your project directory.

After that, rename the file names from:

"CAnimatedMeshSceneNode.h" and "CAnimatedMeshSceneNode.cpp".

To:

"CPhysXAnimatedMeshSceneNode.h" and "CPhysXAnimatedMeshSceneNode.cpp".

And the class name "CAnimatedMeshSceneNode", constructor "CAnimatedMeshSceneNode" and destructor "~CAnimatedMeshSceneNode".

To:

"CPhysXAnimatedMeshSceneNode".

And all the namespaces in the "CAnimatedMeshSceneNode.cpp" file.

Instead of naming them "CPhysXAnimatedMeshSceneNode", you can choose whatever you like. But keep in mind that they have to match.


For example this is my class:

Code: Select all

class CPhysXAnimatedMeshSceneNode : public IAnimatedMeshSceneNode

And this is my constructor:

Code: Select all

//! constructor
		CPhysXAnimatedMeshSceneNode(IAnimatedMesh* mesh, NxActor* actor, ISceneNode* parent, ISceneManager* mgr,	s32 id,
			const core::vector3df& position = core::vector3df(0,0,0),
			const core::vector3df& rotation = core::vector3df(0,0,0),
			const core::vector3df& scale = core::vector3df(1.0f, 1.0f, 1.0f));

And a few examples for the namespaces (open CAnimatedMeshSceneNode.cpp to change them):


Change:

Code: Select all

void CAnimatedMeshSceneNode::setCurrentFrame(f32 frame)

To:

Code: Select all

void CPhysXAnimatedMeshSceneNode::setCurrentFrame(f32 frame)

Another example about the namespace:

Change:

Code: Select all

f32 CAnimatedMeshSceneNode::getFrameNr() const

To:

Code: Select all

f32 CPhysXAnimatedMeshSceneNode::getFrameNr() const

Ok, back to the constructor:

Code: Select all

CPhysXAnimatedMeshSceneNode(IAnimatedMesh* mesh, NxActor* actor, ISceneNode* parent, ISceneManager* mgr,	s32 id,
			const core::vector3df& position = core::vector3df(0,0,0),
			const core::vector3df& rotation = core::vector3df(0,0,0),
			const core::vector3df& scale = core::vector3df(1.0f, 1.0f, 1.0f));

The "NxActor*" will keep a pointer to the AnimatedMesh object to do the physics simulation for it.

Add this function:

Code: Select all

IAnimatedMeshSceneNode* addPhysXAnimatedMeshSceneNode(IAnimatedMesh* mesh, NxActor* actor, ISceneNode* parent=0, s32 id=-1,
	const core::vector3df& position = core::vector3df(0,0,0), const core::vector3df& rotation = core::vector3df(0,0,0), 
	const core::vector3df& scale = core::vector3df(1.0f, 1.0f, 1.0f))
{
	if (!mesh)
	return 0;

	if (!parent)
	parent = smgr->getRootSceneNode();

	IAnimatedMeshSceneNode* node = new 
						CPhysXAnimatedMeshSceneNode(mesh, actor, parent, smgr, id, position, rotation, scale);
	
	node->drop();

	return node;
}

To your main class.

If you want to create a mesh with physics simulation capabilities, all you have to do is, call this function:

Code: Select all

addPhysXAnimatedMeshSceneNode(SomeMesh, SomeActor);

For example, to create a sphere:

Code: Select all

void CreateASphere()
{
	f32 dim = 20.0f; // the typical dimension/radius is 20
	NxActor *newActor; // pointer to newly created object
	ISceneNode *newNode; // pointer to its scene node
	
	vector3df position = camera->getPosition(); 
	vector3df target = camera->getTarget();
	
	core::vector3df irrDir = (target - position).normalize(); // get and normalize the direction
	
	// create the physics model of the sphere
	newActor = CreateSphere(position, dim);
	
	newNode = addPhysXAnimatedMeshSceneNode(sphereMesh, newActor);
	newNode->setMaterialFlag(video::EMF_LIGHTING, false);
	newNode->setMaterialTexture(0, driver->getTexture("data/earth.bmp"));

	newNode->setScale(core::vector3df(dim,dim,dim));
	ApplyForceToActor(newActor, NxVec3(irrDir.X, irrDir.Y, irrDir.Z), gForceStrength);
}

The "void CreateASphere" function will call the function:

Code: Select all

NxActor* CreateSphere(const core::vector3df &pos, const f32 &radius)

Which will create the actual physics model of the sphere mesh.

This is how the complete function for the physics model looks like for the sphere object:

Code: Select all

NxActor* CreateSphere(const core::vector3df &pos, const f32 &radius)
{
	// Add a single-shape actor to the scene
	NxActorDesc actorDesc;
	NxBodyDesc bodyDesc;
	bodyDesc.angularDamping = 0.2f;

	// The actor has one shape, a box, 1m on a side
	NxSphereShapeDesc sphereDesc;
	sphereDesc.radius = radius; // should be 0.5
	actorDesc.shapes.pushBack(&sphereDesc);

	actorDesc.body = &bodyDesc;
	actorDesc.density = 1.0f;
	actorDesc.globalPose.t = NxVec3(pos.X,pos.Y,pos.Z);
	return gScene->createActor(actorDesc);	
}

The project also covers on how to create a box mesh object and the physics model for it.


If you want to be able to load quake 3 maps, then this is how it's done:

Code: Select all

// load a quake 3 map
device->getFileSystem()->addZipFileArchive("data/nemesis.pk3");
IAnimatedMesh* q3levelmesh = smgr->getMesh("data/nemesis.bsp");

The nemesis map is from afecelis which i have included in the zip file (hope he doesn't mind).

The PhysX integration project contains another quake 3 map which you could use to play around with, also included in the zip file.

Change the name nemesis.pk3 to "physicstest.pk3" and "nemesis.bsp" to "physicstest.bsp" in order to use that map instead.

To create your own quake 3 map, you should follow this tutorial from afecelis:

http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=12100

I did not include every texture, lib and dll's in the zip file which I have used in the project.

You'll need to get the SDK from Ageia your self and the textures can be obtained from the "irrlicht\media" folder.

I have included 2 quake 3 maps and a sphere and cube mesh in the zip file.

None of this is mine, so you should check the license before using it for commercial purposes.

Ok, that's it. Feel free to ask your questions about the project.

The project link:

http://www.g0dsoft.com/yustme/Projects/Irrlicht&PhysX/
Last edited by Yustme on Sun Feb 17, 2008 10:17 am, edited 3 times in total.
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Little note:
AGEIA PhysX License wrote:Free SDK Package:

* Most Current PC Binary
* Commercial & non-commercial use on PC
o Must keep registration information current
o Must agree to the EULA at the time of download (pops up prior to download)
o Available for Windows & Linux
o No PhysX hardware optimization requirement
* PS3 platform (through Sony pre-purchase)
* All platforms through some of our middleware partnerships, such as UE3, Gamebryo 2.2, and others
Except that, good job Yustme, I'll be sure to look at what you did and probably because of the PhysX license I'm gonna use your code in my project. So thanks a Lot!

P.S
Add this tutorial to the Irrlicht's Wiki.
Last edited by MasterGod on Sun Feb 17, 2008 12:32 am, edited 3 times in total.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
twilight17
Posts: 362
Joined: Sun Dec 16, 2007 9:25 pm

Post by twilight17 »

Great Tutorial! Thx for posting :D
Post this userbar I made on other websites to show your support for Irrlicht!
Image
http://img147.imageshack.us/img147/1261 ... wernq4.png
Yustme
Posts: 107
Joined: Sat Dec 01, 2007 10:50 pm

Post by Yustme »

Hi,

You're welcome guys :wink:
Virion
Competition winner
Posts: 2148
Joined: Mon Dec 18, 2006 5:04 am

Post by Virion »

8) nice
Falcon
Posts: 36
Joined: Sun Jan 13, 2008 9:24 pm
Location: Z_California

Post by Falcon »

I can't compile your code. I get these errors:

Code: Select all

1>------ Build started: Project: PhysXIrrlichtExample, Configuration: Debug Win32 ------
1>Compiling...
1>CIrrEventReceiver.cpp
1>CPhysXAnimatedMeshSceneNode.cpp
1>Main.cpp
1>c:\programming\physx\physxirrlichtexample\physxirrlichtexample\physxirrlichtexample\main.cpp(41) : error C2259: 'irr::scene::CPhysXAnimatedMeshSceneNode' : cannot instantiate abstract class
1>        due to following members:
1>        'irr::u32 irr::scene::IAnimatedMeshSceneNode::getJointCount(void) const' : is abstract
1>        c:\programming\devirrlicht\trunk\include\ianimatedmeshscenenode.h(138) : see declaration of 'irr::scene::IAnimatedMeshSceneNode::getJointCount'
1>c:\programming\physx\physxirrlichtexample\physxirrlichtexample\physxirrlichtexample\main.cpp(448) : warning C4244: '=' : conversion from 'double' to 'NxReal', possible loss of data
1>Stream.cpp
1>Generating Code...
1>Build log was saved at "file://c:\Programming\PhysX\PhysXIrrlichtExample\PhysXIrrlichtExample\PhysXIrrlichtExample\Debug\BuildLog.htm"
1>PhysXIrrlichtExample - 1 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
-< Dante >
< Industry Senior QA Tester>
< IT Project Manager>
< Independent Programmer>-
Falcon
Posts: 36
Joined: Sun Jan 13, 2008 9:24 pm
Location: Z_California

Post by Falcon »

Ah, nevermind, I just needed to add the getJointCount() to your example and it works. That's what I get for using the latest unreleased Irrlicht :).
-< Dante >
< Industry Senior QA Tester>
< IT Project Manager>
< Independent Programmer>-
frostysnowman
Posts: 83
Joined: Fri Apr 11, 2008 3:06 am
Location: Beaverton, Oregon, US

Post by frostysnowman »

I get a linker error:
LINK : fatal error LNK1104: cannot open file 'NxExtensions.lib'

I can't find NxExtensions.lib from the PhysX sdk. Where is it from?
shadowslair
Posts: 758
Joined: Mon Mar 31, 2008 3:32 pm
Location: Bulgaria

Post by shadowslair »

Try do a Win search in the folder "SDK"

Mine`s in

C:\Program Files\AGEIA Technologies\SDK\v2.8.0\SDKs\lib\Win32

Hopefully yours will be there too. :wink:
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
frostysnowman
Posts: 83
Joined: Fri Apr 11, 2008 3:06 am
Location: Beaverton, Oregon, US

Post by frostysnowman »

I have the newest version of the sdk, so the file path is :
"C:\Program Files\NVIDIA Corporation\NVIDIA PhysX SDK\v2.8.1\SDKs\lib\Win32"

Because nVidia bought Ageia I believe. The only files in this folder are:

NxCharacter.lib

NxCooking.lib

PhysXLoader.lib


Maybe the new version of the sdk doesn't come with NxExtensions.lib? Could someone perhaps send me NxExtensions.lib? Or is that illegal...

An BTW: I have already searched the entire physX sdk for NxExtensions.lib. Don't think I hadn't already tried that :P
Yustme
Posts: 107
Joined: Sat Dec 01, 2007 10:50 pm

Post by Yustme »

Hi frostysnowman,

Try to compile the code by removing the "NxExtensions.lib" from the libraries in the options.

Not sure what you use but if its visual studio, then right click on the project name in the solution explorer and choose properties.

After that go to linker -> input -> Additional dependencies

And remove there the library and recompile the code.

If you don't see the "linker" then you probably got the wrong properties window.

Remember, you have to press right mouse button on the projects name the one without starting with "Solution".

Yustme
frostysnowman
Posts: 83
Joined: Fri Apr 11, 2008 3:06 am
Location: Beaverton, Oregon, US

Post by frostysnowman »

Thanks for your help Yustme. I did what you said, and it compiled. However, after a few seconds after startup, I get an exception.
Unhandled exception at 0x004282b9 in PhysXIrrlichtExample.exe: 0xC0000005: Access violation reading location 0x00000000.
On the call stack, it lists the last command as

Code: Select all

NxTriangleMesh *q3mapTriangleMesh = gPhysicsSDK->createTriangleMesh(readBuffer);
In main.cpp.

:(
shadowslair
Posts: 758
Joined: Mon Mar 31, 2008 3:32 pm
Location: Bulgaria

Post by shadowslair »

I`ll upload my .lib. Wait a minute...
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
shadowslair
Posts: 758
Joined: Mon Mar 31, 2008 3:32 pm
Location: Bulgaria

Post by shadowslair »

Here you are:

http://www.mediafire.com/?yzmwbym3m2n

I hope this will do the job. This is the whole win32 folder.
"Although we walk on the ground and step in the mud... our dreams and endeavors reach the immense skies..."
frostysnowman
Posts: 83
Joined: Fri Apr 11, 2008 3:06 am
Location: Beaverton, Oregon, US

Post by frostysnowman »

I put it in my lib directory, re added it to the linker additional dependencies, and it still crashed the same way :(

I'm very frustrated because I have never got a single PhysX + Irrlicht example to compile and run fine from source!!
Post Reply