0

I am trying to load a HeightmapTerrainShape in OgreBullet by (mostly) using the demo code, but my terrain mesh is offset from the HeightmapTerrainShape. I have no clue why this is happening. This is my code:

void TerrainLoader::setTerrainPhysics(Ogre::Image *imgPtr)

{
    unsigned page_size = terrainGroup->getTerrainSize();
    Ogre::Vector3 terrainScale(4096 / (page_size-1), 600, 4096 / (page_size-1));
    float *heights = new float[page_size*page_size];
    for(unsigned y = 0; y < page_size; ++y)
    {
        for(unsigned x = 0; x < page_size; ++x)
        {
            Ogre::ColourValue color = imgPtr->getColourAt(x, y, 0);
            heights[x + y * page_size] = color.r;
        }
    }

OgreBulletCollisions::HeightmapCollisionShape *terrainShape = new OgreBulletCollisions::HeightmapCollisionShape(
    page_size,
    page_size,
    terrainScale,
    heights,
    true
);
OgreBulletDynamics::RigidBody *terrainBody = new OgreBulletDynamics::RigidBody(
    "Terrain",
    OgreInit::level->physicsManager->getWorld()
);
imgPtr = NULL;

Ogre::Vector3 terrainShiftPos(terrainScale.x/(page_size-1), 0, terrainScale.z/(page_size-1));
terrainShiftPos.y = terrainScale.y / 2 * terrainScale.y;

Ogre::SceneNode *pTerrainNode = OgreInit::sceneManager->getRootSceneNode()->createChildSceneNode();
terrainBody->setStaticShape(pTerrainNode, terrainShape, 0.0f, 0.8f, terrainShiftPos);
//terrainBody->setPosition(terrainBody->getWorldPosition()-Ogre::Vector3(0.005, 0, 0.005));
OgreInit::level->physicsManager->addBody(terrainBody);
OgreInit::level->physicsManager->addShape(terrainShape);
}

This is what it looks like with the debug drawer turned on: enter image description here enter image description here enter image description here

My world is 4096*600*4096 in size, and each chunk is 64*600*64

Nik
  • 1,033
  • 2
  • 11
  • 28

1 Answers1

1
heights[x + y * page_size] = color.r;

This Line gives you negative values. If you combine negative terrain height values with ogre bullet terrain, you get a wrong bounding box conversation.

You need to use the intervall 0-1 for height values.

Had the same problem with perlin noise filter that gives you values from -1 to 1.

MUG4N
  • 19,377
  • 11
  • 56
  • 83
Paul Klein
  • 11
  • 1
  • Would you mind posting a little snippet of what you mean exactly? I am still confused :( – Nik Dec 14 '12 at 21:32