ABOUT PORTFOLIO GAMES DEVLOG DOCS TOOLS CONTACT
HOME > DOCS > Math & Physics > 와이어 로프 진자 운동 시뮬레이션 및 릴리즈 횡속도 증폭 관성 물리Wire Rope Pendulum Simulation and Release Momentum Boost Physics
Math & Physics Intermediate Unity 2021.3+ / Unity 6

와이어 로프 진자 운동 시뮬레이션 및 릴리즈 횡속도 증폭 관성 물리Wire Rope Pendulum Simulation and Release Momentum Boost Physics

2D 힌지·로프 조인트 기반 진자 궤적에서 접선 벡터(Tangent Vector)를 추출하고 로프 해제 시 횡가속도(Horizontal Boost)를 보존하는 관성 물리 구현입니다.Extracting 2D tangent vectors along a pendulum arc to apply swing thrust and preserving amplified horizontal momentum upon rope release in Unity.

#Math #Physics #2D Platformer #Mechanics #Vector
핵심 개념로프 액션 게임의 조작감은 스윙 중 가속이 붙는 방식과 로프를 놓았을 때(Release) 관성이 자연스럽게 이어지는지에서 결정됩니다. 플레이어 좌표와 앵커 좌표 사이의 방향 벡터에 수직인 2D 접선 벡터(Tangent: $(-y, x)$) 방향으로 스윙 추력을 가하고, 로프 해제 시점의 횡방향 속도에 증폭 계수(swingJumpBoost)를 적용해 점프 궤적으로 연결합니다.
Key ConceptResponsive rope-swinging mechanics depend on how pumping force is applied along the arc and how momentum carries over when detaching. Computing the 2D tangent vector ($(-y, x)$) perpendicular to the rope direction allows input thrust to follow the circular arc, while multiplying horizontal velocity by swingJumpBoost upon release converts swing speed into launch distance.

1. 관성 스윙 물리 계산 C# 구현1. Inertial Rope Swing Physics in C#

RopeSwingPhysics.cs
using UnityEngine;

public class RopeSwingPhysics : MonoBehaviour
{
    [SerializeField] private Rigidbody2D rb;
    [SerializeField] private float swingForce = 25f;
    [SerializeField] private float swingJumpBoost = 1.35f;
    [SerializeField] private float maxSwingSpeed = 22f;

    public void ApplySwingForce(Vector2 anchorPos, float horizontalInput)
    {
        if (Mathf.Abs(horizontalInput) < 0.01f) return;

        Vector2 ropeDir = (anchorPos - rb.position).normalized;
        // 로프 반경 벡터에 수직인 접선 벡터(Tangent: 90도 반시계 회전)
        Vector2 tangent = new Vector2(-ropeDir.y, ropeDir.x);

        // 플레이어의 좌우 입력 부호와 접선 벡터의 X 방향을 일치시킴
        if (tangent.x * horizontalInput < 0f)
        {
            tangent = -tangent;
        }

        rb.AddForce(tangent * (swingForce * Mathf.Abs(horizontalInput)), ForceMode2D.Force);

        // 과도한 원심 가속 방지를 위한 최대 속력 상한 클램핑
        if (rb.linearVelocity.sqrMagnitude > maxSwingSpeed * maxSwingSpeed)
        {
            rb.linearVelocity = rb.linearVelocity.normalized * maxSwingSpeed;
        }
    }

    public void ReleaseRope()
    {
        Vector2 v = rb.linearVelocity;
        // 로프 이탈 시 횡방향 관성 증폭 및 상향 속도 보정
        v.x *= swingJumpBoost;
        if (v.y > 0f) v.y *= 1.1f;
        rb.linearVelocity = v;
    }
}
RopeSwingPhysics.cs
using UnityEngine;

public class RopeSwingPhysics : MonoBehaviour
{
    [SerializeField] private Rigidbody2D rb;
    [SerializeField] private float swingForce = 25f;
    [SerializeField] private float swingJumpBoost = 1.35f;
    [SerializeField] private float maxSwingSpeed = 22f;

