IrrPhysx 0.2 - Nvidia Physx 2.8.1 wrapper

Announce new projects or updates of Irrlicht Engine related tools, games, and applications.
Also check the Wiki
specter
Posts: 3
Joined: Sat Jun 20, 2009 3:03 pm

Post by specter »

thanks for your answer, Dorth!

i searched the physx sdk docs and found the answer:
Collision Groups
The first condition checks the shapes collision group membership. All shapes can be assigned to a collision group with the following call:

NxShape::setGroup(NxCollisionGroup)
The CollisionGroup is simply an integer between 0 and 31. For example, the below call assigns our shape to the 11th group:

myShape->setGroup(11);
All shapes start out in group 0, but this does not really come with any built in meaning. The SDK maintains a 32x32 symmetric Boolean matrix which identifies whether or not shapes of a particular group should be collided against shapes of another group. Initially, all group pairs are enabled. You can change the entries of the matrix with the following call:

NxScene::setGroupCollisionFlag(CollisionGroup g1, CollisionGroup g2, bool enable);
For example, the below call disables collisions between group 11 and group 0:

gScene->setGroupCollisionFlag(11, 0, false);
Collision groups are useful if you know ahead of time that there are certain kinds of actors which should not collide with certain other kinds.

Actors can also be assigned to groups. Actor groups are orthogonal to shape groups, even though they are used for similar things. They work the same way, however there are 0x7fff possible actor groups, instead of only 32 as with shapes. This allows for more flexibility. First, each actor can be assigned to a group as shown below:

myActor->setGroup(333);
By default each actor starts out in group 0. Next, set up relationships between pairs of actor groups:

gScene->setActorGroupPairFlags(333,334, NX_NOTIFY_ON_START_TOUCH | NX_NOTIFY_ON_END_TOUCH);
A combination of the following flags are permitted:

NX_NOTIFY_ON_START_TOUCH, NX_NOTIFY_ON_END_TOUCH, NX_NOTIFY_ON_TOUCH.

See the section on Triggers for an explanation of these flags.

so, i set the groups correctly but the wrapper has no call for the setGroupCollisionFlag. i just have to build this myself into the wrapper. i guess this will be not a big problem.

the setCollisionGroup function is in the CPhysxObject class:

Code: Select all

void CPhysxObject::setCollisionGroup(u16 group) {

	IPhysxObject::setCollisionGroup(group);

	Actor->setGroup(group);
    NxShape*const* shapes = Actor->getShapes();
    NxU32 nShapes = Actor->getNbShapes();
    while (nShapes--)
        shapes[nShapes]->setGroup(group;)
	
}
so i will implement a function which calls the setGroupCollisionFlag function. any suggestion where and how to do this?
specter
Posts: 3
Joined: Sat Jun 20, 2009 3:03 pm

Post by specter »

ok, i solved my problem and maybe this is useful for somone else. i added just the call to the function "setGroupCollisionFlag" into the wrapper.

i put it into "CPhysxManager.cpp" because i think there it belongs:

Code: Select all

.
.
.
void CPhysxManager::setGroupCollisionFlag(int g1, int g2, bool enable)	{
	Scene->setGroupCollisionFlag(g1, g2, enable);
}
.
.
.
the definition goes to "CPhysxManager.h":

Code: Select all

.
.
.
void setGroupCollisionFlag(int g1, int g2, bool enable);
.
.
.
and finaly to "IPhysxManager.h"

Code: Select all

.
.
.
/**
			\brief Sets the defined collision groups to either collide with each other or not
			\param g1 - collision group1
			\param g2 - collision group2
			\param enable - bool value if the two groups should collide or not
			*/
			virtual void setGroupCollisionFlag(int g1, int g2, bool enable) = 0;
.
.
.
so, nothing fancy and it works for me.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Glad you managed to get that working, I believe I've already added this kind of support to the next version of IrrPhysx so it'll be there when that eventually gets released... probably not for a month or two though :(
Image Image Image
yamashi
Posts: 82
Joined: Sat Jan 03, 2009 4:53 am

Post by yamashi »

Well for me nothing works at all so yea I am kinda disappointed...
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Post your problems and we might be able to help :)

