btHeightfieldTerrainShape - must do smth wrong?

polylux
Posts: 5
Joined: Sun Oct 23, 2011 4:44 pm

btHeightfieldTerrainShape - must do smth wrong?

Post by polylux »

Good evening bullets.

I have been working some while on this:
Basic setup: Irrlicht engine for the looks, bullet physics for the feel.
I try to create a terrain collision shape by iterating over its graphical counterpart and collecting height vals.
This is the function in question:

Code: Select all

btHeightfieldTerrainShape* ITerrainShape::createShape(irr::scene::ITerrainSceneNode* const terrain, u32 gridSize /*which is 512 normally*/)
{
    terrain->updateAbsolutePosition();
    const core::aabbox3df bb = terrain->getBoundingBox();
    s32 sizeX = bb.MaxEdge.X - bb.MinEdge.X;
    s32 sizeY = bb.MaxEdge.Y - bb.MinEdge.Y;
    s32 sizeZ = bb.MaxEdge.Z - bb.MinEdge.Z;
    f32 minHeight = 10000000.f, maxHeight = -1000000.f;

    heightVal = new f32[gridSize*gridSize];
    const f32 stepWidthX = (f32)sizeX / (f32)gridSize;
    const f32 stepWidthZ = (f32)sizeZ / (f32)gridSize;
    u32 runVal = 0;
    for (f32 z = bb.MinEdge.Z; z < bb.MaxEdge.Z; z+=stepWidthZ)
    {
        for (f32 x = bb.MinEdge.X; x < bb.MaxEdge.X; x+=stepWidthX)
        {
            const f32 curVal = terrain->getHeight(x, z);
            heightVal[runVal++] = curVal;
            if (curVal > maxHeight)
                maxHeight = curVal;
            if (curVal < minHeight)
                minHeight = curVal;
        }
    }

    btHeightfieldTerrainShape* s = new btHeightfieldTerrainShape(gridSize, gridSize, heightVal, 1.f, minHeight, maxHeight, 1, PHY_FLOAT, false);
    delete[] heightVal;
    return s;
}
When I run my test program (basicall just a cube falling onto the terrain), I get this error:
Overflow in AABB, object removed from simulation
If you can reproduce this, please email bugs@continuousphysics.com

Please include above information, your Platform, version of OS.

Thanks.
I can confirm that this error is always reproducible. I think that I might miss smth here that causes the error. Help is greatly appreciated.

Stats:
Bullet Physics: 2.79
System: Arch Linux

Thank you,
p.
Mako_energy02
Posts: 171
Joined: Sun Jan 17, 2010 4:47 am

Re: btHeightfieldTerrainShape - must do smth wrong?

Post by Mako_energy02 »

AABB overflows are 99.99% of the time a result of dividing some attribute of the body/shape by zero. Double check your inputs and your math to make sure that isn't the case first.
polylux
Posts: 5
Joined: Sun Oct 23, 2011 4:44 pm

Re: btHeightfieldTerrainShape - must do smth wrong?

Post by polylux »

Will do (again), thank you.
I have to append that the sim runs fine up to that point the two objects get close to each other.
Mako_energy02
Posts: 171
Joined: Sun Jan 17, 2010 4:47 am

Re: btHeightfieldTerrainShape - must do smth wrong?

Post by Mako_energy02 »

That's interesting...although I'm not really sure what that means for where the error lies. In my experience AABB overflows trigger on the first call to StepSimulation() after the error is created, which is usually during initialization and thus...the first call you make.

Do you do anything special with the field terrain during the simulation by chance? I can't see anything readily wrong with the snippet you provided, but that is just initialization. Also...what other objects are in the scene? Are you sure that error is in reference to the heightfield?
polylux
Posts: 5
Joined: Sun Oct 23, 2011 4:44 pm

Re: btHeightfieldTerrainShape - must do smth wrong?

Post by polylux »

Thanks Mako for your reply.

The scene consists of 2 objects only. One is the terrain and the second is a cube slowly falling down onto it. It takes a good couple of hundred sim steps before the cube hits the terrain - everything runs smoothly until then. When I replace the terrain with another object (e.g. equally sized box) collision handling works as expected.

I am new to bullet which makes it kind of obvious I probably forgot something to init / set / ...
Maybe there is some sort of 'checklist' of things to set or hints where to start digging if such an error occurs? Seemingly bullet doesn't know what to do with the data I provide ;)

edit:
Built the latest SVN and now create a terrain with heightfield vals all set to zero - still the same problem.
Mako_energy02
Posts: 171
Joined: Sun Jan 17, 2010 4:47 am

Re: btHeightfieldTerrainShape - must do smth wrong?

Post by Mako_energy02 »

Can you show the code you use to attach it to the body, after the shape is made up to when it's added to the world? There could be an error there, maybe.

Beyond that I'm not sure how much more help I can be. I personally haven't used the heightfield terrain shape just yet. Unless someone else speaks up the only other thing I can suggest is looking at the terrain demo provided in the bullet SDK. It shows example usage of the shape.
polylux
Posts: 5
Joined: Sun Oct 23, 2011 4:44 pm

Re: btHeightfieldTerrainShape - must do smth wrong?

Post by polylux »

Thank you and sorry for the late reply.

That's a good idea, I'll sum up the process the shape goes through:

How the shape's initialized can be seen in my first post, that function yields the btTerrainHeightField shape.

Right after that, the following is done:
1. set the local scaling accordingly
2. set mass to 0.f
3. inertia calc for mass and vec(0.f, 0.f, 0.f).
4. margin set to .5f

Creation of the rigid body:
1. create a btDefaultMotionState for the terrain
2. create a btRigidBodyConstructionInfo with mass, motion state (step 1.), and the collision shape
3. create the btRigidBody providing the cInfo from step 2.
4. add rigid body to the sim world

That's pretty much it. Could it probably be that the terrain is too big in size? This one is 512x512.

Thanks,
o.
polylux
Posts: 5
Joined: Sun Oct 23, 2011 4:44 pm

Re: btHeightfieldTerrainShape - must do smth wrong?

Post by polylux »

Today I took my sample project to work where I have the exact same software setup (OS, bullet, ...) and guess what... runs fine! I really don't know what the problem can be on my side. Maybe that's something interesting to investigate for the lib itself?

Would be happy to contribute to any bugfixing,
p.