ABOUT PORTFOLIO GAMES DEVLOG DOCS TOOLS CONTACT
HOME > DOCS > Math & Physics > 3차 베지어 곡선(Cubic Bézier Curve) 보간 공식과 투사체 유도 미사일 궤적 계산Cubic Bézier Curve Interpolation and Homing Missile Trajectory Calculation
Math & Physics Beginner Unity 2021.3+ / Unity 6

3차 베지어 곡선(Cubic Bézier Curve) 보간 공식과 투사체 유도 미사일 궤적 계산Cubic Bézier Curve Interpolation and Homing Missile Trajectory Calculation

시작점, 도착점, 2개의 제어점(Control Points)을 활용하여 부드러운 카메라 궤적, 유도 미사일 경로, UI 커브를 생성하는 C# 수학 구현입니다.C# implementation of Cubic Bézier curve evaluation and tangent derivatives using start, end, and two control points for homing missile paths and camera rails.

#Math #Bezier #Spline #Trajectory #Interpolation
핵심 개념3차 베지어 곡선 공식 $B(t) = (1-t)^3 P_0 + 3(1-t)^2 t P_1 + 3(1-t) t^2 P_2 + t^3 P_3$ ($0 \le t \le 1$)은 시작점 $P_0$에서 끝점 $P_3$까지 두 제어점 $P_1, P_2$의 영향을 받아 부드럽게 휘어지는 곡선을 생성하며, 1차 도함수 $B'(t)$를 통해 진행 방향 접선 벡터를 즉시 구할 수 있습니다.
Key ConceptThe Cubic Bézier formula $B(t) = (1-t)^3 P_0 + 3(1-t)^2 t P_1 + 3(1-t) t^2 P_2 + t^3 P_3$ ($0 \le t \le 1$) interpolates a smooth curve from start point $P_0$ to end point $P_3$ shaped by control points $P_1$ and $P_2$, while its first derivative $B'(t)$ directly yields the forward tangent vector along the path.

1. 베지어 곡선 포인트 및 접선 벡터 계산기1. Bézier Curve Point and Tangent Vector Evaluator

BezierCurveUtil.cs
using UnityEngine;

public static class BezierCurveUtil
{
    public static Vector3 GetPoint(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
    {
        t = Mathf.Clamp01(t);
        float u = 1f - t;
        float tt = t * t;
        float uu = u * u;
        float uuu = uu * u;
        float ttt = tt * t;

        return (uuu * p0) + (3f * uu * t * p1) + (3f * u * tt * p2) + (ttt * p3);
    }

    // 곡선의 진행 방향(접선 벡터 / 1차 미분 도함수) 계산
    public static Vector3 GetTangent(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
    {
        t = Mathf.Clamp01(t);
        float u = 1f - t;
        return 3f * u * u * (p1 - p0) + 6f * u * t * (p2 - p1) + 3f * t * t * (p3 - p2);
    }
}
BezierCurveUtil.cs
using UnityEngine;

public static class BezierCurveUtil
{
    public static Vector3 GetPoint(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
    {
        t = Mathf.Clamp01(t);
        float u = 1f - t;
        float tt = t * t;
        float uu = u * u;
        float uuu = uu * u;
        float ttt = tt * t;

        return (uuu * p0) + (3f * uu * t * p1) + (3f * u * tt * p2) + (ttt * p3);
    }

    // Compute curve forward direction (tangent vector / first derivative)
    public static Vector3 GetTangent(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
    {
        t = Mathf.Clamp01(t);
        float u = 1f - t;
        return 3f * u * u * (p1 - p0) + 6f * u * t * (p2 - p1) + 3f * t * t * (p3 - p2);
    }
}

2. 실무 적용 및 주의사항2. Practical Considerations and Caveats

  • 접선 벡터(GetTangent)를 이용한 미사일 회전 정렬: 유도 미사일이 곡선을 따라 날아갈 때 매 프레임 Vector3 dir = GetTangent(p0, p1, p2, p3, t).normalized;를 구해 transform.forward = dir (2D에서는 transform.right = dir)로 설정하면 이동 궤적과 탄두 방향이 자연스럽게 일치합니다.
  • 파라미터 $t$와 실제 이동 속도 불균일성(Arc-Length Parameterization): 베지어 파라미터 $t$를 등속(t += Time.deltaTime)으로 증가시키면 제어점이 몰려 있는 구간에서는 느려지고 제어점이 먼 구간에서는 빨라집니다. 엄격한 등속 이동이 필요한 카메라 레일에서는 곡선을 잘게 쪼갠 호 길이 누적표(LUT)를 미리 계산해 보간해야 합니다.
  • 에디터 기즈모(OnDrawGizmos) 시각화: 씬 뷰에서 제어점 배치를 조정할 때 $t$를 0.05f 단위로 20등분하여 Gizmos.DrawLine으로 연결해 두면 실행 전에 미사일 궤적 곡률을 직관적으로 점검할 수 있습니다.
  • Orienting Projectiles via GetTangent: As a homing missile advances along the curve, evaluate Vector3 dir = GetTangent(p0, p1, p2, p3, t).normalized; each frame and assign transform.forward = dir (or transform.right = dir in 2D) so the projectile nose aligns with its velocity vector.
  • Non-Uniform Speed Across Parameter $t$ (Arc-Length Parameterization): Incrementing $t$ linearly (t += Time.deltaTime) causes speed to vary where control points are unevenly spaced. For constant-speed camera rails, precompute a cumulative arc-length lookup table (LUT) or normalize step size by speed / GetTangent(...).magnitude.
  • Scene View Visualization with OnDrawGizmos: Sampling GetPoint in increments of 0.05f (20 segments) and connecting them with Gizmos.DrawLine inside OnDrawGizmos makes tuning control point positions straightforward in the Editor.

베지어 곡선의 위치 다항식과 1차 도함수 접선 계산을 함께 활용하면, 별도의 애니메이션 클립 없이 런타임에 목표 위치가 변하는 동적 유도 탄도와 카메라 경로를 생성할 수 있습니다.

Evaluating both the Bernstein polynomial position and its first-derivative tangent enables smooth runtime trajectories for homing projectiles and procedural camera paths without pre-baked animation clips.

참고자료 & 관련 링크