PhysX rotation question

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

PhysX rotation question

Post by Seven »

I need to be able to rotate a PhysX actor manaully and cannot figure it out. Can someone help me out?

Code: Select all

void CSObject::SetRotation(vector3df rot)
{
	if(rot.X > 360) rot.X = rot.X-360;
	if(rot.X < 0) rot.X = 360+rot.X;
	if(rot.Y > 360) rot.Y = rot.Y-360;
	if(rot.Y < 0) rot.Y = 360+rot.Y;
	if(rot.Z > 360) rot.Z = rot.Z-360;
	if(rot.Z < 0) rot.Z = 360+rot.Z;

	m_Rotation = rot;

	if (m_NxActor) 
	{     
// this is the missing part		
	}

	if (m_Node)
	{
		m_Node->setRotation(rot);
	}

	if (m_AnimatedSceneNode) 
	{
		m_AnimatedSceneNode->setRotation(rot);
	}
}

I have tried just about everything I know and I still do not get it. Any help is greatly appreciated.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

m_NxActor->setGlobalOrientation(NxMat33)....?

Surely? Have you not read the Physx API?
Image Image Image
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

certainly, and please dont call me shirley. (for the young uns, it's a movie quote)


anyhow, as much as I hate to have to ask, can you be more specific about creating the matrix for the rotation?



is this even close or am I completely lost on this?

Code: Select all

	if (m_NxActor) 
	{     
		
		NxMat33 mat;
		float A       = cos(rot.X);
		float B       = sin(rot.X);
		float C       = cos(rot.Y);
		float D       = sin(rot.Y);
		float E       = cos(rot.Z);
		float F       = sin(rot.Z);
		float AD      =   A * D;
		float BD      =   B * D;

		mat.setRow(0,NxVec3(C * E, -C * F, D));
		mat.setRow(1,NxVec3(BD * E + A * F, -BD * F + A * E, -B * C));
		mat.setRow(2,NxVec3(-AD * E + B * F, AD * F + B * E, A * C));

		m_NxActor->setGlobalOrientation(mat);	
	}
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

solved

Code: Select all

	if (m_NxActor) 
	{     
		core::matrix4 irrMat;
		irrMat.setRotationDegrees(rot);
		NxMat33 m;
		m.setColumnMajorStride4(&irrMat.pointer()[0]);
		m_NxActor->setGlobalOrientation(m);	
	}
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

We have clearance, Clarence.
Image Image Image
Ion Dune
Posts: 453
Joined: Mon Nov 12, 2007 8:29 pm
Location: California, USA
Contact:

Post by Ion Dune »

Code: Select all

NxVec3
What's our vector, Victor?
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

Roger Under, Over.
charles88
Posts: 29
Joined: Tue Nov 04, 2008 10:02 am

Post by charles88 »

Wow! thank you very much seven and JP. :)
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

No problem. For anyone else stumbling on troubles integrating irrlicht with Physx and getting the correct way to convert vectors, matrices and rotations etc between the two SDKs then feel free to check out IrrPhysx. It's got some good conversion functions in there which you're free to steal if you don't want to actually use IrrPhysx :)
Image Image Image
charles88
Posts: 29
Joined: Tue Nov 04, 2008 10:02 am

Post by charles88 »

Thx JP. Yeah, I have browsed through your wrapper, and I saw the function:

Code: Select all

createTriangleMeshObject (IPhysxMesh *  mesh,  
  const core::vector3df &  position,  
  const core::vector3df &  rotation = core::vector3df(0, 0, 0)   
 ) 
)
You said it creates a static object, so how can I create a dynamic object? Any suggestion for that? Because I am load my node from a .X file and want to create a dynamic actor for it.

:(. Please help, thx once again.[/code]
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Well what is the object? What do you want to do with it? Check out the examples and you'll see some various things you could do. You could create a convex mesh object from it, like the letter models in the mesh example or you could create a box/sphere/capsule object for it. Or you could create an object which is built up from various boxes/spheres/capsules to create a more complicated shape which might wrap nicely around your model.

Is it a model which will just be a non-moving object that would like be possible to knock over but not actually move by itself, only if hit by something? Or is it a character/enemy in your game which you want to have moved around?
Image Image Image
charles88
Posts: 29
Joined: Tue Nov 04, 2008 10:02 am

Post by charles88 »

Thx JP for your reply, yeah it's a model that I load from a .x file. I put the model's verticies and indices into a triangle mesh but then it became static, even though I set property for body to be dynamic.

So I should build up various shapes just to cover up my entire model? Is there any easy way of doing it? Is it I must first get the skeleton of the model? :) Sorry but I have no clue on how to do it that's why I cooked the mesh in the triangle mesh.

Or any thread that post this problem will be helpful.

I will look at your wrapper again tomorrow, hopefully can find lots of clues from it. :)
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Triangle meshes in Physx are always static, you can't make them dynamic, I guess that may be for performance reasons or something...

