【发布时间】:2015-08-28 22:17:29
【问题描述】:
我正在创建一个简单的游戏,该游戏涉及使用 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);
但是,这不会改变轴心点,它只是将球杆移动到轴心点上方(创建一个不需要的空间)。
简而言之,旋转矩阵只是围绕它的中心旋转刚体。我只想将轴心点从刚体的中心移动到刚体的边缘。
【问题讨论】:
标签: c++ opengl matrix rotation bulletphysics