convex hull v.s. user mesh

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

convex hull v.s. user mesh

Postby Neo » Tue Jul 10, 2012 9:53 am

Hey, I'm just building newton system in my irrlicht map...and I need to make everything (excloding the floor) in my map into dynamic body.
but the thing is, for a little bit more complex object, for example a wall with an arch door in it, the convex hull seems not a ideal choise,it turns out that
the collision which the function createConvexHull provided would be a wall with no arch door(I saw this in the sandbox)...

another option is to make the objects into usermesh or treecollision, however, they are static bodies. Any other idea? Thx
Neo
 
Posts: 127
Joined: Fri May 11, 2012 12:29 pm
Location: China,Sichuan

Re: convex hull v.s. user mesh

Postby Julio Jerez » Tue Jul 10, 2012 10:18 am

you can separate the arch and use NewtonMeshApproximateConvexDecomposition to make into a compound, check out ConvexApproximation.cpp in teh sandbox
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: convex hull v.s. user mesh

Postby Neo » Wed Jul 11, 2012 5:35 am

Julio Jerez wrote:you can separate the arch and use NewtonMeshApproximateConvexDecomposition to make into a compound, check out ConvexApproximation.cpp in teh sandbox

Thx, but there comes a new problem: My meshes are extracted from the irrlicht scene, and I'm quite sure that irrlicht mesh has different archetecture with the newton mesh joint.
So, because I cannot load mesh directly from .ngd file like the sdkdome did, I want to extract the vertices and faces from the irrlicht mesh(like what I did in building tree collision and convexhull collision), and somehow rebuild a newton mesh with them.
I've tried to use NewtonMeshAddFace to create the newton mesh, but it turned out totally in a mess...
So any ideas? Did I use the mesh joint functions in a right way?
Neo
 
Posts: 127
Joined: Fri May 11, 2012 12:29 pm
Location: China,Sichuan

Re: convex hull v.s. user mesh

Postby Neo » Wed Jul 11, 2012 6:12 am

here is the code:
Code: Select all
NewtonBody* mapManager::createMeshConvexHull(NewtonWorld* nWorld, scene::IMeshSceneNode* irr_meshNode, s32 shapeID,bool sync,scene::IMesh*irrmesh)
{
   //Function to create a NewtonMeshCollision from irrlicht mesh
   scene::IMesh *mesh;
   if(sync)mesh=static_cast<scene::IMeshSceneNode*>(irr_meshNode)->getMesh();//if the scene node is the mesh source, extract the mesh
   else mesh=irrmesh;//load external mesh

   NewtonMesh*cMesh=NewtonMeshCreate(nWorld);

   NewtonMeshBeginFace(cMesh);
   u32 nMeshBuffer=0;//total count of the meshBuffer
   u32 vertice_index[3]={0,0,0};//vertex index
   scene::IMeshBuffer *mesh_buffer=NULL;
   f32 vertex_array[33];//vertex array(a newtonmesh node has 11 values
   //they are vertice(vertex_array[0]~vertex_array[3])、normal(vertex_array[4]~vertex_array[6])、and uv coordinates(vertex_array[7]~vertex_array[10]))
   //as a collison,all we need is the first 4 values, plus the 4th value(w axis) has to be set to 0.

   for (nMeshBuffer=0;nMeshBuffer<mesh->getMeshBufferCount();nMeshBuffer++)
   {
      mesh_buffer=mesh->getMeshBuffer(nMeshBuffer);
      //get node and index pointer
      video::S3DVertex *vertices=(video::S3DVertex*)mesh_buffer->getVertices();
      u16*indices=mesh_buffer->getIndices();

      //fill the collision model
      for (u32 i=0;i<mesh_buffer->getIndexCount();i+=3)
      {
         //extract 3 node one time
         for(u32 j=0;j<3;j++)vertice_index[j]=indices[i+j];

         //first
         vertex_array[0]=vertices[vertice_index[0]].Pos.X;
         vertex_array[1]=vertices[vertice_index[0]].Pos.Y;
         vertex_array[2]=vertices[vertice_index[0]].Pos.Z;
         vertex_array[4]=0.0f;
         //second
         vertex_array[11]=vertices[vertice_index[1]].Pos.X;
         vertex_array[12]=vertices[vertice_index[1]].Pos.Y;
         vertex_array[13]=vertices[vertice_index[1]].Pos.Z;
         vertex_array[14]=0.0f;
         //third
         vertex_array[21]=vertices[vertice_index[2]].Pos.X;
         vertex_array[22]=vertices[vertice_index[2]].Pos.Y;
         vertex_array[23]=vertices[vertice_index[2]].Pos.Z;
         vertex_array[24]=0.0f;


         //add new face to newtonMesh
         NewtonMeshAddFace(cMesh,//destination model
            3,//total vertices for one face
            &vertex_array[0],//vertex array
            11*sizeof(f32),//the bit size of one node
            0);//material ID(no need here)
      }
   }

   NewtonMeshEndFace(cMesh);

   NewtonMesh* const convexApproximation=NewtonMeshApproximateConvexDecomposition(cMesh,0.01f,0.02f,64,200,NULL);
   NewtonCollision* const compoundC=NewtonCreateCompoundCollisionFromMesh(nWorld,convexApproximation,0.001f,shapeID,0);
   
   vector3df scale=irr_meshNode->getScale()*IrrToNewton;//irrToNewton is set to 0.1, I use this value for simulation purpose
   vector3df pos=irr_meshNode->getPosition()*IrrToNewton;
   NewtonCollisionSetScale(compoundC,scale.X,scale.Y,scale.Z);//scale the collision
   matrix4 matrix;
   matrix.makeIdentity();
   matrix.setTranslation(pos);//set the position of the newtonBody
   NewtonBody* mb=NewtonCreateBody(nWorld,compoundC,&matrix.pointer()[0]);
   
   NewtonMeshDestroy(cMesh);
   NewtonMeshDestroy(convexApproximation);
   NewtonDestroyCollision(compoundC);
   return mb;
}
Neo
 
Posts: 127
Joined: Fri May 11, 2012 12:29 pm
Location: China,Sichuan

Re: convex hull v.s. user mesh

Postby Julio Jerez » Wed Jul 11, 2012 8:38 am

you need to cinver the mesh to a NewtonMesh, by makeing one and adding the faces to it.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: convex hull v.s. user mesh

Postby Neo » Wed Jul 11, 2012 11:02 am

Julio Jerez wrote:you need to cinver the mesh to a NewtonMesh, by makeing one and adding the faces to it.

I did...as above...but it turns out that collision mesh in scene is totally a huge....stuff...I cannot even tell want is it...it's not like the original mesh at all...just messy faces stick together...
I just wonder what's wrong with my function(see above)?
Neo
 
Posts: 127
Joined: Fri May 11, 2012 12:29 pm
Location: China,Sichuan


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest