Math & Physics
Intermediate
Unity 2021.3+ / Unity 6
목표 지점에 도달하기 위한 포물선 투사체 발사 각도 및 초기 속도 계산Calculating Ballistic Launch Angle and Initial Velocity to Hit a Target
중력 가속도(g)와 고도차(h)가 존재하는 2D/3D 환경에서 목표 좌표(Target)에 투사체를 도달시키기 위한 2차 방정식 탄도학 수식 풀이입니다.Solving the quadratic ballistic trajectory equations in 2D/3D space with gravity (g) and elevation difference (h) to compute the exact launch velocity vector.
#Math #Physics #Ballistics #Trajectory #Gameplay
핵심 개념수류탄 투척이나 곡사포 발사 시 원하는 목표 지점에 투사체가 탄착하게 하려면 수직 중력 방정식 $y = v_y t - \frac{1}{2}gt^2$와 수평 등속 방정식 $x = v_x t$를 연립해야 합니다. 초기 속력 $v$가 고정되었을 때 판별식을 통해 사거리 도달 가능 여부를 판정하고 발사각 $\theta$를 역산할 수 있습니다.
Key ConceptFor a grenade or artillery shell to land on a specific target coordinate, the vertical gravity equation $y = v_y t - \frac{1}{2}gt^2$ and horizontal motion equation $x = v_x t$ must be solved simultaneously. Given a fixed launch speed $v$, the discriminant determines whether the target is within range and yields the launch angle $\theta$.
1. 탄도학 발사 벡터 계산 구현1. Ballistic Launch Velocity Calculation in C#
using UnityEngine;
public static class BallisticsMath
{
/// <summary>
/// 고정된 발사 속력으로 목표점에 도달하기 위한 초기 속도 벡터를 반환합니다.
/// </summary>
public static bool TryCalculateLaunchVelocity(
Vector3 start, Vector3 target, float speed, float gravity, out Vector3 velocity)
{
velocity = Vector3.zero;
Vector3 diff = target - start;
Vector3 diffXZ = new Vector3(diff.x, 0f, diff.z);
float x = diffXZ.magnitude;
if (x < 0.001f) return false; // 수평 거리가 0에 가까울 때 0 나눗셈 방지
float y = diff.y;
float v2 = speed * speed;
float v4 = v2 * v2;
float g = Mathf.Abs(gravity);
float underSqrt = v4 - g * (g * x * x + 2f * y * v2);
if (underSqrt < 0f) return false; // 사거리 부족 (도달 불가)
float sqrtTerm = Mathf.Sqrt(underSqrt);
// 저각 탄도(직사): (v2 - sqrtTerm), 고각 탄도(곡사): (v2 + sqrtTerm)
float angle = Mathf.Atan((v2 - sqrtTerm) / (g * x));
Vector3 dirXZ = diffXZ / x;
velocity = dirXZ * (speed * Mathf.Cos(angle)) + Vector3.up * (speed * Mathf.Sin(angle));
return true;
}
}
using UnityEngine;
public static class BallisticsMath
{
/// <summary>
/// Computes the initial launch velocity vector required to hit a target at a fixed speed.
/// </summary>
public static bool TryCalculateLaunchVelocity(
Vector3 start, Vector3 target, float speed, float gravity, out Vector3 velocity)
{
velocity = Vector3.zero;
Vector3 diff = target - start;
Vector3 diffXZ = new Vector3(diff.x, 0f, diff.z);
float x = diffXZ.magnitude;
if (x < 0.001f) return false; // Guard against division by zero when horizontal distance is near zero
float y = diff.y;
float v2 = speed * speed;
float v4 = v2 * v2;
float g = Mathf.Abs(gravity);
float underSqrt = v4 - g * (g * x * x + 2f * y * v2);
if (underSqrt < 0f) return false; // Target out of range for given speed
float sqrtTerm = Mathf.Sqrt(underSqrt);
// Low-angle direct arc: (v2 - sqrtTerm); High-angle mortar arc: (v2 + sqrtTerm)
float angle = Mathf.Atan((v2 - sqrtTerm) / (g * x));
Vector3 dirXZ = diffXZ / x;
velocity = dirXZ * (speed * Mathf.Cos(angle)) + Vector3.up * (speed * Mathf.Sin(angle));
return true;
}
}
2. 실무 적용 및 주의사항2. Practical Considerations and Caveats
- 저각(Low-Arc)과 고각(High-Arc) 탄도 선택: 2차 방정식의 두 해 중
v2 - sqrtTerm은 비행 시간이 짧은 직사(저각) 궤적을 만들고,v2 + sqrtTerm은 장애물 너머로 높게 솟구치는 박격포식 곡사(고각) 궤적을 만듭니다. - 공기 저항(Linear Drag) 비활성화 확인: 위 해석해는 진공 상태의 포물선 방정식을 전제로 하므로, 투사체의
Rigidbody.linearDamping(구drag) 값이 0보다 크면 실제 탄착점이 목표보다 짧아집니다. 궤적 일치가 필요하면 공기 저항을0으로 설정해야 합니다. - 이동 중인 타깃에 대한 선행 조준(Lead Targeting): 목표물이 속도 $\vec{v}_{target}$으로 이동 중이라면, 먼저 현재 거리 기준 예상 비행 시간 $t = \frac{x}{v \cos\theta}$를 구한 뒤 목표 좌표를 $\text{target} + \vec{v}_{target} \cdot t$로 보정하여 재계산하면 명중률을 높일 수 있습니다.
- Choosing Between Low-Arc and High-Arc Solutions: The quadratic formula yields two valid trajectories:
v2 - sqrtTermproduces a fast, flat direct shot, whereasv2 + sqrtTermproduces a high mortar lob that clears intermediate obstacles. - Set Rigidbody Drag to Zero: This analytical solution assumes vacuum kinematics without air resistance; if
Rigidbody.linearDamping(formerlydrag) is greater than zero, the projectile will fall short of the predicted impact point. - Iterative Lead Targeting for Moving Targets: When aiming at a target moving with velocity $\vec{v}_{target}$, estimate the time of flight $t = \frac{x}{v \cos\theta}$ and recompute the launch vector toward the predicted position $\text{target} + \vec{v}_{target} \cdot t$.
판별식을 통한 사거리 도달 검사와 저각·고각 선택 수식을 활용하면, AI 포탑이나 투척 무기 시스템에서 지형 고도차에 구애받지 않는 정확한 탄도 계산을 구현할 수 있습니다.
Evaluating the ballistic discriminant alongside selectable low/high launch arcs allows AI turrets and throwable weapon systems to hit targets reliably across uneven terrain.