🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

[solved] How to apply local torque to an axis?

Started by
3 comments, last by Axiverse 5 years, 12 months ago

I'm writing a physics engine based on bullet. For a body, the angular velocity is stored as a vec3, which is the normalized axis of rotation scaled by the angle. To add global torque, you simply add to this vec3. However, I want to add torque based on the body's local frame of reference. I was wondering how to approach this.

The approach that I think is viable is.

torquequat = Convert vec3 torque into quaternion.

localtorquequat = Convert localTorque into quaternion.

globaltorquequat = Multiply localTorque by the inverse of the angular position.

combinedtorquequat = torquequat * angularposition.inverse() * localtorquequat.

decompose into axis angle and scale axis by angle.

 

This feels incredibly involved for adding a local torque. Are there better alternatives?

Advertisement
1 hour ago, Axiverse said:

For a body, the angular velocity is stored as a vec3, which is the normalized axis of rotation scaled by the angle. To add global torque, you simply add to this vec3.

This is wrong, you can not add torque to angular velocity. You can only add torque to torque.

So i am not sure what you ask for exactly.

Do you want to calculate angular velocity caused from a global torque? (Here we need to be careful to apply inertia in correct space)

 

Yes. I'm really trying to convert local space torque into global space torque. The code is as follows. AngularPosition (quat).Transform rotates the localTorque by the orientation of the body, which seems to be wrong.


        /// <summary>
        /// Applies a torque impulse on the body. Will only cause any angular changes.
        /// </summary>
        /// <param name="torque"></param>
        public void ApplyTorqueImpulse(Vector3 torque)
        {
            angularVelocity = angularVelocity +
                Matrix3.Transform(inverseInertiaTensorWorld, torque) * angularFactor;
        }

        public void ApplyLocalTorqueImpulse(Vector3 localTorque)
        {
            // This is wrong?
            ApplyTorqueImpulse(AngularPosition.Transform(localTorque));
        }

This post seems to suggest that I can just rotate the localTorque vector by the orientation and it should work, however I'm not seeing this to be the case.

https://answers.unrealengine.com/questions/495309/applying-torque-in-local-space-on-a-physics-driven.html

 

Full Source:

Body https://github.com/AxiverseCode/Axiverse/blob/master/Source/Libraries/Axiverse.Physics/Body.cs

Math: https://github.com/AxiverseCode/Axiverse/tree/master/Source/Libraries/Axiverse.Mathematics/Core

Hmm, it seems like the angular velocity is stored in local space in my code. The culprit line was in the integratetransform function. I'm calculating angular position like:

angularPosition = (angularPosition * deltaOrientation).Normal();

Whereas bullet has:

btQuaternion predictedOrn = dorn * orn0;

This topic is closed to new replies.

Advertisement