TreeCollisionSerializer - doesn't restore correctly

subquantum

21-12-2007 01:21:53

Hi. My game builds a treecollision off of the terrain, serializes it, and uses it for the current run. In subsequent runs, it then uses the deserialized collision mesh. The first run, right after the minute long geometry creation process, everything works beautifully. Every triangle shows up when I press F3, and there are no problems. When I use the deserialized version, however, only maybe 10 triangles appear (out of the many, many thousand there are) and the program hangs whenever any objects head to the +,+ corner of my map. I really need to serialize this geometry, because waiting a minute for one map to load is pretty unacceptable, but, so is the game crashing when a projectile (or player) wanders into the corner.

Here's my serialization code: (Note, I'm not optimizing the mesh)

col->finish(false);
pman->terrainBody = new OgreNewt::Body(pman->world, col);

OgreNewt::TreeCollisionSerializer* serializer = new OgreNewt::TreeCollisionSerializer();

serializer->exportTreeCollision(col, collisionfile);


And, my deserialization code:

FILE* file = fopen(collisionfile.c_str(), "rb");

if (file) {
Ogre::DataStreamPtr ptr(new Ogre::FileHandleDataStream(file));

OgreNewt::CollisionPrimitives::TreeCollision* col = new OgreNewt::CollisionPrimitives::TreeCollision(PhysicsManager::GetSingleton()->world);
OgreNewt::TreeCollisionSerializer* serializer = new OgreNewt::TreeCollisionSerializer();

serializer->importTreeCollision(ptr, col);
PhysicsManager::GetSingleton()->terrainBody = new OgreNewt::Body(PhysicsManager::GetSingleton()->world, col);
delete col;
} else {
HeightfieldCollision* collisionGenerator = new HeightfieldCollision;
Ogre::TerrainPageSource::addListener(collisionGenerator);
}