Segmentation Fault at World Simulation Step

micdoodle8
Posts: 3
Joined: Sat Mar 15, 2014 11:21 pm

Segmentation Fault at World Simulation Step

Post by micdoodle8 »

Hey there,

I've been having some issues when getting bullet implemented into my engine. Seems as through when two rigid bodies are colliding, there is a segmentation fault in the btDiscreteDynamicsWorld stepSimulation function. The seg fault is happening about 1 in 4 times, where it will sometimes work perfectly any the object stops when hitting the level.

The first is a static mesh, so I'm using a btTriangleIndexVertexArray and btBvhTriangleMeshShape. The second is just a test, so I'm using a basic btBoxShape. I've never worked directly with a physics library before, so please bear with me if there's something completely wrong in there :P

Here's my code, seg fault happens on the first line inside the main loop:

Code: Select all


int main(int argc, char** argv)
{
    CollidableMeshStatic* staticMesh = new CollidableMeshStatic("level0.obj");
    staticMesh->initMesh();
	meshes.push_back(staticMesh);

	btBroadphaseInterface* broadphase = new btDbvtBroadphase();
	btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
	btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
	btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver();
    btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);

    dynamicsWorld->setGravity(btVector3(0, -9.81, 0));

    btCollisionShape* fallShape = new btBoxShape(btVector3(1, 1, 1));

    btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 0, 0)));
    btRigidBody::btRigidBodyConstructionInfo groundRightBodyCI(0, groundMotionState, staticMesh->getCollisionShape(), btVector3(0, 0, 0));
    btRigidBody* groundRigidBody = new btRigidBody(groundRightBodyCI);

    dynamicsWorld->addRigidBody(groundRigidBody);

    btDefaultMotionState* fallMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 4.1, 0)));
    btScalar mass = 1;
    btVector3 fallIneratia(0, 0, 0);
    fallShape->calculateLocalInertia(mass, fallIneratia);

    btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass, fallMotionState, fallShape, fallIneratia);
    btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI);
    dynamicsWorld->addRigidBody(fallRigidBody);

	while (display.isRunning())
	{
        dynamicsWorld->stepSimulation(1 / 60.0F, 10); // Seg Fault here!

        btTransform trans;
        fallRigidBody->getMotionState()->getWorldTransform(trans);

        std::cout << "Box height: " << trans.getOrigin().getY() << std::endl;

       // <snip>
	}

	Util_S.destroyPointerVec(meshes);

	delete dynamicsWorld;
	delete solver;
	delete dispatcher;
	delete collisionConfiguration;
	delete broadphase;

	return 0;
}

And the code that creates the level shape:

Code: Select all


void CollidableMeshStatic::initCollidableMesh(std::vector<int> indices, std::vector<glm::vec3> positions, int numIndices, int numVertices)
{
    m_pMeshCollisionVertexArray = new btTriangleIndexVertexArray(numIndices / 3, &indices[0], sizeof(int) * 3, numVertices, &positions[0].x, sizeof(float) * 3);
    m_pMeshCollisionShape = new btBvhTriangleMeshShape(m_pMeshCollisionVertexArray, true);
}

I would really appreciate if anyone know's how to approach this issue, I've been trying to solve this issue through different methods for several hours now. Thanks :D
micdoodle8
Posts: 3
Joined: Sat Mar 15, 2014 11:21 pm

Re: Segmentation Fault at World Simulation Step

Post by micdoodle8 »

I apologize, the issue had nothing to do with bullet... I had my classes setup incorrectly and the position/index vectors fell out of scope.