It's a bit hard to test, as the demo's keep crashing sort of randomly, throwing an assertion in a vector or math operation. Anyhow:
- Default floor - demo works, but crashes at some point
- No floor - demo works, players falls deep down, then the demo crashes at some point
- My quad floor - demo crashes after 1 second, before hitting the floor
It doesn't like the testFloor CollisionTree I made for sure, though it crashes in all circumstances anyway, so it might be coincidence. Anyhow, code below. Added a function to create the test floor, replaced parts of the original floor construction code.
Back in my own program, I noticed:
- It works when inserting Boxes into the SceneCollision, instead of Trees. I can downscale objects now.
- It doesn't work with trees
- It neither works with the simplified floormesh (as I tried in the Newton demo)
Now I thought, maybe the mesh itself is a problem? The standard demo floor is a 6-quad box (right?).
In my case, floors or walls are just triangles without volume. A floor or wall could be
a flat quad (made of 2 tris). Basically I just re-use the visual mesh for Newton. And that visual mesh could be anything really.
I must say that I always had trouble with stuff falling through the world sometimes, in older versions of Newton (1,2). So maybe the root of the problem lays in the trees, or the way how I construct them.
- Code: Select all
// Make a 2-triangle flat test floor
NewtonCollision* testCreateFlatFloor(DemoEntityManager* const scene)
{
NewtonCollision* tree = NewtonCreateTreeCollision(scene->GetNewton(), 0);
NewtonTreeCollisionBeginBuild(tree);
dFloat face[3][3];
face[0][0] = +50; face[0][1] = 0; face[0][2] = -50;
face[1][0] = -50; face[1][1] = 0; face[1][2] = -50;
face[2][0] = -50; face[2][1] = 0; face[2][2] = +50;
NewtonTreeCollisionAddFace(tree, 3, &face[0][0], 36, 0);
face[0][0] = +50; face[0][1] = 0; face[0][2] = -50;
face[1][0] = -50; face[1][1] = 0; face[1][2] = +50;
face[2][0] = +50; face[2][1] = 0; face[2][2] = +50;
NewtonTreeCollisionAddFace(tree, 3, &face[0][0], 36, 0);
NewtonTreeCollisionEndBuild( tree, 0);
return tree;
} // testCreateFlatFloor
static void LoadFloor(DemoEntityManager* const scene, NewtonCollision* const sceneCollision)
{
NewtonWorld* const world = scene->GetNewton();
// add a flat plane
dMatrix matrix(dGetIdentityMatrix());
DemoEntityManager::dListNode* const floorNode = LoadScene(scene, "flatPlane.ngd", matrix);
DemoEntity* const entity = floorNode->GetInfo();
DemoMesh* const mesh = (DemoMesh*)entity->GetMesh();
dAssert (mesh->IsType(DemoMesh::GetRttiType()));
// Replace original code with testFloor
/* NewtonCollision* const tree = NewtonCreateTreeCollision(world, 0);
NewtonTreeCollisionBeginBuild(tree);
dFloat* const vertex = mesh->m_vertex;
for (DemoMesh::dListNode* node = mesh->GetFirst(); node; node = node->GetNext()){
DemoSubMesh* const subMesh = &node->GetInfo();
unsigned int* const indices = subMesh->m_indexes;
int trianglesCount = subMesh->m_indexCount;
for (int i = 0; i < trianglesCount; i += 3) {
dFloat face[3][3];
for (int j = 0; j < 3; j ++) {
int index = indices[i + j] * 3;
face[j][0] = vertex[index + 0];
face[j][1] = vertex[index + 1];
face[j][2] = vertex[index + 2];
}
// index = indices[i + 1] * 3;
// face[1] = dVector (vertex[index + 0], vertex[index + 1], vertex[index + 2]);
// index = indices[i + 2] * 3;
// face[2] = dVector (vertex[index + 0], vertex[index + 1], vertex[index + 2]);
int matID = 0;
//matID = matID == 2 ? 1 : 2 ;
NewtonTreeCollisionAddFace(tree, 3, &face[0][0], 3 * sizeof (dFloat), matID);
}
}
NewtonTreeCollisionEndBuild (tree, 1);
*/
NewtonCollision* const tree = testCreateFlatFloor(scene);
// add the collision tree to the collision scene
void* const proxy = NewtonSceneCollisionAddSubCollision (sceneCollision, tree);
// destroy the original tree collision
NewtonDestroyCollision (tree);
// set the parameter on the added collision share
matrix = entity->GetCurrentMatrix();
NewtonCollision* const collisionTree = NewtonSceneCollisionGetCollisionFromNode (sceneCollision, proxy);
NewtonSceneCollisionSetSubCollisionMatrix (sceneCollision, proxy, &matrix[0][0]);
NewtonCollisionSetUserData(collisionTree, entity);
// set the application level callback, for debug display
#ifdef USE_STATIC_MESHES_DEBUG_COLLISION
NewtonStaticCollisionSetDebugCallback (collisionTree, ShowMeshCollidingFaces);
#endif
}