Bullet Physics 在手动设置后重置刚体原点
Bullet Physics resetting rigidbody origin after manually setting
正在使用刚性body 的 Bullet Physics 引擎中的角色控制器,特别是蹲伏功能。我 运行 遇到一个问题,即 Bullet 正在将 body 的世界变换原点重置为我手动设置之前的状态。这似乎是在调用 stepSimulation()
时发生的。感觉可能有一个我没有设置的参数,虽然它可能是什么在这个时候逃避我。我上传了以下 video which shows exactly what I'm trying to explain:
- 0:00 - 09:00 未提供输入
- 10:00 按下蹲伏键(碰撞形状正确缩放,然后平移回原点,而不是停留在设置的地面上)
- 12:00 蹲下键释放
- 13:00 按下蹲伏键
- 16:00 按下向前移动键(对 body 施加力使其落回地面)
下面是我缩放碰撞 object 和转换刚性 body 原点的代码:
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()
执行,此时刚体body object被转换回上面之前的原点正在执行的代码块。
是否有任何我可能忽略的明显突出的东西?
通过以下方式完全移除、重置和读取刚体到动态世界解决了这个问题:
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);
}
正在使用刚性body 的 Bullet Physics 引擎中的角色控制器,特别是蹲伏功能。我 运行 遇到一个问题,即 Bullet 正在将 body 的世界变换原点重置为我手动设置之前的状态。这似乎是在调用 stepSimulation()
时发生的。感觉可能有一个我没有设置的参数,虽然它可能是什么在这个时候逃避我。我上传了以下 video which shows exactly what I'm trying to explain:
- 0:00 - 09:00 未提供输入
- 10:00 按下蹲伏键(碰撞形状正确缩放,然后平移回原点,而不是停留在设置的地面上)
- 12:00 蹲下键释放
- 13:00 按下蹲伏键
- 16:00 按下向前移动键(对 body 施加力使其落回地面)
下面是我缩放碰撞 object 和转换刚性 body 原点的代码:
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()
执行,此时刚体body object被转换回上面之前的原点正在执行的代码块。
是否有任何我可能忽略的明显突出的东西?
通过以下方式完全移除、重置和读取刚体到动态世界解决了这个问题:
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);
}