0
votes

I'm attempting to implement a simple door which just disappears when the player gets close. The problem is, the door is still blocking the player even after bering removed from the world.

On every frame, I do the following:

  1. Tick the simulation

  2. Iterate through all collisions

  3. If there is a collision between the player and a sensor object placed close to the door, remove the door using this method:

    self.dynamicsWorld->removeCollisionObject(object.collisionBody);

The strange thing is, this works perfectly for removing projectiles in my scene, which are removed once they have contact with any other body.

Here's how the world is set up:

btDefaultCollisionConfiguration* collisionConfiguration;
btCollisionDispatcher* dispatcher;
btBroadphaseInterface* overlappingPairCache;
btSequentialImpulseConstraintSolver* solver;
btDiscreteDynamicsWorld* dynamicsWorld;


collisionConfiguration = new btDefaultCollisionConfiguration();
dispatcher = new btCollisionDispatcher(collisionConfiguration);
overlappingPairCache = new btDbvtBroadphase();
solver = new btSequentialImpulseConstraintSolver;
dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration);
dynamicsWorld->setGravity(btVector3(0, 0, gravity));

And here's how the door is set up:

float mass = 0;
bool isDynamic = false;

btBoxShape* shape = new  btBoxShape(size);

btTransform transform;
transform.setIdentity();
btVector3 localInertia(0, 0, 0);
if (isDynamic) {
    shape->calculateLocalInertia(mass, localInertia);
}else{
    mass = 0;
}
btDefaultMotionState* motionState = new btDefaultMotionState(transform);
btRigidBody::btRigidBodyConstructionInfo cInfo(mass, motionState, shape, localInertia);
btRigidBody* body = new btRigidBody(cInfo);

btTransform transform;
body->getMotionState()->getWorldTransform(transform);
transform.setOrigin(position);
body->getMotionState()->setWorldTransform(transform);

self.dynamicsWorld->addRigidBody(body);

I have already tried disabling deactivation on this object, and making it a a dynamic object without mass, but the result is the same.

So how do I remove a static object from the world and actually make it work?

1

1 Answers

1
votes

To delete a Bullet Physics object, first its motion state and collision shape have to be deleted, then its rigid body has to be removed from the world and deleted itself as in:

void InceptionPhysics::deleteShape(btRigidBody* rigidBody) {
    delete rigidBody->getMotionState();
    delete rigidBody->getCollisionShape();
    m_dynamicsWorld->removeRigidBody(rigidBody);
    delete rigidBody;
}

If you don't, you will end up with memory leaks. You may use VLD (Visual Leak Detector) to track them down.