Torque in Bullet
I'm working on a game which involves spaceships. We're using OGRE and Bullet.
The player's ship is supposed to pitch and yaw with the mouse -- moving the mouse left will yaw the ship left, for example. The problem is, I can't figure out how to apply torque in Bullet.
Before we started integrating Bullet, our solution was to directly modify the OGRE scene node like so (using OIS for input):
bool Game::mouseMoved(const OIS::MouseEvent &event) {
shipNode->yaw(Ogre::Degree(-event.state.X.rel));
shipNode->pitch(Ogre::Degree(event.state.Y.rel));
return true;
}
And that worked fine.
Now that we're using Bullet, my plan was to apply torque to the rigid body, like so:
bool Game::mouseMoved(const OIS::MouseEvent &event) {
//shipNode->yaw(Ogre::Degree(-event.state.X.rel));
//shipNode->pitch(Ogre::Degree(event.state.Y.rel));
shipRigidBody->applyTorqueImpulse(btVector3(-event.state.X.rel, event.state.Y.rel, 0));
return true;
}
Then, after stepping the simulation, update the scene node's orientation, like so:
void Game::update(const Ogre::FrameEvent &event) {
// ... snip ...
physicsWorld->stepSimulation(event.timeSinceLastFrame, 10);
btTransform shipTransform;
shipMotionState->getWorldTransform(shipTransform);
shipNode->setPosition(shipTransform.getOrigin().x(), /* ... snip ... */);
shipNode->setOrientation(shipTransform.getRotation().getAngle(), shipTransform.getRotation().getAxis().x(), /* ... snip ... */);
}
But with this setup, I can't orient the ship at all.
Next, I tried just applying a torque impulse of (100, 10, 200)
every frame no matter what, then printing out shipTransform.getRotation().getAngle())
. Always 0. At this point, I became extremely confused, since you'd think that always applying torque would make the body's orientation change.
So, my question is: Am I missing something stupid about btRigidBody::applyTorqueImpulse()
? Or am I barking up the wrong tree entirely?
PS: shipRigidBody
is constructed like this:
shipMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 100, 0)));
shipShape = new btBoxShape(btVector3(50, 20, 75));
shipShape->calculateLocalInertia(btScalar(1), btVector3(0, 0, 0));
shipRigidBody = new btRigidBody(btRigidBody::btRigidBodyConstructionInfo(1, shipMotionState, shipShape));
I also have code which accelerates the ship forwards or backwards based on keyboard input, and that code (which uses btRigidBody::applyCentralImpulse()
) is working fine.
上一篇: 碰撞后移除物体(子弹物理)
下一篇: 子弹转矩