Havok integration question.

Discussion about everything. New games, 3d math, development tips...
Post Reply
3DModelerMan
Posts: 1691
Joined: Sun May 18, 2008 9:42 pm

Havok integration question.

Post by 3DModelerMan »

Does anyone have a function that converts an IMesh into a Havok hkpExtendedMeshShape? I've been trying to write one for a while now, and I've gotten nowhere.
That would be illogical captain...

My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
DtD
Posts: 264
Joined: Mon Aug 11, 2008 7:05 am
Location: Kansas
Contact:

Post by DtD »

I don't use Havok, but here is the code we use to convert an IMesh to a btTriangleMesh for Bullet. It might be able to point you in the general direction:

Code: Select all

irr::scene::IMeshBuffer* mesh = amesh->getMesh(0)->getMeshBuffer(0);
 btTriangleMesh* btmesh = new btTriangleMesh();
 
 irr::video::S3DVertex* verticies = (irr::video::S3DVertex*)mesh->getVertices();
 irr::u16* indicies = mesh->getIndices();
 
 for(irr::u32 i=0;i < mesh->getIndexCount();i+=3)
 {
  btmesh->addTriangle(
   btVector3(
    verticies[indicies[i]].Pos.X,
    verticies[indicies[i]].Pos.Y,
    verticies[indicies[i]].Pos.Z
   ),
   btVector3(
    verticies[indicies[i+1]].Pos.X,
    verticies[indicies[i+1]].Pos.Y,
    verticies[indicies[i+1]].Pos.Z
   ),
   btVector3(
    verticies[indicies[i+2]].Pos.X,
    verticies[indicies[i+2]].Pos.Y,
    verticies[indicies[i+2]].Pos.Z
   )
  );
 }
 
 btBvhTriangleMeshShape* btshape = new btBvhTriangleMeshShape(btmesh,false);
 //...
amesh is defined as "irr::scene::IAnimatedMesh* amesh;" and was loaded in an earlier step "amesh = smgr->getMesh(worldmodel);"
luthyr
Posts: 69
Joined: Wed Dec 30, 2009 5:47 pm

Post by luthyr »

This is the function I am using to convert an IMesh to a havok rigid body. I modified the code a bit that someone else had posted to this forum a while back.

Code: Select all

hkpRigidBody* createTriangleMesh(hkpWorld* pWorld, IMesh* mesh, vector3df scale, 
                                 hkpMotion::MotionType motionType)
{
   hkpExtendedMeshShape* meshShape = new hkpExtendedMeshShape(0.0f);

   for (irr::u32 i = 0; i < mesh->getMeshBufferCount(); i++)
   {
      irr::scene::IMeshBuffer* mb = mesh->getMeshBuffer(i);

      irr::f32* vertexBuffer = new irr::f32[mb->getVertexCount() * 3];
      irr::u16* indexBuffer = mb->getIndices();

      int tmpCounter = 0;

      for (irr::u32 x = 0; x < mb->getVertexCount(); x++)
      {
         vector3df tmp = mb->getPosition(x);
         vertexBuffer[tmpCounter] = tmp.X * scale.X;
         vertexBuffer[tmpCounter+1] = tmp.Y * scale.Y;
         vertexBuffer[tmpCounter+2] = tmp.Z * scale.Z;

         tmpCounter += 3;
      }

      hkpExtendedMeshShape::TrianglesSubpart part;

      part.m_vertexBase = vertexBuffer;
      part.m_vertexStriding = sizeof(float) * 3;
      part.m_numVertices = mb->getVertexCount();

      part.m_indexBase = indexBuffer;
      part.m_indexStriding = sizeof(unsigned short)*3;
      part.m_numTriangleShapes = mb->getIndexCount()/3;
      part.m_stridingType = hkpExtendedMeshShape::INDICES_INT16;

      meshShape->addTrianglesSubpart(part);
   }

   hkpRigidBodyCinfo rbCi;
   rbCi.m_shape = meshShape;
   rbCi.m_motionType = motionType;

   hkpRigidBody* r = new hkpRigidBody(rbCi);
   pWorld->addEntity(r);
   //r->removeReference();

   return r;
}
Post Reply