【问题标题】:Bullet Physics resetting rigidbody origin after manually settingBullet Physics 在手动设置后重置刚体原点
【发布时间】:2020-08-26 03:54:51
【问题描述】:

在 Bullet Physics 引擎中使用刚体的角色控制器,特别是蹲伏功能。我遇到了一个问题,Bullet 将身体的世界变换的原点重置为手动设置之前的状态。这似乎是在调用 stepSimulation() 时发生的。感觉可能有一个参数我没有设置,尽管此时我可能会避开它。我上传了以下video which shows exactly what I'm trying to explain

  • 0:00 - 09:00 未提供输入
  • 10:00 蹲下键被按下(碰撞形状正确缩放,然后被转换回原点,而不是停留在设置的地面上)
  • 12:00 蹲下键释放
  • 13:00 按下蹲伏键
  • 16:00 按下向前移动键(对身体施加力使其落回地面)

下面是我缩放碰撞对象并转换刚体原点的代码:

if (this->input.keyLeftControl && !this->crouching)
{
    this->crouching = true;

    //Set scale
    btVector3 scale = this->clientBody->getCollisionShape()->getLocalScaling();
    scale.setY(crouchScale);
    this->clientBody->getCollisionShape()->setLocalScaling(scale);
    this->ghostObject->getCollisionShape()->setLocalScaling(scale);
    this->collisionWorld->dynamicsWorld->updateSingleAabb(this->clientBody);


    auto clientOrigin = this->clientBody->getWorldTransform().getOrigin();
    btVector3 newOrigin = btVector3(clientOrigin.getX(), clientOrigin.getY() - 0.525, clientOrigin.getZ());
    clientBody->getWorldTransform().setOrigin(newOrigin);
    this->ghostObject->getWorldTransform().setOrigin(newOrigin);    
}

此代码执行后,原点是正确的,直到stepSimulation() 执行,此时刚体对象被转换回执行上述代码块之前的原点。

有什么明显的东西让我可能忽略的任何人都脱颖而出?

【问题讨论】:

    标签: c++ collision-detection physics-engine bulletphysics bullet


    【解决方案1】:

    通过以下方式完全移除、重置和读取刚体到动力学世界,解决了这个问题:

    if (this->input.keyLeftControl && !this->crouching)
    {
        this->crouching = true;
        this->crouchNeedFall = true;
    
        //Set scale
        btVector3 scale = this->clientBody->getCollisionShape()->getLocalScaling();
        scale.setY(crouchScale);
        this->clientBody->getCollisionShape()->setLocalScaling(scale);
        this->ghostObject->getCollisionShape()->setLocalScaling(scale);
        this->collisionWorld->dynamicsWorld->updateSingleAabb(this->clientBody);
    
        auto clientOrigin = this->clientBody->getWorldTransform().getOrigin();
        btVector3 newOrigin = btVector3(clientOrigin.getX(), clientOrigin.getY() - 0.525, clientOrigin.getZ());
    
        this->collisionWorld->dynamicsWorld->removeRigidBody(this->clientBody);
    
        this->clientBody->clearForces();
        this->clientBody->setLinearVelocity(btVector3(0, 0, 0));
        this->clientBody->setAngularVelocity(btVector3(0, 0, 0));
        this->clientBody->getWorldTransform().setOrigin(newOrigin);
    
        this->ghostObject->getWorldTransform().setOrigin(newOrigin);
    
        this->collisionWorld->dynamicsWorld->addRigidBody(this->clientBody);
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多