- Home /
Question by
ProfilerOne · Feb 23, 2014 at 09:51 PM ·
rotationphysicsrigidbodytorque
Rigidbody rotation with PID controller and torque
Hey, I am trying to stabilize a rigidybodys rotation by using torque; the goal is to set three angles or a local vector and the object stabilizes itself by using a PID controller. What I've got so far is this:
Vector3 x = Vector3.Cross(transform.up, Vector3.up);
float theta = Mathf.Asin(x.magnitude);
Vector3 w = x.normalized * theta / Time.fixedDeltaTime;
Quaternion q = transform.rotation * rigidbody.inertiaTensorRotation;
Vector3 T = q * Vector3.Scale(rigidbody.inertiaTensor, (Quaternion.Inverse(q) * w));
Vector3 rotationCorrection = PID.Update(T, Time.deltaTime);
rigidbody.AddTorque(Vector3.Normalize(rotationCorrection) * Mathf.Clamp(Vector3.Magnitude(rotationCorrection), -5.0f, 5.0f), ForceMode.Acceleration);
This is based on code I found in the forums and modified it but this only controlls two of three axis, in this case you can rotate the object around the 'up'-axis. The final goal is in addition to the stabilization to make so object rotate e.g. 15° forward like an accelerating hellicopter and stabilize it in this rotation. Has anyone an idea how to achieve this?
Thank you guys :)
Comment