Problems with TerrainGeometry

qobold

08-02-2008 18:22:15

Hello,

I'm trying to add a terrain to the ogreode environment. Falling down works perfect, but everything is falling through my landscape. I have tried to copy the relevant parts from Landscape.cpp, but I think I missed something ;)
My code:

Constructor:
PhysicsHandler::PhysicsHandler(Ogre::SceneManager* scene_mgr, Ogre::Root* ogre_root_) {
scene_mgr_ = scene_mgr;
ode_world_ = new OgreOde::World(scene_mgr_);
ode_world_->setGravity(Ogre::Vector3(0,-9.80665,0));
ode_world_->setCFM(10e-5);
ode_world_->setERP(0.8);
ode_world_->setAutoSleep(true);
ode_world_->setAutoSleepAverageSamplesCount(10);
ode_world_->setContactCorrectionVelocity(1.0);
ode_space_ = ode_world_->getDefaultSpace();
/* initialize stepper */
ode_stepper_ = new OgreOde::StepHandler(ode_world_, OgreOde::StepHandler::QuickStep, _time_step, max_frame_time,
time_scale);
ode_stepper_->setAutomatic(OgreOde::StepHandler::AutoMode_PostFrame, ogre_root_);
}


adds an entity to the ode world:
void PhysicsHandler::registerEntity(EntityPtr entity, Ogre::Entity* ogre_ent, Ogre::SceneNode* ogre_node) {
OgreOde::Body* ode_body = new OgreOde::Body(ode_world_);
ogre_node->attachObject(ode_body);

Ogre::Vector3 size(10.0,10.0,10.0);
OgreOde::BoxMass mMass(0.5, size);
mMass.setDensity(5.0, size);

OgreOde::Geometry* geom = (OgreOde::Geometry*)new OgreOde::BoxGeometry(size, ode_world_, ode_space_); // temporary
ogre_node->setScale(size.x * 0.1,size.y * 0.1,size.z * 0.1);
ode_body->setMass(mMass);
geom->setBody(ode_body);
ogre_ent->setUserObject(geom);
}

registers the terrain:
void PhysicsHandler::registerTerrain() {
int nodes_per_side = scene_mgr_->getOption("PageSize", &nodes_per_side);
Ogre::Vector3 scale = Ogre::Vector3::ZERO;
scene_mgr_->getOption("Scale", &scale);

//scene_mgr_->getOption("Width", &w);
//scene_mgr_->getOption("Height", &h);

if (nodes_per_side == 0)
{
Ogre::ConfigFile config;
Ogre::String val;

config.load("sample/data/levels/demo/terrain.cfg", "=", true); // todo!!!

val = config.getSetting( "PageSize" );
assert( !val.empty() );
nodes_per_side = atoi( val.c_str() );

val = config.getSetting( "PageWorldX" );
assert( !val.empty() );
scale.x = atof( val.c_str() ) / nodes_per_side;

val = config.getSetting( "MaxHeight" );
assert( !val.empty() );
scale.y = atof( val.c_str() );

val = config.getSetting( "PageWorldZ" );
assert( !val.empty() );
scale.z = atof( val.c_str() ) / nodes_per_side;
}
Ogre::Real worldWidth = scale.x * (nodes_per_side - 1);
Ogre::Real worldHeight = scale.z * (nodes_per_side - 1);

int nodes_per_side_all_pagesW = 0;
int nodes_per_side_all_pagesH = 0;

nodes_per_side_all_pagesW = nodes_per_side;
nodes_per_side_all_pagesH = nodes_per_side;

bool center = false;

_terrain = new OgreOde::TerrainGeometry(ode_world_,
ode_world_->getDefaultSpace(),
scale,
nodes_per_side_all_pagesW,
nodes_per_side_all_pagesH,
worldWidth,
worldHeight,
center);
_terrain->setHeightListener(this);
ode_world_->setCollisionListener(this);
}


Collision handler:
bool PhysicsHandler::collision(OgreOde::Contact* contact) {
Geometry * const g1 = contact->getFirstGeometry ();
Geometry * const g2 = contact->getSecondGeometry ();
const bool isg1Sphere = g1 && g1->getClass () != Geometry::Class_Sphere;
const bool isg2Sphere = g2 && g2->getClass () != Geometry::Class_Sphere;

if((isg1Sphere || isg2Sphere))
{
//handled by Vehicle.
}
else
{
contact->setBouncyness(0.0);
contact->setCoulombFriction(18.0);
}

return true;
}

heightat handler:
Ogre::Real PhysicsHandler::heightAt(const Ogre::Vector3& position)
{
return _terrain->getHeightAt(position);
}


Could you please give me a tip? I don't have any idea.

Please excuse my bad english and code :)

Greetings Fred

rewb0rn

08-02-2008 18:37:50

call _terrain->setDebug(true) and check wether the ogre entity and the ogreode geometry fit.

dermont

08-02-2008 19:17:01

Also try reducing the density to 0.01 in mMass.setDensity(5.0, size). Your problem with objects falling through the landscape may be due to the bodies large mass.

a) OgreOde::BoxMass mMass(0.5, size) - sets total box mass to 0.5
b) mMass.setDensity(5.0, size) - sets total box mass to 5.0*size, in your case 5*10*10*10

qobold

08-02-2008 19:20:08

Many thanks, dermonts tip solved it!
Thank you both for your (very) fast help :)