Ghainlin
04-05-2013 03:37:18
Hi guys,
I'm trying to experiment with OgreBullet in an example program (using code::blocks and the ExampleApplication/ExampleFrameListener framework) by combining the Ogre terrain tutorial with the ogrebullet tutorial. Everything compiles correctly and I can execute the program without fault. I can get the original OgreBullet demo working, but when I try to insert the terrain data into the physics engine I get ... nothing. Well, actually that's not quite true. It works, but not quite the way I expected it to.
The Terrain shows correctly, and I can create and shoot boxes as per the OgreBullet demo, but at the current settings, the box does not intersect with the terrain. In fact, the debugDrawer shows the bounding box for the cube but does not show the bounding box for the terrain (and I've looked everywhere),.
I've been tinkering with the terrainScale settings and if I set the Y component to 0.0, the cube falls through the terrain and collides with an invisible object (no debugDrawer) just below it. Setting it any higher either causes the cube to sit and bounce around, or simply fall straight through.
I was wondering if anyone has had similar problems and fixed them, or if someone could suggest where to look? I've looked through the forums and google, but haven't found any answers...
Thanks in advance.
P.S. Here's my code from the ExampleListener constructor:
( Values for WORLDSIZE and TERRAINSIZE are taken from the original terrain demo. )
I'm trying to experiment with OgreBullet in an example program (using code::blocks and the ExampleApplication/ExampleFrameListener framework) by combining the Ogre terrain tutorial with the ogrebullet tutorial. Everything compiles correctly and I can execute the program without fault. I can get the original OgreBullet demo working, but when I try to insert the terrain data into the physics engine I get ... nothing. Well, actually that's not quite true. It works, but not quite the way I expected it to.
The Terrain shows correctly, and I can create and shoot boxes as per the OgreBullet demo, but at the current settings, the box does not intersect with the terrain. In fact, the debugDrawer shows the bounding box for the cube but does not show the bounding box for the terrain (and I've looked everywhere),.
I've been tinkering with the terrainScale settings and if I set the Y component to 0.0, the cube falls through the terrain and collides with an invisible object (no debugDrawer) just below it. Setting it any higher either causes the cube to sit and bounce around, or simply fall straight through.
I was wondering if anyone has had similar problems and fixed them, or if someone could suggest where to look? I've looked through the forums and google, but haven't found any answers...
Thanks in advance.
P.S. Here's my code from the ExampleListener constructor:
( Values for WORLDSIZE and TERRAINSIZE are taken from the original terrain demo. )
// setup for physics engine:
mMoveSpeed = 50; // defined in ExampleFrameListener
mNumEntitiesInstanced = 0; // how many shapes are created
Vector3 gravityVector( 0,-9.81,0 );
AxisAlignedBox bounds( Ogre::Vector3( -10000, -10000, -10000 ), Ogre::Vector3( 10000, 10000, 10000 ) ); //aligned box for Bullet
mWorld = new OgreBulletDynamics::DynamicsWorld( mSceneMgr, bounds, gravityVector );
// add Debug info display tool
debugDrawer = new OgreBulletCollisions::DebugDrawer();
debugDrawer->setDrawWireframe( true ); // we want to see the Bullet containers
mWorld->setDebugDrawer( debugDrawer );
mWorld->setShowDebugShapes( true ); // enable it if you want to see the Bullet containers
SceneNode *node = mSceneMgr->getRootSceneNode()->createChildSceneNode( "debugDrawer", Ogre::Vector3::ZERO );
node->attachObject( static_cast <SimpleRenderable *> (debugDrawer) );
...
// setup the terrain physics:
Vector3 terrainScale( WORLDSIZE / ( TERRAINSIZE - 1 ), 1, WORLDSIZE / ( TERRAINSIZE - 1 ) );
float* terrainData = mTerrainGroup->getTerrain( 0, 0 )->getHeightData();
float* heights = new float[ TERRAINSIZE * TERRAINSIZE ];
// The following works.
// At least boxes stop at this point but keep shaking...
for ( int i = 0; i < (TERRAINSIZE-1) * (TERRAINSIZE-1); i++ )
terrainData[ i ] = terrainData[ i ] / 128.0f;
// Fix the terrain physics alignment:
for ( int i = 0; i < TERRAINSIZE; i++ )
{
memcpy( heights + TERRAINSIZE * i, terrainData + TERRAINSIZE * (TERRAINSIZE-i-1), sizeof(float) * TERRAINSIZE );
}
// create a terrain shape from a HeightmapCollisionShape
OgreBulletCollisions::HeightmapCollisionShape* terrainShape = new OgreBulletCollisions::HeightmapCollisionShape(
TERRAINSIZE,
TERRAINSIZE,
terrainScale,
heights,
true
);
// Create a body for the physics world
OgreBulletDynamics::RigidBody* terrainBody = new OgreBulletDynamics::RigidBody( "Terrain", mWorld );
const float terrainBodyRestitution = 0.1f;
const float terrainBodyFriction = 0.8f;
Ogre::SceneNode* pTerrainNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
terrainBody->setStaticShape(
pTerrainNode,
terrainShape,
terrainBodyRestitution,
terrainBodyFriction,
mTerrainGroup->getTerrain( 0, 0 )->getPosition()
);
mBodies.push_back( terrainBody );
mShapes.push_back( terrainShape );