2012-02-06 5 views
1

내 기본 게임에서 쿼터니언을 사용하여 기본 각도를 계산하고 있습니다.특정 축에서 쿼터니온의 투영 각도를 얻는 방법은 무엇입니까?

이제 특정 축을 기준으로 쿼터니온의 각도를 얻으려고합니다. 그래서, 쿼터니온과 새로운 축이 주어졌습니다. 투영 된 각도를 되돌릴 수 있습니까?

내게는 두 벡터 사이의 부호 각도를 계산하는 것이 확실한 방법입니다. 이 코드는 Unity3D C#으로 작성되었지만 반드시 그런 것은 아닙니다. 더 일반적인 방법입니다.

public static float DirectionalAngle(Vector3 from, Vector3 to, Vector3 up) 
{ 
    // project the vectors on the plane with up as a normal 
    Vector3 f = Vector3.Exclude(up, from); 
    Vector3 t = Vector3.Exclude(up, to); 

    // creates an angle-axis rotation 
    Quaternion q = Quaternion.FromToRotation(f, t); 

    // do something to get the angle out of the quaternion, 
    // given the new axis. 
    // TODO: Make this method. Doesn't exist. 
    Quaternion q2 = Quaternion.GetAngleFromAxis(q, Vector3.right); 

    return q2.Angle(q2); 
} 

항상 각도와 축 쿼터니언에서뿐만 아니라, X, Y, Z, W 및 오일러 각을 얻을 수있다. 하지만 쿼터니언에서 투영 된 각도를 축에 표시하는 방법은 없습니다.

답변

1

발견! 내가해야만 한 일은 쿼터니언을 로컬 공간에서 지향하는 방식으로 회전시키는 것뿐이었습니다. 이 방법은 내가 테스트 한 모든 비행기에서 소리가 나는 것처럼 보입니다.

public static float DirectionalAngle(Vector3 from, Vector3 to, Vector3 up) 
{ 
    // project the vectors on the plane with up as a normal 
    Vector3 f = Vector3.Exclude(up, from); 
    Vector3 t = Vector3.Exclude(up, to); 

    // rotate the vectors into y-up coordinates 
    Quaternion toZ = Quaternion.FromToRotation(up, Vector3.up); 
    f = toZ * f; 
    t = toZ * t; 

    // calculate the quaternion in between f and t. 
    Quaternion fromTo = Quaternion.FromToRotation(f,t); 

    // return the eulers. 
    return fromTo.eulerAngles.y; 
} 
+0

정확하게 이해하면; 투영 된 벡터'f'와't' 사이의 각도를 찾고 있지 않습니까? 이 경우 충분합니다. 'Vector3.Angle (f, t)' –

+0

부호없는 각도를 지정하면 안됩니다. 나는이 둘의 서명 된 (지향성) 각이 필요하다. 그래서 정말로 * from * to * to *. 그러나 나는 일반적으로 사용되는'atan2 '접근법을 싫어한다. – Marnix

+1

'atan2' 메소드는 실제로가는 길입니다. 이것은 대단히 비효율적이며 (여전히 어쨌든 후드 아래 atan2를 포함합니다). – JCooper

관련 문제