C # how to realize the conversion between Euler angle and quaternion
Editor to share with you how to achieve the conversion between Euler angle and quaternion, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article. Let's learn about it.
/ / Euler Angle to Quaternion
Void cjx_from_euler (float roll, float pitch, float yaw)
{
Float cr2 = cosf (roll*0.5f)
Float cp2 = cosf (pitch*0.5f)
Float cy2 = cosf (yaw*0.5f)
Float sr2 = sinf (roll*0.5f)
Float sp2 = sinf (pitch*0.5f)
Float sy2 = sinf (yaw*0.5f)
Q1 = cr2*cp2*cy2 + sr2*sp2*sy2
Q2 = sr2*cp2*cy2-cr2*sp2*sy2
Q3 = cr2*sp2*cy2 + sr2*cp2*sy2
Q4 = cr2*cp2*sy2-sr2*sp2*cy2
}
# define RAD_TO_DEG 57.29577951f
/ / Radian to angle
Float degrees (float rad)
{
Return rad * RAD_TO_DEG
}
/ / Quaternion to Euler angle
Void cjx_to_euler (float * roll, float * pitch, float * yaw)
{
If (roll) {
* roll = degrees (atan2f (2.0f* (q1*q2 + q3*q4), 1-2.0f* (q2*q2 + q3*q3)
}
If (pitch) {
/ / use safe_asin () to deal with the singularity when pitch is close to 90-90.
* pitch = degrees (safe_asin (2.0f * (q1*q3-q2*q4)
}
If (yaw) {
* yaw = degrees (atan2f (2.0f* (q2*q3-q1*q4), 2.0f* (q1*q1 + q2*q2)-1))
}
}
These are all the contents of the article "how to convert Euler Angle to Quaternion". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!