I am trying to make a simple bike which has 2 tires and is a rigid body of-course. Since it only has 2 tires, it keeps falling down.
So, in-order to balance the vehicle, I am trying to use quaternions to rotate it only on Y-axis in-order to keep it standing while giving it movabiloty on the other 2 axis (X & Z).
The approach I have taken is to check if the rotation of the vehicle is different from the one it had when it spawned (it spawned standing), and based on that force the vehicle to rotate to how it was standing before it falls down due to imbalance of being on only 2 tires and not 4). This way, I am able to keep the vehicle standing at all times, but have movement across other axis restricted, which in-turn doesn't allow my vehicle to sideways, but only straight and back.
Before you read the code, I would like to mention that I have simplified & shortened this code for easier understanding and I'm only looking for help in understand how to go about it and not an answer code solution.
Code:
Quat qCurrentRotation = GetRotation(); // updated every frame
Quat qTargetRotation = qInitialRotation; // stored when vehicle spawned
qFinalRotation = Quat::CreateIdentity();
if (qCurrentRotation != qTargetRotation)
{
float fSmoothFactor = 0.1f;
qFinalRotation = Quat::CreateNlerp(qCurrentRotation, qTargetRotation, qTargetRotation);
mVehicle->SetRotation(qFinalRotation);
}
The code above makes the bike have the same rotation as how it was spawned. Though it's buggy, and makes the vehicle flicker. Leaving that aside, can someone please advise me how use quaternions, interpolations & angles to only stop the rotation of my vehicle on one axis (Y) so that it doesn't fall down and allow it to be movable on the other two (X & Z).