我正在创建一个简单的游戏,涉及使用C++中的子弹库来摆动俱乐部。但是,我无法按照自己想要的方式旋转球杆(刚体)。在其边缘旋转刚体?
仅供参考,这里是俱乐部的结构:
struct Club {
btRigidBody *btPhys;
btScalar rotateAmount;
btVector3 axis;
}
这里是我旋转俱乐部:
//Create a rotation matrix from the club's world transform
btMatrix3x3 orn = club.btPhys->getWorldTransform().getBasis();
//Apply a rotation to the matrix
orn *= btMatrix3x3(btQuaternion(btVector3(0, 0, 1), btScalar(degreesToRads(club.rotationAmount))));
//Set the rotation of the club
club.btPhys->getWorldTransform().setBasis(orn);
然而,这只是旋转刚体上的的中心俱乐部。我想要刚体旋转边缘(俱乐部的手柄)。
所以我增加了一个变换来移动俱乐部向上上面的支点: (此代码直接在前面的代码后如下图)
btTransform trans;
//Get the club's trasnform
club.btPhys->getMotionState()->getWorldTransform(trans);
float x, y, z;
x = trans.getOrigin().getX();
y = trans.getOrigin().getY();
z = trans.getOrigin().getZ();
//Set the transform to be 2 units above the club's origin (the club is 4 units long)
trans.setOrigin(btVector3(btScalar(x), btScalar(y + 2.0f), btScalar(z)));
//Apply the transform to the club
club.btPhys->getMotionState()->setWorldTransform(trans);
然而,这并不只是改变支点,它将球杆移动到枢轴点上方(创建不需要的空间)。
简而言之,旋转矩阵只是围绕其中的中心旋转刚体。我只是想要将刚性体中心的枢轴点移动到刚体的边缘。
绝对完美,非常感谢! – Jaitnium