【发布时间】:2016-12-21 20:21:35
【问题描述】:
总结问题:
到目前为止,我的世界里有两个身体,一个是地面,另一个是一个名为“fallingStar”的坠落盒子。
1) 我不明白为什么我的子弹世界与我绘制的世界不对齐,除非我将 btVector3(2,2,2) 的偏移量设置为 (btDefault)MotionState。
代码中的任何地方都没有花哨的魔法来解释偏移量。或者至少我找不到任何原因,不是在着色器中,不是在任何地方。
2) 我希望能够使用btDefaultMotionState 的多个实例,确切地说,我想为坠落的实体使用一个实例并将其放置在地面上方的某个位置,然后为地面创建另一个实例只需与我的图形地面对齐,永远不动。
我对 2) 的体验是,无论出于何种原因,坠落实体的 btDefaultMotionState 实例总是也影响地面实例,没有任何参考。
现在是代码:
fallingBox的创建:
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);
如果我重新启用这部分代码ALL,主体再次显示偏移量,但不仅增加 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);
这里我特意将motionstate设置为nullptr,但这并没有改变任何东西。
现在我真的很好奇...我以为btDefaultMotionState的实现可能是一个单例,但看起来不是这样,所以...为什么要设置一个身体的运动状态影响整个世界?
【问题讨论】:
-
会不会是你在创建
fallingBoxBody刚体后删除了fallMotionState?这将导致刚体中的一个悬空指针可能会别名下一个相同大小的分配,可能导致您所看到的?要么,要么你有其他一些以前的悬空指针被fallMotionState -
@Frank 好吧,这发生在构造函数中,稍后超出范围,但我没有在这里手动释放任何东西。 (是的,可能是泄漏的原因)
-
切换一切使用智能指针,仍然没有运气
标签: c++ game-engine game-physics bulletphysics