Does it compile? Does it link? Does it run? What platform? What compiler/IDE?
Image Image Image
yamashi
Posts: 82
Joined: Sat Jan 03, 2009 4:53 am

Post by yamashi »

VS08, vista 64bits.
It compiles, link, runs but when I create a terrain scene node and send it to the physX manager, my camera will collide but with random shapes, I tried to draw the debug data but when I do I get a SEGFAULT on a SMaterial or something...
I litteraly copy pasted the heightmap code to my project and it doesn't work... even if i use your heightmaps :/
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Hmm well vista and 64bit is well beyond anything i could test out I'm afraid.. You could always try debugging it if it's causing you issues ;)
Image Image Image
yamashi
Posts: 82
Joined: Sat Jan 03, 2009 4:53 am

Post by yamashi »

Tried but can't load the symbols and I couldn't find a way to correct the problems :/
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

That version of IrrPhysx might not have a debug build setup so you'd have to set that up yourself by linking to the necessary debug libs.
Image Image Image
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

design request

Post by Seven »

JP,
I load multiple worlds at startup and then switch between them for differing reasons. Of particular use to me is being able to switch to a phsx capable menu system where I have activity goin on behind the menu choices. With this in mind, I have to modify IrrPhyx so that the (SDK) is not part of the manager, so that the manager class really becomes a IrrPhysxScene class which houses explosions and whatnot bt doesnt create the SDK. i hope this makes sense. anyhow, the request is that the original design reflect this thinking. the way IrrPhyx is currently laid out, there is only one scene available and i really need more.

any thoughts on this?
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Feel free to make whatever changes to the SDK you need to make in order to use it within your project, that's part of the reason it's opensource :)

I probably won't change the way IrrPhysx is designed myself though as I've designed it to abstract Physx away completely from the user so they only have to worry about Irrlicht types and don't have the learn anything about Physx... Sure it's probably a bit limited to do it that way but what the heck!
Image Image Image
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

fair enough. thanks for the wrapper to start with though, you are really doing a nice job. looking forward to the next release!
yamashi
Posts: 82
Joined: Sat Jan 03, 2009 4:53 am

Post by yamashi »

I got it working.
I had to modify the createHeightfieldObject function.
Here is the modified function :

Code: Select all

