Math & Physics
Advanced
Unity 2021.3+ / Unity 6
짐벌락 없는 쿼터니언 부드러운 회전 보간(SmoothDamp Rotation)
Vector3 오일러 각의 360도 래핑 및 짐벌락 문제를 원천 차단하고 임계 감쇠 스프링-댐퍼 모델로 목표 회전에 부드럽게 안착하는 쿼터니언 보간 기법.
#Math #Quaternion #Rotation #Interpolation #Transform
핵심 개념카메라나 물체를 회전시킬 때
Mathf.SmoothDamp를 오일러 각(Euler Angles)에 적용하면 0도와 360도 경계에서 회전이 역방향으로 꼬이거나 짐벌락이 일어납니다. 쿼터니언의 최단 경로 내적 검사($Dot < 0$)와 각속도 댐핑을 캡슐화한 수식입니다.1. 쿼터니언 SmoothDamp 구현
using UnityEngine;
public static class QuaternionUtil
{
public static Quaternion SmoothDamp(
Quaternion rot, Quaternion target, ref Vector3 deriv, float time)
{
if (Time.deltaTime <= 0.00001f) return rot;
// 최단 경로 보정 (-q와 q는 같은 회전)
var dot = Quaternion.Dot(rot, target);
var multi = dot > 0f ? 1f : -1f;
target.x *= multi;
target.y *= multi;
target.z *= multi;
target.w *= multi;
// 보간 계산
var result = new Vector4(
Mathf.SmoothDamp(rot.x, target.x, ref deriv.x, time),
Mathf.SmoothDamp(rot.y, target.y, ref deriv.y, time),
Mathf.SmoothDamp(rot.z, target.z, ref deriv.z, time),
Mathf.SmoothDamp(rot.w, target.w, ref deriv.x, time)
).normalized;
return new Quaternion(result.x, result.y, result.z, result.w);
}
}