[SOLVED] Segfault in OgreBulletDynamicsWorld

Blaxar

02-09-2014 22:40:05

Hello everyone.
I was following the OgreBullet Tutorial2, I get the thing to compile but I also get a segfault at runtime, it happens in the plane collision detection part:


OgreBulletCollisions::CollisionShape *Shape;
Shape = new OgreBulletCollisions::StaticPlaneCollisionShape(Ogre::Vector3(0,1,0), 0); // (normal vector, distance)
OgreBulletDynamics::RigidBody *defaultPlaneBody = new OgreBulletDynamics::RigidBody(
"BasePlane",
mWorld);
defaultPlaneBody->setStaticShape(Shape, 0.1, 0.8); // (shape, restitution, friction)
// push the created objects to the deques
mShapes.push_back(Shape);
mBodies.push_back(defaultPlaneBody);


Specifically:

defaultPlaneBody->setStaticShape(Shape, 0.1, 0.8); // (shape, restitution, friction)

If I comment this one line, I get to run the application minus the collision on the floor: the box falls but go through it, which makes sense since it has no physical shape attached to it yet without this line.

I've tracked down the origin of the segfault, but sadly it comes to this line in ogrebullet/Dynamics/src/OgreBulletDynamicsWorld.cpp


void DynamicsWorld::addRigidBody(RigidBody *rb, short collisionGroup, short collisionMask)
{
mObjects.push_back(static_cast <Object *>(rb));

if (collisionGroup == 0 && collisionMask == 0)
{
// use default collision group/mask values (dynamic/kinematic/static)
static_cast<btDiscreteDynamicsWorld *>(mWorld)->addRigidBody(rb->getBulletRigidBody());
}
else
{
static_cast<btDiscreteDynamicsWorld *>(mWorld)->addRigidBody(rb->getBulletRigidBody(), collisionGroup, collisionMask);
}
}


Specifically:

mObjects.push_back(static_cast <Object *>(rb));


Note that mObjects is a std::deque<Objects *>, Objects is a base class of RigidBody and that rb is correctly instantiated (I can access its methods even down in this function).

So I tried various stuff, like changing the static_cast to a normal cast, or forcing g++ to use C++11 (instead of the default standard, which is C++98 on my system apparently), both did nothing.

Has anyone encountered this ?

Thank you in advance.

[EDIT]

Ok, I found the problem, OgreBullet in itself is fine (if not that would have been annoying).

The problem came from my setup() function, for some reason I was calling createScene() before createFrameListener(), but in the case of this tutorial this is problematic, since this in createFrameListener():


mMoveSpeed = 50; // defined in ExampleFrameListener
mNumEntitiesInstanced = 0; // how many shapes are created
// Start Bullet
mWorld = new OgreBulletDynamics::DynamicsWorld(mSceneMgr, mBounds, mGravityVector);
// 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
Ogre::SceneNode *node = mSceneMgr->getRootSceneNode()->createChildSceneNode("debugDrawer", Ogre::Vector3::ZERO);
node->attachObject(static_cast <Ogre::SimpleRenderable *> (debugDrawer));


has to be done before this in createScene():


OgreBulletCollisions::CollisionShape *Shape;
Shape = new OgreBulletCollisions::StaticPlaneCollisionShape(Ogre::Vector3(0,1,0), 0); // (normal vector, distance)
OgreBulletDynamics::RigidBody *mDefaultPlaneBody = new OgreBulletDynamics::RigidBody(
"BasePlane", mWorld);
std::cout << mDefaultPlaneBody->getName() << std::endl;
mDefaultPlaneBody->setStaticShape(Shape, 0.1, 0.8); // (shape, restitution, friction)
// push the created objects to the deques
mShapes.push_back(Shape);
mBodies.push_back(mDefaultPlaneBody);


[/EDIT]