쿼터니언을 이용한 임의의축 회전 문제 도움요청합니다.

조회수 998회

https://youtu.be/oXvPy1Q-UbU

쿼터니언을 이용하여 위와 같이 x,y축 으로 회전하도록 구현하고 싶습니다.(위 예시는 유니티) 유니티에서 구현했을때는 다음과 같이 코드를 작성했습니다.

transform.Rotate(new Vector3(1, 1, 0), 20 * Time.deltaTime);

하지만 opengl상에서 구현했을때에는 다음과 같이 큐브가 찌그러져서 회전을 하게 됩니다.

https://youtu.be/sOvXYHGaI00

아래는 실제로 제가 작성한 코드입니다.

Transform trans;

매프레임마다 호출되는 함수

trans->Rotate(Vector3d(1.f , 1.f ,0.f), 20 * Time::DeltaTime());

호출 API는 최대한 유니티와 비슷하게 하려고 하였습니다. Transform::Rotate 함수는 다음과 같이 구현되어 있습니다.

void Transform::Rotate(const Vector3d& axis, float angle) {
  // this is a problem
  mRotation = Quaternion::AngleAxis(Math::Radians(angle), Vector3d(axis).Normalize()) * mRotation;

  mRotation.Normalize();
}

여기서 mRotation은 쿼터니언의 객체입니다. 초기상태는 x,y,z=0, w=1 입니다. Quaternion::AngleAxis 구현 함수

Quaternion Quaternion::AngleAxis(const float angle, const Vector3d& axis) {
    const float halfAngle = angle * 0.5f;
    const float sin_half = std::sin(halfAngle);
    Quaternion result;

    result.w = std::cos(halfAngle);
    result.x = sin_half * axis.x;
    result.y = sin_half * axis.y;
    result.z = sin_half * axis.z;

    return result;
}

어떻게하면 유니티와 동일하게 회전을 구현할수 있을까요? 제가 잘못 이해한 부분이 어디인건가요?

  • (•́ ✖ •̀)
    알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)