在Bullet Physics中从世界中移除后,刚体仍然发生碰撞

时间:2016-10-17 06:57:43

标签: bulletphysics bullet

我正在尝试实现一个简单的门,当玩家靠近时它会消失。问题是,即使在从世界上移走之后,门仍然阻挡着玩家。

在每一帧中,我都会执行以下操作:

  1. 勾选模拟

  2. 迭代所有碰撞

  3. 如果播放器与靠近门的传感器对象发生碰撞,请使用以下方法移除门:

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

  4. 奇怪的是,这对于去除场景中的射弹非常有效,一旦它们与任何其他身体接触就会被移除。

    以下是世界的设置方式:

    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));
    

    以下是门的设置方式:

    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);
    

    我已经尝试禁用此对象的停用,并使其成为没有质量的动态对象,但结果是相同的。

    那么如何从世界中移除静态对象并实际使其工作?

1 个答案:

答案 0 :(得分:0)

要删除Bullet Physics对象,首先必须删除运动状态和碰撞形状,然后必须从世界中移除刚体并将其自身删除,如下所示:

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

如果不这样做,最终会发生泄漏。您可以使用VLD(视觉泄漏检测器)跟踪它们。