So this is an animated character in your game? In that case i would suggest a ragdoll approach, so basically make up a ragdoll in Physx and attach it to the model's skeleton, then you should basically have shapes around all the body parts of your model for collision. Then what you'd do is let the animation drive the transformation of the ragdoll body parts when your character is walking around and then if you want your character to die and crumple into a heap then you just flip the process on its head and instead let the ragdoll physics objects dictate where the model's bones should be. This is something i'm planning for (probably) the next version of IrrPhysx but i don't know when that will be released, can't make any promises! If you do any of this stuff yourself i'd love to hear how it goes and what problems you may or may not run into!
Image Image Image
charles88
Posts: 29
Joined: Tue Nov 04, 2008 10:02 am

Post by charles88 »

Thx again JP, I am working on it now. :) will post if I encounter any problem.
charles88
Posts: 29
Joined: Tue Nov 04, 2008 10:02 am

Post by charles88 »

Hi JP, before I look into ragdoll. I have created a convex class, but what I have here is that my convex mesh is also always static. :( Here is the code:

Code: Select all

NxConvexMesh *getIrrPhysxConvexMesh(NxPhysicsSDK *gPhysicsSDK, IMesh *irrMesh,const vector3df &pos, const vector3df &scale)
{
	s32 meshBufferCount = irrMesh->getMeshBufferCount();

	array<NxVec3> vertices;
	array<NxU32> indices;

	NxU32 tempIndexCount = 0;

	for (int i = 0; i < meshBufferCount; ++i)
	{
		IMeshBuffer *mb = irrMesh->getMeshBuffer(i);

		s32 numVertices = mb->getVertexCount();
		s32 numIndices = mb->getIndexCount();

		switch (mb->getVertexType())
		{
		case EVT_2TCOORDS:
			{
				S3DVertex2TCoords *mbVertices = (S3DVertex2TCoords*)mb->getVertices();
				for (int j = 0; j < numVertices; ++j)
				{
					NxVec3 ver = NxVec3(mbVertices[j].Pos.X * scale.X, mbVertices[j].Pos.Y * scale.Y, mbVertices[j].Pos.Z * scale.Z);
					vertices.push_back(ver);
				}

				break;
			}
		case EVT_STANDARD:
			{
				S3DVertex *mbVertices = (S3DVertex*)mb->getVertices();
				for (int j = 0; j < numVertices; ++j)
					vertices.push_back(NxVec3(mbVertices[j].Pos.X * scale.X, mbVertices[j].Pos.Y * scale.Y, mbVertices[j].Pos.Z * scale.Z));

				break;
			}
		case EVT_TANGENTS:
			{
				S3DVertexTangents *mbVertices = (S3DVertexTangents*)mb->getVertices();
				for (int j = 0; j < numVertices; ++j)
					vertices.push_back(NxVec3(mbVertices[j].Pos.X * scale.X, mbVertices[j].Pos.Y * scale.Y, mbVertices[j].Pos.Z * scale.Z));

				break;
			}
		default :
			{
				break;
			}
		}

		u16 * mbIndices = mb->getIndices();

		for (int j = 0; j < numIndices; ++j)
		{
			NxU32 in = mbIndices[j] + tempIndexCount;
			indices.push_back(in);
		}

		tempIndexCount += numVertices;
	}

	NxConvexMeshDesc convexMeshDesc;    
	convexMeshDesc.numVertices = vertices.size();   
	std::cout<<"vectices size "<<vertices.size()<<std::endl;
	//convexMeshDesc.numTriangles = indices.size() / 3;
	//std::cout<<"No. Triangles "<<indices.size()/3<<std::endl;

	convexMeshDesc.pointStrideBytes = sizeof(NxVec3); 
	//convexMeshDesc.triangleStrideBytes = 3*sizeof(NxU32);

	convexMeshDesc.points = vertices.const_pointer(); 
	//convexMeshDesc.triangles = indices.const_pointer();    
	convexMeshDesc.flags = NX_CF_COMPUTE_CONVEX; 

	MemoryWriteBuffer buf;
	std::cout<<"Cook Convex Mesh: "<<CookConvexMesh(convexMeshDesc, buf)<<std::endl;
	MemoryReadBuffer readBuffer(buf.data);

	NxConvexMesh *convexMesh = gPhysicsSDK->createConvexMesh(readBuffer);

	indices.clear();
	vertices.clear();

	return convexMesh;
}

NxActor *createIrrPhysxConvexMesh(NxPhysicsSDK *gPhysicsSDK, NxScene *gScene, IMesh * irrMesh, const vector3df &pos, const vector3df &scale, bool dynamic)
{
	if (!irrMesh)
		return 0;
       
	NxConvexShapeDesc convexShapeDesc;        
	convexShapeDesc.meshData = getIrrPhysxConvexMesh(gPhysicsSDK,irrMesh,pos,scale);     
	
	NxBodyDesc BodyDesc;
	BodyDesc.angularDamping = 0.5f;
	BodyDesc.mass = 100.0f;
	BodyDesc.massLocalPose.t = NxVec3(0,0,0);

	NxActorDesc actorDesc;            
	actorDesc.shapes.pushBack(&convexShapeDesc); 

	if(dynamic)
		actorDesc.body = &BodyDesc;
	else
		actorDesc.body = NULL;

	actorDesc.density = 10.0f; 
	actorDesc.globalPose.t  = NxVec3(pos.X,pos.Y,pos.Z);            
	           
	
	NxActor *actor = gScene->createActor(actorDesc);

	return actor;
}
Is there anything wrong in there? Please help, thanks.
Post Reply