For me most of the time it works, only sometimes it seems that the convex hull is too big, and other times it is too small, and other times it doesn't exist, but most of the time it works and it's accurate. But on my friends system it crashes often.
This leads me to believe that there is some nasty memory corruption in this function, maybe due to the way I pass the float array? Or maybe not enough or too much memory allocated? I'm running all out of ideas. If there is anyone out there that could point me to what's wrong with this function, I would greatly appriciate it!
Anyway, here's the function:
Code: Select all
CollisionMesh CreateNewtonCollisionConvex(IAnimatedMesh *Mesh)
{
debugout << "Creating collision mesh.";
// Check if the animated mesh passed exists.
if (!Mesh)
{
debugout << "CreateNewtonCollision : error : mesh passed to the function does not exist.";
}
// 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;
debugout << "Passed the initialisation stuff.";
// Loop through all vertices and add them to an array of float that newton wants.
for (int bi = 0 ; bi < BufferCount ; bi++)
{
debugout << "Looping mesh buffer nr : " << bi;
mb = MyMesh->getMeshBuffer(bi);
debugout << "Pointer to mesh buffer : " << mb;
varray = new float[mb->getVertexCount()*3];
debugout << "Pointer to varray : " << varray;
debugout << "sizeof(varray) : " << sizeof(varray);
int VerticleCount = mb->getVertexCount();
debugout << "VertexCount : " << VerticleCount;
S3DVertex* Vertices = (S3DVertex*)mb->getVertices();
for (int ii = 0 ; ii < mb->getVertexCount() ; ii += 3)
{
debugout << "Looping through verticle nr : " << ii;
varray[ii] = Vertices[ii].Pos.X;
varray[ii+1] = Vertices[ii].Pos.Y;
varray[ii+2] = Vertices[ii].Pos.Z;
}
}
// Finalize and return result.
debugout << "Finalizing...";
Collision = NewtonCreateConvexHull(nWorld,mb->getVertexCount(),(float*)varray,(sizeof(float)*3),NULL);
Body = NewtonCreateBody(nWorld,Collision);
CollisionMesh result;
result.Body = Body;
result.Collision = Collision;
debugout << "Pointer to Body : " << Body;
debugout << "Pointer to Collision : " << Collision;
debugout << "Done.";
return result;
}