I fixed the problem anyway... I hadn't put the NewtonCollisionForEachPolygonDo command inside the driver->Beginscene and driver->Endscene functions.
For some reason only one of the debug meshes is drawn...

However, it seems clear what the problem is.. The collision mesh looks nothing like the rendered scene node! far too few tris to make the collision accurate.
Here is the code I use to produce the convex hull:
- Code: Select all
//Function to create a NewtonCollision from irrlicht mesh
NewtonCollision *CreateCollisionFromMesh(NewtonWorld *nWorld,IMesh *mesh)
{
//Get number of vertices
u32 nVertices = 0, nMeshBuffer;
for( nMeshBuffer = 0 ; nMeshBuffer < mesh->getMeshBufferCount(); ++nMeshBuffer)
{
IMeshBuffer *buffer = mesh->getMeshBuffer(nMeshBuffer);
nVertices += buffer->getVertexCount();
}
// allocate block for positions of every vertex in mesh, no need to delete
// anything, the array cleans up for us.
core::array<f32> vertices;
vertices.reallocate(nVertices * 3);
//Get mesh buffers and copy face vertices
for( nMeshBuffer = 0 ; nMeshBuffer < mesh->getMeshBufferCount(); ++nMeshBuffer)
{
scene::IMeshBuffer *buffer = mesh->getMeshBuffer(nMeshBuffer);
// handle the irrlicht supported vertex types
switch(buffer->getVertexType())
{
case video::EVT_STANDARD:
{
video::S3DVertex* verts = (video::S3DVertex*)buffer->getVertices();
for(u32 v = 0; v < buffer->getVertexCount(); ++v)
{
vertices.push_back(verts[v].Pos.X);
vertices.push_back(verts[v].Pos.Y);
vertices.push_back(verts[v].Pos.Z);
}
}
break;
case video::EVT_2TCOORDS:
{
video::S3DVertex2TCoords* verts = (video::S3DVertex2TCoords*)buffer->getVertices();
for(u32 v = 0; v < buffer->getVertexCount(); ++v)
{
vertices.push_back(verts[v].Pos.X);
vertices.push_back(verts[v].Pos.Y);
vertices.push_back(verts[v].Pos.Z);
}
}
break;
case video::EVT_TANGENTS:
{
video::S3DVertexTangents* verts = (video::S3DVertexTangents*)buffer->getVertices();
for(u32 v = 0; v < buffer->getVertexCount(); ++v)
{
vertices.push_back(verts[v].Pos.X);
vertices.push_back(verts[v].Pos.Y);
vertices.push_back(verts[v].Pos.Z);
}
}
break;
default:
return 0; // don't know vertex type! bail.
}
}
//Create Newton collision object
return NewtonCreateConvexHull(nWorld, nVertices, &vertices[0], sizeof(f32)*3,0.1f,0,NULL);
}
This is called inside my int main function like so:
- Code: Select all
//Create two Sobjects
SObject Sobj1;
SObject Sobj2;
//Load mesh and create new irrlicht node
Sobj1.irr_node = smgr->addMeshSceneNode(smgr->getMesh("sphere.3ds")->getMesh(0));
Sobj2.irr_node = smgr->addMeshSceneNode(smgr->getMesh("sphere.3ds")->getMesh(0));
Sobj1.irr_node->getMaterial(0).EmissiveColor.set(255,255,0,255);
Sobj2.irr_node->getMaterial(0).EmissiveColor.set(255,0,255,255);
Sobj1.irr_node->setDebugDataVisible(EDS_HALF_TRANSPARENCY | EDS_BBOX_ALL);
Sobj2.irr_node->setDebugDataVisible(EDS_HALF_TRANSPARENCY | EDS_BBOX_ALL);
//Now create collision mesh from irrlicht mesh
Sobj1.nwtn_collision = CreateCollisionFromMesh(nWorld,Sobj1.irr_node->getMesh());
Sobj2.nwtn_collision = CreateCollisionFromMesh(nWorld,Sobj2.irr_node->getMesh());
//Set position
Sobj1.irr_node->setPosition(vector3df(100,0,0));
Sobj2.irr_node->setPosition(vector3df(0,0,0));
Sobject isjust a struct containing a newton collision and an irrlicht scene node:
- Code: Select all
//Struct for collision object
struct SObject
{
//Irrlicht scene node
IMeshSceneNode *irr_node;
//Newton collision object
NewtonCollision *nwtn_collision;
};