子弹物理盒不旋转

时间:2014-06-29 19:08:29

标签: c++ opengl physics bullet

我正在使用以下代码使用项目符号物理引擎设置一个简单的世界。当我运行它时,框不会表现得真实,因为它们不会旋转。这就像他们的轮换被锁定一样。有没有人以前见过这样的东西或知道可能导致它的原因?谢谢。

World::World()
{
mBroadphase = new btDbvtBroadphase();
mCollisionConfiguration = new btDefaultCollisionConfiguration();
mDispatcher = new btCollisionDispatcher(mCollisionConfiguration);
mSolver = new btSequentialImpulseConstraintSolver();
mWorld = new btDiscreteDynamicsWorld(mDispatcher, mBroadphase, mSolver, mCollisionConfiguration);
mWorld->setGravity(btVector3(0, -10, 0));

mGroundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 1);
mBoxShape = new btBoxShape(btVector3(4, 1.5, 1));

mGroundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, -10, 0)));
btRigidBody::btRigidBodyConstructionInfo groundRBCI(0, mGroundMotionState, mGroundShape, btVector3(0, 0, 0));

mGroundRigidBody = new btRigidBody(groundRBCI);
mGroundRigidBody->setUserIndex(1);
mWorld->addRigidBody(mGroundRigidBody);

btScalar mass = 1;
btVector3 boxInertia(0, 0, 0);
mBoxShape->calculateLocalInertia(mass, boxInertia);

mBoxMotionState1 = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 35, -20)));
btRigidBody::btRigidBodyConstructionInfo BoxRBCI1(mass, mBoxMotionState1, mBoxShape, btVector3(0, 0, 0));
mBoxRigidBody1 = new btRigidBody(BoxRBCI1);
mBoxRigidBody1->setUserIndex(2);

mBoxMotionState2 = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(2, 20, -20)));
btRigidBody::btRigidBodyConstructionInfo BoxRBCI2(mass, mBoxMotionState2, mBoxShape, btVector3(0, 0, 0));
mBoxRigidBody2 = new btRigidBody(BoxRBCI2);
mBoxRigidBody2->setUserIndex(3);

mBoxMotionState3 = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(-5, 12, -20)));
btRigidBody::btRigidBodyConstructionInfo BoxRBCI3(mass, mBoxMotionState3, mBoxShape, btVector3(0, 0, 0));
mBoxRigidBody3 = new btRigidBody(BoxRBCI3);
mBoxRigidBody3->setUserIndex(4);

mWorld->addRigidBody(mBoxRigidBody1);
mWorld->addRigidBody(mBoxRigidBody2);
mWorld->addRigidBody(mBoxRigidBody3);



}

1 个答案:

答案 0 :(得分:6)

啊哈!我遇到了这个问题而且我找不到答案,所以我很乐意提供帮助!

该行

mBoxShape->calculateLocalInertia(mass, boxInertia);

修改boxInertia,然后您需要将其传递给btRigidBody::btRigidBodyConstructionInfo的构造函数,如下所示:

btRigidBody::btRigidBodyConstructionInfo BoxRBCI1(mass, mBoxMotionState1, mBoxShape, boxInertia);

然后你的盒子应该旋转:)