I have created a bullet vehicle with a compound as the chassis and the compound is formed of 2 bodies, a chassis and a turret.
I can obtain the turret transform and opengl matrix like this:
// get chassis and turret transforms
btTransform chassisTransform = m_vehicle->getChassisWorldTransform();
btTransform turretTransform = compound->getChildTransform(1);
// multiply transforms to get updated turret transform
turretTransform *= chassisTransform;
// get turret matrix
btScalar turretMatrix[16];
turretTransform.getOpenGLMatrix(turretMatrix);
The turretTransform is the transform of a btCollisionShape object (turretShape).
I am now trying to rotate this turret around it's Y axis.
I have tried this:
turretTransform.setRotation(btQuaternion(btVector3(0, 1, 0), angle));
Where angle is a float, but nothing happens.
I'm definitely missing something, but do not fully understand how these rotations work.
I had to change the car-turret setup and created a separate rigid body for the turret. So now I have 2 different rigid bodies, the car and the turret.
After going through the ConstraintDemo I came out with this:
// create constraint
btVector3 axisA(0.f, 1.f, 0.f);
btVector3 axisB(0.f, 0.f, 0.f);
btVector3 pivotA(0.f, 1.f, 0.f);
btVector3 pivotB(0.f, 0.f, 0.f);
hinge = new btHingeConstraint(*m_carChassis, *turretBody, pivotA, pivotB, axisA, axisB);
// add constraint to world
bt_dynamicsWorld->addConstraint(hinge, true);
I can now use hinge->enableAngularMotor(true, 0, 1);
to stop the turret rotation abdhinge->enableAngularMotor(true, angle, 1);
(float angle) to rotate it when I want to.