btDefaultMotionState
实例,确切地说,我想为下落物体使用一个实例,并将其放置在地面上方的某个位置,然后为地面创建另一个实例,应该简单地与我的图形地面对齐,永远保持不动。
关于2),我所经历的是btDefaultMotionState
实例始终影响地面的实例,而没有参考。
现在来看一下代码:
创建下落物体:btCollisionShape *fallingBoxShape = new btBoxShape(btVector3(1,1,1));
btScalar fallingBoxMass = 1;
btVector3 fallingBoxInertia(0,0,0);
fallingBoxShape->calculateLocalInertia(fallingBoxMass, fallingBoxInertia);
// TODO this state somehow defines where exactly _ALL_ of the physicsWorld is...
btDefaultMotionState *fallMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(2,2,2)));
//btDefaultMotionState *fallMotionState = new btDefaultMotionState();
btRigidBody::btRigidBodyConstructionInfo fallingBoxBodyCI(fallingBoxMass, fallMotionState, fallingBoxShape, fallingBoxInertia);
/*btTransform initialTransform;
initialTransform.setOrigin(btVector3(0,5,0));*/
this->fallingBoxBody = new btRigidBody(fallingBoxBodyCI);
/*fallMotionState->setWorldTransform(initialTransform);
this->fallingBoxBody->setWorldTransform(initialTransform);*/
this->physicsWorld->addBody(*fallingBoxBody);
对我来说,有趣的部分是必要的偏移量btVector3(2,2,2)
以使其与我的绘制世界对齐,以及以下内容:
btTransform initialTransform;
initialTransform.setOrigin(btVector3(0,5,0));
this->fallingStarBody = new btRigidBody(fallingStarBodyCI);
fallMotionState->setWorldTransform(initialTransform);
如果我重新启用代码的这部分,所有物体都会再次显示出偏移量,但不只是上移5个单位,如果由于某种原因worldTransform会影响每个实体,那么我可以在某种程度上理解它,但现在物体偏移2,2,2,这让我完全无法理解。
我猜这一行是无用的:
fallMotionState->setWorldTransform(initialTransform);
因为有或没有都不会改变任何东西。
现在来看创建地面的代码:
btCompoundShape *shape = new btCompoundShape();
... just some logic, nothing to do with bullet
btTransform transform;
transform.setIdentity();
transform.setOrigin(btVector3(x + (this->x * Ground::width),
y + (this->y * Ground::height),
z + (this->z * Ground::depth)));
btBoxShape *boxShape = new btBoxShape(btVector3(1,0,1)); // flat surface, no box
shape->addChildShape(transform, boxShape);
(这部分只是为每个表面瓷砖创建了一个复合图形 :))
btRigidBody::btRigidBodyConstructionInfo info(0, nullptr, shape);
return new btRigidBody(info);
我故意将运动状态设置为 nullptr
,但这并没有改变任何东西。
现在我真的很好奇... 我以为 btDefaultMotionState
的实现是一个单例,但看起来并不是这样,那么... 为什么设置一个物体的运动状态会影响整个世界呢?
fallingBoxBody
刚体后删除了fallMotionState
?这将导致刚体中的悬空指针,可能会与下一个相同大小的分配别名,从而导致你所看到的问题。或者,你有一些其他之前的悬空指针被fallMotionState
覆盖。 - user4442671