IPhysxObject* CPhysxManager::createHeightfieldObject(scene::ITerrainSceneNode* terrain, u32 smoothFactor) {


	if (!Scene || !terrain) return NULL;

	// Get the heightmap path 
	io::IAttributes* attribs = Device->getFileSystem()->createEmptyAttributes(); 
	if (!attribs) return NULL;
	terrain->serializeAttributes(attribs); 
	core::stringc heightmapName = attribs->getAttributeAsString("Heightmap"); 
	attribs->drop();
					
	// Get the terrain's parameters
	video::IImage* heightmap = Device->getVideoDriver()->createImageFromFile(heightmapName.c_str());
	u32 dimension = heightmap->getDimension().Width;
	core::vector3df position = terrain->getPosition();
	core::vector3df scale = terrain->getScale();
	core::vector3df centre = terrain->getTerrainCenter() - position;
	core::vector3df rotation = -terrain->getRotation();

	NxHeightFieldDesc heightFieldDesc;
	heightFieldDesc.nbColumns = dimension;
	heightFieldDesc.nbRows = dimension;
	heightFieldDesc.convexEdgeThreshold = 0;

	// allocate storage for samples
	heightFieldDesc.samples = new NxU32[dimension*dimension];
	heightFieldDesc.sampleStride = sizeof(NxU32);

	// Grab the raw heightmap data
	char* currentByte = (char*)heightFieldDesc.samples;
	for (NxU32 row = 0 ; row < dimension ; ++row) {
		for (NxU32 column = 0 ; column < dimension ; ++column) {			
			NxHeightFieldSample* currentSample = (NxHeightFieldSample*)currentByte;

			currentSample->height = (NxI16)((heightmap->getPixel(row,column).getLuminance() - centre.Y)*scale.Y);
			currentSample->tessFlag = 1;

			currentByte += heightFieldDesc.sampleStride;
		}
	}	
	heightmap->drop(); // done with this

	// Smooth it out to match Irrlicht's ITerrainSceneNode's smoothing
	u32 run;
	u32 index;
	for (run = 0 ; run < smoothFactor ; ++run) {
		currentByte = (char*)heightFieldDesc.samples;
		currentByte += heightFieldDesc.sampleStride * 2;
		for (index = 2 ; index < (dimension * dimension - 1) ; ++index) {
			NxHeightFieldSample* currentSample = (NxHeightFieldSample*)currentByte;
			currentSample->height = (NxI16) ROUND( ( ((NxHeightFieldSample*)(currentByte - heightFieldDesc.sampleStride * 2))->height +
											         ((NxHeightFieldSample*)(currentByte - heightFieldDesc.sampleStride    ))->height +
											         ((NxHeightFieldSample*)(currentByte + heightFieldDesc.sampleStride    ))->height +
									                 ((NxHeightFieldSample*)(currentByte + heightFieldDesc.sampleStride * 2))->height   ) / 4.0f );
			currentByte += heightFieldDesc.sampleStride;
		}
	}

	for (run = 0 ; run < smoothFactor ; ++run) {
		currentByte = (char*)heightFieldDesc.samples;
		currentByte += heightFieldDesc.sampleStride * dimension;
		for (index = dimension ; index < (dimension * (dimension - 2)) ; ++index) {
			NxHeightFieldSample* currentSample = (NxHeightFieldSample*)currentByte;
			currentSample->height = (NxI16) ROUND( ( ((NxHeightFieldSample*)(currentByte - heightFieldDesc.sampleStride * dimension))->height +
											         ((NxHeightFieldSample*)(currentByte + heightFieldDesc.sampleStride * dimension))->height   ) / 2.0f );
			currentByte += heightFieldDesc.sampleStride;
		}
	}

	NxHeightField* heightField = PhysicsSDK->createHeightField(heightFieldDesc);

	// data has been copied, we can free our buffer
	delete [] heightFieldDesc.samples;

	NxHeightFieldShapeDesc heightFieldShapeDesc;
	heightFieldShapeDesc.heightField	= heightField;
	heightFieldShapeDesc.shapeFlags		= NX_SF_FEATURE_INDICES | NX_SF_VISUALIZATION;
	heightFieldShapeDesc.heightScale	= 1;
	heightFieldShapeDesc.rowScale		= -scale.X;
	heightFieldShapeDesc.columnScale	= scale.Z;
	heightFieldShapeDesc.materialIndexHighBits = 0;
	heightFieldShapeDesc.holeMaterial = 2;
	NxMat33 rot;
	rot.id();
	heightFieldShapeDesc.localPose = NxMat34(rot, NxVec3(centre.X-40, centre.Y*scale.Y, centre.Z) - NxVec3((dimension+1)*-scale.X/2.f,0,(dimension-1)*scale.Z/2.0f));

	NxActorDesc actorDesc;
	actorDesc.shapes.pushBack(&heightFieldShapeDesc);
	actorDesc.body = NULL;
	actorDesc.globalPose.t = NxVec3(position.X, position.Y, position.Z);
	rotationVectorToPhysxMatrix(rotation, actorDesc.globalPose.M);
	NxActor* actor = Scene->createActor(actorDesc);

	return createPhysxObject(actor, EOT_HEIGHTFIELD);

}
There is just a little bug with smoothing, the physic mesh is not really smoothed at the same spot.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Thanks for the code! I'll look at putting it into the next version of IrrPhysx when I next look at it!
Image Image Image
grumpymonkey
Posts: 222
Joined: Mon Jan 19, 2009 10:03 pm
Location: Miami, Florida
Contact:

Post by grumpymonkey »

does this have a character controller?
Image
Post Reply