Hi,
I am trying to have velocity of an object always rotated in the same direction as object is looking at.
My scenario is that I have a spaceship that have velocity PxVec3(0,100,-10000) so its moving fast forward and little in up direction.
I can fully control space ship by adding angularForce to make it roll, pitch or yaw and also by adding forces to control linear velocity.
Now what I need to do is to keep it linear velocity directed to where space ship is looking at (also keeping in mind that it can be rolled) so as a final result I should keep my -10000 velocity in my forward direction and also keep my 100 speed in up direction.
I written function that updates object linear velocity on each frame
void applyVelocityRotation() {
PxTransform transform = actor->getGlobalPose();
PxVec3 lv(actor->getLinearVelocity());
PxQuat q = transform.q;
PxMat33 matrix = PxMat33(lastQuat);
PxQuat iq(matrix.getInverse());
PxQuat relativeQuaternion(iq * q);
PxVec3 rv = relativeQuaternion.rotate(lv);
actor->setLinearVelocity(rv, true);
lastQuat = q; //storing last quaternion to calculate relative rotation
}
Basically I am calculating relative rotation then rotating linear velocity vector and reapplying it to actor.
Problem is that velocity is rotated in like in mirrored axis. When I also add some roll in z axis and then try to change pitch then all directions of rotated velocity vector are so messed up that I can not see which axis are mirrored or what really happened.
I think that steps to achieve my goal should be as simple as taking current linear velocity rotating it with current actor rotation.
There is something wrong with my implementation and I can not get to right solution.
I spent so many hours on this problem but I need Your help.
I am also thinking that probably there can be better approach to implement ship movement but the above is only idea I have had.
Maybe someone can also suggest some better idea, however I think I am close and probably will apply few control modes for player.
Thanks!
Maciej