I hope it helps...
- Code: Select all
void ogre_testApp::DetermineTerrainX_Dimension()
{
// A hack for getting the X-dimension of your Ogre terrain at load time.
// the Ogre TerrainSceneManager creates your terrain and sets one corner at world center.
// this makes it inconvevnient when you'd like to know where terrain center is.
// the Ogre terrain scene manager API provides no direct way to determine the world
// coodinates of your world terrain geometry (at least not that I have found...)
// this method determines the X-dimension of your loaded terrain -- with which you can determine
// terrain center -- and with which you can accurately calculate offsets.
// Keep in mind, moving the terrain node causees misalignments with collision
// detection. So, use offsets, and leave sleeping terrains lie.
// Steps:
// 1. initialize your Ogre terrain app as recommended
// 2. after you use your scene manager to setWorldGeometry( your_terrain_config_file ), run this code.
// 3. use the resulting value to offset the positions of other world resources loaded later,
// i.e., SceneNode::setPosition(...) .
// this method aassumes we are starting in the upper-left (or is it lower-left?) corner of our terrain.
// basically, we start by hovering above one of the corners of the terrain.
// we make one trial test to see if we hit dirt. If so, then we walk it down the line,
// poking down all the way until we run out of terrain -- keeping track of our progress, mind you.
int iX_Width = 1; // our starting point. we'll assume at least a width of 1
const int iY_Line = 1000; // make sure your query starts above your highest terrain
const int iZ_Line = 1; // run our queries just inside the edge
RaySceneQuery* raySceneQuery = NULL; // our ray query machine
Ray updateRay; // our "sounding" tool - for poking down towards the ground
bool bFoundTerrain = false; // our finishing flag
// create a custom query machine and initialize its ray tool
raySceneQuery = m_pSceneMgr->createRayQuery(Ray(Vector3(iX_Width, iY_Line, iZ_Line), Vector3::NEGATIVE_UNIT_Y));
// do an initial "sounding" to make sure you're in the right place
// poke down now and see if you hit anything
RaySceneQueryResult& qryResult = raySceneQuery->execute();
// examine your results. If positive, then we are going down the line, baby.
RaySceneQueryResult::iterator i = qryResult.begin();
if (i != qryResult.end() && i->worldFragment)
{
bFoundTerrain = true; // set the flag
iX_Width++; // increment our known width
raySceneQuery->clearResults(); // clear it out
}
// given that we found terrain on our first try, let's move over and try again
// keep moving until we don't hit dirt no more :-)
while (bFoundTerrain)
{
updateRay.setOrigin(Vector3(iX_Width, iY_Line, iZ_Line));
updateRay.setDirection(Vector3::NEGATIVE_UNIT_Y);
raySceneQuery->setRay(updateRay);
RaySceneQueryResult& qryResult = raySceneQuery->execute();
RaySceneQueryResult::iterator i = qryResult.begin();
if (i != qryResult.end() && i->worldFragment)
{
bFoundTerrain = true;
iX_Width++;
m_rOffsetX = m_rOffsetZ = iX_Width;
raySceneQuery->clearResults();
}
else
{
bFoundTerrain = false;
}
}
}
Loading the Ogre at terrain center -- after using offsets from this method.





