我目前正在评估我正在使用 C++ 和 Bullet 物理库 ="http://www.ogre3d.org/" rel="noreferrer">Ogre3D。我已经通过从 btMotionState 派生并插入我的 SceneNodes 很好地集成了 Ogre3D 和 Bullet,但现在我在计算应该传递给 btRigidBody::applyCentralImpulse 和 btRigidBody::applyTorqueImpulse 方法以实现哪些值时遇到了很多麻烦我正在寻找的结果。
当我按下键盘上的向左或向右键时,我希望飞船在局部 Z 轴上滚动。当我按向上或向下键时,我希望它在局部 X 轴上倾斜。当我按 A 或 Z 时,我希望它沿局部 Z 轴方向加速/减速。我可以在 Ogre 中使用一些四元数数学并直接在 SceneNode 上应用平移/旋转来完美实现这一点,但我真的想使用力/扭矩方法在 Bullet 引擎中应用这些值,这样它将继续移动/俯仰/滚动即使用户停止按键,摩擦力也会作用在物体上,根据需要减慢速度。
那么,我如何计算提供给这两种脉冲方法的必要值,以确保脉冲根据身体当前的方向而不是使用世界轴起作用?
谢谢,
马克
更新:
我能够计算出向前和向后运动所需的脉冲,但我仍在努力解决如何重新定向偏航/俯仰/滚动值以便将它们与扭矩脉冲方法一起使用。这是我进行向前/向后运动的方法:
if (mKeyboard->isKeyDown(OIS::KC_A))
mBody->applyCentralImpulse(mBody->getWorldTransform().getBasis().getColumn(2) * 20 * time);
if (mKeyboard->isKeyDown(OIS::KC_Z))
mBody->applyCentralImpulse(mBody->getWorldTransform().getBasis().getColumn(2) * -20 * time);
I'm currently evaluating the Bullet Physics Library for a 3D space game I'm writing using C++ and Ogre3D. I've gotten Ogre3D and Bullet integrated nicely by deriving from btMotionState and plugging in my SceneNodes, but now I'm having a lot of trouble calculating what values I should pass to btRigidBody::applyCentralImpulse and btRigidBody::applyTorqueImpulse methods in order to achieve the results I'm looking for.
When I press the LEFT or RIGHT keys on the keyboard, I want the spaceship to roll on the local Z axis. When I press UP or DOWN, I want it to pitch on the local X axis. When I press A or Z, I want it to accelerate/decelerate in the direction of the local Z axis. I can achieve this perfectly in Ogre using some quaternion mathematics and applying the translate/rotation directly on the SceneNode, but I really want to apply these values in the Bullet engine using the force/torque methods so it will continue to move/pitch/roll even after the user stops pressing keys, and so friction will act on the object to slow it down as necessary.
So, how do I calculate the necessary values to provide to these two impulse methods in order to ensure that the impulse acts based on the body's current orientation instead of using the world's axes?
Thanks,
Marc
Update:
I was able to work out the impulses needed for forward and backward movement, but I am still struggling with how to reorient yaw/pitch/roll values in order to use them with the torque impulse method. Here's how I did the forward/backward movement:
if (mKeyboard->isKeyDown(OIS::KC_A))
mBody->applyCentralImpulse(mBody->getWorldTransform().getBasis().getColumn(2) * 20 * time);
if (mKeyboard->isKeyDown(OIS::KC_Z))
mBody->applyCentralImpulse(mBody->getWorldTransform().getBasis().getColumn(2) * -20 * time);
发布评论
评论(2)
因此,查看 btRigidBody.h,
我注意到以下代码:
现在,据我了解,您希望扭矩等于某个常数乘以与飞船相关的 x(或 z)轴的旋转矢量。
正如我们所知,广义旋转矩阵可以确定如下:
这意味着,如果您可以识别轴对齐扭矩(我不知道这一点) )你可以用你的:
根据 http://www.bulletphysicals.com /Bullet/BulletFull/classbtTransform.html
这里的 * 运算符被重写以对扭矩矢量执行世界变换。
So looking at btRigidBody.h
I notice the following code:
Now as I understand it, you want your Torque to be equal to some constant times the rotational vector around the x (or z) axis associated with your spaceship.
As we know the generalized rotation matrix can be determined as follows:
This means that if you can identify an axis aligned torque (which I don't know off the top of my head) you can transform it with your:
Which according to http://www.bulletphysics.com/Bullet/BulletFull/classbtTransform.html
the * operator here is overridden to preform the world transform on the Torque vector.
经过很长一段时间的挫折后,我终于使用上述作为 applyTorqueImpulse (或 applyTorque)的输入得到了身体局部扭矩。我并不假装理解它为何在此时起作用,但它确实如此。
来自 Bullet:
也许这就是 tzenes 提到的关于产生轴对齐扭矩的内容。但令我感到困惑的是,我找不到其他人这样做的例子。肯定有人想要在身体的局部空间施加扭矩吗?但我在网上发现的任何东西都不起作用,尽管看起来应该如此。
如果你只应用变换*扭矩,它一开始看起来会起作用,直到你开始远离你的世界的原点。因此,如果距离原点越远,旋转就越困难,或者根据身体所在的位置,物体开始反向旋转,这可能是您的问题。
编辑:
哦,这就是我的翻译方法:
After a long period of frustration, I finally got body-local torque to work using the above as the input to applyTorqueImpulse (or applyTorque). I don't pretend to understand why it works at this point, but it does.
From Bullet:
Maybe this is what tzenes was mentioning about producing the axis aligned torque. But I am puzzled that I can find no example of anyone else doing it this way. Surely someone else has wanted torque applied in the body's local space? But nothing I found online worked at all, despite it looking like it should.
If you only apply transform*torque, it'll look like it works at first, until you start moving away from the origin of your world. So if it feels harder to rotate the further you are away from the origin, or if things start rotating in reverse depending on where the body is, this is probably your problem.
EDIT:
Oh, and here's how I have my translations: