How to calculate correct angles for PID?
Hello.
I'm working on flying mechanic for space craft and I use PID to calculate it's movement.
I have a target which I'd like my ship to look to. So, here is my implementation, most part of it is taken from this great tutorial about PID.
Also, it may be worth it to take a look at PID implementation for Unity
// Target rotation between ship and its target
Quaternion targetRotation = Quaternion.LookRotation(transform.position - Target.position);
// The "error" between current angle and desired (to look at the target)
float xAngleError = Mathf.DeltaAngle(transform.rotation.eulerAngles.x, targetRotation.eulerAngles.x);
// Calculate correction via PID
float xTorqueCorrection = xAxisPIDController.GetOutput(xAngleError, Time.fixedDeltaTime);
// Apply to rigid body
_rigidbody.AddRelativeTorque(xTorqueCorrection * Vector3.right, ForceMode.Force);
And how it looks:
So, there are a few problems I have, but I think all of them are because of angles calculation (Even tutorial's author mentioned it).
When target is directly above the ship, it starts to spin.
I think it happens because of the way I calculate errors. It works great for all the Z and Y, but not for X.
Please, help me find the correct way to calculate errors for PID.
Your answer
Follow this Question
Related Questions
Predict the hit position 0 Answers
Rotate sphere to the direction of movement 0 Answers
Why does this quaternion euler not compute? 0 Answers