Cant solve angular momentum

Hi fellow programmers,

last two days i was trying to simulate angular drag. So physics space ship in space, using an PID controller, should be fully self-sufficient in rotational stuff. Intention is, i only set quaternion requiredRotation, which aims at direction ship should rotate to, and she takes care of all physics.

PID works relative good, it rotates towards required direction, but without sufficient drag, it overshoot because ship can rotate too fast (because im unable to solve angular momentum (to also prevent engines to overshoot with too much power)), or it rotating miserably slowly when a small rotation is required (because D is set high)

this is what i ended with: http://pastebin.com/6bf6JU9Z
((tpf * tpf) is there because on another place i do torque.multLocal(tpf);, its not elegant i know)

So i would like to ask, if anyone know some script that simulates angular drag using only torque and it is in bullet physics, or how can i make ship angular stop instantly…

PS: linear momentum is easy, this stops ship in one tick:


Vector3f force = new Vector3f(rigidBody.getLinearVelocity());
force.normalizeLocal().negateLocal().multLocal(rigidBody.getMass() / tpf);
rigidBody.applyCentralForce(force);

but do the same with angular velocity is bloody hell :frowning:

So,

i was able to calculate angular momentum, so i can stop ship instantly in one tick


javax.vecmath.Vector3f inertiaVector = new javax.vecmath.Vector3f();
rigidBody.getCollisionShape().calculateLocalInertia(rigidBody.getMass(), inertiaVector);
Vector3f localInertia = new Vector3f(inertiaVector.x, inertiaVector.y, inertiaVector.z);

then i can make ship stop with this force: (code not cleaned yet)


Vector3f worldAngVel = rigidBody.getAngularVelocity();

Vector3f localAngVel = rigidBody.getPhysicsRotation().inverse().mult(worldAngVel);
localAngVel.multLocal(localInertia);
        
worldAngVel = rigidBody.getPhysicsRotation().mult(localAngVel);

Vector3f torque = worldAngVel.negate();
torque.divideLocal(tpf);

rigidBody.applyTorque(torque);

i dont know why above works, but when i edit it to this


Vector3f worldAngVel = rigidBody.getAngularVelocity().clone();
Vector3f worldInertia = rigidBody.getPhysicsRotation().mult(localInertia);

worldAngVel.multLocal(worldInertia);

its messed up then… maybe because inertia of object with mass 14000 is too high ?

EDIT: yep, precision issues, it is safer to transform world angular velocity into local and then multiply it with inertia and then transform it back to world