requires nWorld global (newton world)
I was too lazy to put it as a function argument but i guess you could do that.
Code: Select all
/////////////////////////////////////////////////////////////////////////////
struct CollisionMesh
{
NewtonCollision* Collision;
NewtonBody* Body;
};
/////////////////////////////////////////////////////////////////////////////
CollisionMesh CreateNewtonCollisionConvex(IAnimatedMesh *Mesh)
{
// get a mesh from the animated mesh. (default first frame)
IMesh* MyMesh = Mesh->getMesh(0);
// Init stuff
int BufferCount = MyMesh->getMeshBufferCount();
IMeshBuffer* mb = 0;
NewtonBody* Body;
NewtonCollision* Collision;
float *varray = 0;
// Loop through all vertices and add them to an array of float that newton wants.
for (int bi = 0 ; bi < BufferCount ; bi++)
{
mb = MyMesh->getMeshBuffer(bi);
int VerticleCount = mb->getVertexCount();
debugout << "VertexCount : " << VerticleCount;
varray = new float[VerticleCount*3];
switch(mb->getVertexType())
{
case EVT_STANDARD:
{
S3DVertex* Vertices = (S3DVertex*)mb->getVertices();
for (int ii = 0 ; ii < VerticleCount ; ii++)
{
varray[(ii*3)] = Vertices[ii].Pos.X;
varray[(ii*3)+1] = Vertices[ii].Pos.Y;
varray[(ii*3)+2] = Vertices[ii].Pos.Z;
}
break;
}
case EVT_2TCOORDS:
{
S3DVertex2TCoords* Vertices = (S3DVertex2TCoords*)mb->getVertices();
for (int ii = 0 ; ii < VerticleCount ; ii++)
{
varray[(ii*3)] = Vertices[ii].Pos.X;
varray[(ii*3)+1] = Vertices[ii].Pos.Y;
varray[(ii*3)+2] = Vertices[ii].Pos.Z;
}
break;
}
}
}
// Finalize and return result.
Collision = NewtonCreateConvexHull(nWorld,mb->getVertexCount(),(float*)varray,(sizeof(float)*3),NULL);
delete varray;
Body = NewtonCreateBody(nWorld,Collision);
CollisionMesh result;
result.Collision = Collision;
result.Body = Body;
return result;
}