[Android,OpenGL,Java] Quaternion to Euler
- Mobile/android
- 2012. 4. 16.
Reference URL
http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm
위의 주소를 참조하시면 됩니다만!
코드를 수정할 때 Quaternion 이 normalized 되어 있다면, 위에 방식으로, 아니면 아래 방식으로 사용함.
01. normalized
public vector3f Quat2Angle(float x, float y, float z, float w) {
double pitch, roll, yaw;
double test = x * y + z * w;
if (test > 0.499) { // singularity at north pole
yaw = 2 * Math.atan2(x, w);
pitch = Math.PI / 2;
roll = 0;
vector3f euler = new vector3f((float) pitch, (float) roll,
(float) yaw);
return euler;
}
if (test < -0.499) { // singularity at south pole
yaw = -2 * Math.atan2(x, w);
pitch = -Math.PI / 2;
roll = 0;
vector3f euler = new vector3f((float) pitch, (float) roll,
(float) yaw);
return euler;
}
double sqx = x * x;
double sqy = y * y;
double sqz = z * z;
yaw = Math.atan2(2 * y * w - 2 * x * z, 1 - 2 * sqy - 2 * sqz);
pitch = Math.asin(2 * test);
roll = Math.atan2(2 * x * w - 2 * y * z, 1 - 2 * sqx - 2 * sqz);
vector3f euler = new vector3f((float) pitch, (float) roll, (float) yaw);
return euler;
}
gg
01. non-normalized
public vector3f Quat2Angle(float x, float y, float z, float w) {
double pitch, roll, yaw;
double sqw = w * w;
double sqx = x * x;
double sqy = y * y;
double sqz = z * z;
double unit = (sqx * sqy) + (sqz * sqw);
double test = (x * y) + (z * w);
// singularity at north pole
if (test > 0.499 * unit) {
pitch = 0.0;
roll = 2.0 * Math.atan2(x, w);
yaw = Math.PI / 2;
vector3f euler = new vector3f((float) pitch, (float) roll, (float) yaw);
return euler;
}
// singularity at south pole
if (test < -0.499 * unit) {
pitch = 0.0;
roll = -2.0 * Math.atan2(x, w);
yaw = Math.PI / 2;
vector3f euler = new vector3f((float) pitch, (float) roll,(float) yaw);
return euler;
}
// attitude = pitch (X Axis of OSG)
// heading = roll (Y Axis of OSG)
// bank = yaw (Z Axis of OSG)
pitch = Math.atan2((2.0 * x * w) - (2.0 * y * z), -sqx + sqy - sqz + sqw);
roll = Math.atan2((2.0 * y * w) - (2.0 * x * z), sqx - sqy - sqz + sqw);
yaw = Math.asin(2.0 * test / unit);
vector3f euler = new vector3f((float) pitch, (float) roll, (float) yaw);
return euler;
}*/
코드를 따라서 작성해보고, 위의 URL 에서 계산해서 검증할 수 있다.
확인을 꼭 해보도록 하자.
그리고 인터넷 소스는 항상 신뢰하지 말자. (수식을 보고 확인을 하고 넘어가도록 하자. )
다 읽었으면 배너 클릭! (뭐 못보면 할 수 없고...)
'Mobile > android' 카테고리의 다른 글
| Android Eclipse Setting (0) | 2012.07.10 |
|---|---|
| 안드로이드 디버깅하기 (1) | 2012.05.25 |
| [android] JSON parsing (0) | 2012.02.21 |
| [Android] JNI 에서 조심해야 할 사항! (unsatisfiedlinkerror) (1) | 2012.01.18 |
| Easy logging switch between debug/release versions (0) | 2012.01.17 |