    public void ApplySwingForce(Vector2 anchorPos, float horizontalInput)
    {
        if (Mathf.Abs(horizontalInput) < 0.01f) return;

        Vector2 ropeDir = (anchorPos - rb.position).normalized;
        // Compute tangent vector perpendicular to the rope radius (90-degree CCW rotation)
        Vector2 tangent = new Vector2(-ropeDir.y, ropeDir.x);

        // Align tangent X direction with player horizontal input sign
        if (tangent.x * horizontalInput < 0f)
        {
            tangent = -tangent;
        }

        rb.AddForce(tangent * (swingForce * Mathf.Abs(horizontalInput)), ForceMode2D.Force);

        // Cap maximum swing speed to prevent runaway centripetal acceleration
        if (rb.linearVelocity.sqrMagnitude > maxSwingSpeed * maxSwingSpeed)
        {
            rb.linearVelocity = rb.linearVelocity.normalized * maxSwingSpeed;
        }
    }

    public void ReleaseRope()
    {
        Vector2 v = rb.linearVelocity;
        // Amplify horizontal inertia and slightly boost upward velocity on detach
        v.x *= swingJumpBoost;
        if (v.y > 0f) v.y *= 1.1f;
        rb.linearVelocity = v;
    }
}

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

  • 앵커 상단 데드존(Deadzone) 설정: 플레이어가 앵커보다 높이 올라가거나(rb.position.y > anchorPos.y) 로프가 수평에 가까워지면 접선 벡터의 x 성분이 0에 수렴해 좌우 방향이 급격히 뒤집힐 수 있습니다. ropeDir.y < 0.1f 구간에서는 추가 스윙 추력을 제한해 비정상적인 공중 회전을 방지해야 합니다.
  • 릴리즈 직후 지상 이동 감속(Air Drag) 유예: 일반 플랫포머 이동 코드는 매 프레임 rb.linearVelocity.x = inputX * moveSpeed로 속도를 덮어쓰기 때문에, 로프를 놓자마자 증폭된 관성 속도가 즉시 기본 이동 속도로 깎여버립니다. 로프 이탈 후 지면에 착지하기 전까지는 기존 가로 속도가 기본 이동 속도보다 빠를 때 이를 보존하는 모멘텀 보간 로직이 필요합니다.
  • DistanceJoint2D의 maxDistanceOnly 활성화: 강체 막대기가 아닌 휘어지는 로프를 표현하려면 DistanceJoint2D.maxDistanceOnly = true로 설정하여, 플레이어가 앵커 쪽으로 가까워질 때는 줄이 느슨해지고 최대 길이에 도달했을 때만 장력이 걸리도록 구성합니다.
  • Upper-Hemisphere Anchor Deadzone: When the player swings nearly horizontal to or above the anchor (ropeDir.y < 0.1f), tangent.x approaches zero and can flip sign abruptly. Suppress player swing thrust above this angle so gravity naturally pulls the character back down the arc.
  • Preserve Airborne Momentum After Release: Standard platformer controllers that overwrite rb.linearVelocity.x = inputX * moveSpeed every frame will immediately clamp the boosted release speed. Blend air control gradually until the character lands so launch momentum is retained.
  • Enable maxDistanceOnly on DistanceJoint2D: Setting DistanceJoint2D.maxDistanceOnly = true allows the rope to go slack when moving closer to the anchor while enforcing tension only at maximum radius.

접선 벡터 기반의 스윙 가속과 이탈 후 공중 관성 보존 규칙을 결합하면, 연속된 그래플링 포인트를 리듬감 있게 통과하는 2D 로프 액션 메커니즘을 구현할 수 있습니다.

Combining tangent-aligned swing forces with airborne momentum preservation after release creates fluid 2D grappling traversal across chained anchor points.

참고자료 & 관련 링크