Rotating object on 1 axis only

219 Views Asked by At

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).

1

There are 1 best solutions below

0
On

I would recommend to go a different approach. If you have to reset the rotation every frame this may impact your performance and is quite 'dirty'. Maybe try to override the physics for your vehicle type. At the moment I got no code here but the docs (coding reference) and the engine/physics code should guide you enough. Maybe take a look on the GameSDK Code to see how vehicle physics are handled there.