danni
22-10-2006 16:01:47
This has had me baffled for the longest time and i have tried just about every solution i could see on the forums.
I am building a collision mesh per the tutorial and a few threads on the forum here.
First problem, I can press f3 and see the mesh while it is building, once it is complete f3 crashes the application. Same once the serialized data is loaded completely. I saw others having this issue, but no solutions.
Second, everything falls through the terrain like no collisions are occurring. Just for the sake of ruling potential sources out, i added a buoyancy plane for the water on the level, collides fine with that, so the issue is the generated collision mesh for the terrain. Any clues what i am missing here?
I am building a collision mesh per the tutorial and a few threads on the forum here.
First problem, I can press f3 and see the mesh while it is building, once it is complete f3 crashes the application. Same once the serialized data is loaded completely. I saw others having this issue, but no solutions.
Second, everything falls through the terrain like no collisions are occurring. Just for the sake of ruling potential sources out, i added a buoyancy plane for the water on the level, collides fine with that, so the issue is the generated collision mesh for the terrain. Any clues what i am missing here?
#include "TerrainListener.h"
#include "PlayState.h"
TerrainManager::TerrainManager(PagingLandScapeSceneManager* sceneMgr, OgreNewt::World *m_World, OgreNewt::MaterialID *mat) {
floormat = mat;
mSceneMgr = sceneMgr;
mWorld = m_World;
tileLoadDelegate=new PagingLandscapeDelegate();
tileLoadDelegate->bind(this,&TerrainManager::tileLoaded);
mSceneMgr->setOption("addLoadTileListener",tileLoadDelegate);
tileUnloadDelegate=new PagingLandscapeDelegate();
tileUnloadDelegate->bind(this,&TerrainManager::tileUnloaded);
mSceneMgr->setOption("addUnloadTileListener",tileUnloadDelegate);
mSceneMgr->setOption("LoadNow", NULL);
}
TerrainManager::~TerrainManager() {
delete mWorld;
}
void TerrainManager::tileLoaded(PagingLandscapeEvent* event) {
//recover PLSM2's tile object
int pageX=event->mPagex;
int pageZ=event->mPagez;
int tileX=event->mTilex;
int tileZ=event->mTilez;
String tileData = "page"+StringConverter::toString(pageX) +"-" + StringConverter::toString(pageZ) +"-tile"+StringConverter::toString(tileX)+"-"+StringConverter::toString(tileZ) + ".collision";
if (!tileBodies[tileData])
{
OgreNewt::TreeCollisionSerializer* serializer=new OgreNewt::TreeCollisionSerializer();
OgreNewt::CollisionPrimitives::TreeCollision* collision = NULL;
if(!ResourceGroupManager::getSingleton().resourceExists("PLSM2",tileData)){
vector<void*> params;
int renderLevel=2;
params.push_back(&pageX);
params.push_back(&pageZ);
params.push_back(&tileX);
params.push_back(&tileZ);
params.push_back(&renderLevel);
mSceneMgr->getOption("PageGetTileVertexData_2",¶ms);
int *numVtx=((int*)params[5]);
Vector3* vertices=((Vector3*)params[6]);
IndexData* indexData=((IndexData*)params[7]);
collision = new OgreNewt::CollisionPrimitives::TreeCollision(mWorld,*numVtx,vertices,indexData,true);
delete[] vertices;
delete numVtx;
serializer->exportTreeCollision(collision,"Collision//" + tileData);
} else {
collision=new OgreNewt::CollisionPrimitives::TreeCollision(mWorld);
DataStreamPtr ptr = ResourceGroupManager::getSingleton().openResource(tileData);
serializer->importTreeCollision(ptr,collision);
}
SceneNode *sn;
sn = mSceneMgr->getRootSceneNode()->createChildSceneNode (tileData);
sn->setPosition(event->mBbox.getCenter());
OgreNewt::Body* body=new OgreNewt::Body(mWorld,collision);
body->setMaterialGroupID(floormat);
body->attachToNode(sn);
tileBodies[tileData] = body;
delete serializer;
if (collision) delete collision;
}
}
void TerrainManager::tileUnloaded(PagingLandscapeEvent* event) {
size_t pageX=event->mPagex;
size_t pageZ=event->mPagez;
size_t tileX=event->mTilex;
size_t tileZ=event->mTilez;
String tileData = "page"+StringConverter::toString(pageX) +"-" + StringConverter::toString(pageZ) +"-tile"+StringConverter::toString(tileX)+"-"+StringConverter::toString(tileZ) + ".collision";
if (tileBodies[tileData]) {
mSceneMgr->getRootSceneNode()->removeAndDestroyChild (tileData);
OgreNewt::Body *body = tileBodies[tileData];
if (body) delete body;
tileBodies[tileData] = NULL;
}
}