본문으로 건너뛰기

기본 힘 및 위치 튜토리얼

이 가이드는 Inverse3 커서에 힘을 적용하고 그 움직임을 시각화하는 간단한 예제를 제공합니다. 마지막에는 Inverse3 장치가 가상의 고무줄로 시작 위치에 묶여 있는 듯한 감각을 시뮬레이션하며, 구형 GameObject가 커서의 위치를 표시합니다.

소개​

시작하기 가이드에서는 Inverse3 오브젝트, 그 기능, 그리고 일정한 힘을 생성하는 방법을 소개했습니다. 여기서의 목표는 커서에 고무줄 효과를 시뮬레이션하는 것입니다. 고무줄은 스프링과 유사하게 동작하므로, 그 힘은 강성과 두 끝점 사이의 거리 모두의 영향을 받습니다. 따라서 위치와 강성을 입력받아, 커서가 원점에서 멀어지는 움직임에 저항하는 힘을 생성하는 함수를 고안하는 것을 목표로 합니다.

씬 설정​

GameObjects > Haply 메뉴를 통해 **Haptic Rig (one hand)**를 생성하며 시작합니다.

ForceAndPosition 컴포넌트​

Haptic Origin GameObject를 선택하고 ForceAndPosition.cs라는 새 스크립트를 추가한 다음, 아래 코드로 ForceAndPosition 클래스를 작성합니다:

[SerializeField]
private Inverse3 inverse3 = null;

[SerializeField, Range(0, 400)]
private float stiffness = 100;

private void OnDeviceStateChanged(object sender, Inverse3EventArgs args)
{
var inverse3 = args.DeviceController;

// Calculate the force.
var force = (inverse3.WorkspaceCenterLocalPosition - inverse3.CursorLocalPosition) * stiffness;

// Apply the force to the cursor.
inverse3.SetCursorLocalForce(force);
}

이 코드 조각은 강성을 100 뉴턴/미터(N/m)로 설정하여 비교적 부드러운 스프링을 시뮬레이션합니다. 또한 작업 공간의 중심을 반환하는 속성인 inverse3.WorkspaceCenterLocalPosition을 도입합니다. 이 메서드는 작업 공간 중심에서 커서의 위치를 뺀 다음, 그 결과에 강성을 곱하여 힘을 계산합니다. 마지막으로 inverse3.SetCursorLocalForce를 사용하여 힘을 커서에 적용합니다.

시작하기 가이드에 자세히 설명된 대로, OnEnable 및 OnDisable 메서드에 OnDeviceStateChanged 콜백을 포함하십시오:

protected void OnEnable()
{
inverse3.DeviceStateChanged += OnDeviceStateChanged;
}

protected void OnDisable()
{
inverse3.DeviceStateChanged -= OnDeviceStateChanged;
}

게임플레이​

Inverse3 커서를 잡은 상태에서 Play Mode를 활성화하고 장치를 움직여 보십시오. 커서를 이동하면 힘이 발생하는 것을 확인할 수 있습니다. 커서가 시작 위치에서 멀리 이동할수록 이 힘은 더욱 뚜렷하게 느껴집니다.

커서 이동

소스 파일​

이 예제에 사용된 최종 씬과 모든 관련 파일은 Unity의 패키지 관리자에서 Tutorials 샘플을 통해 가져올 수 있습니다.

ForceAndPosition.cs​

/*
* Copyright 2024 Haply Robotics Inc. All rights reserved.
*/

using Haply.Inverse.DeviceControllers;
using Haply.Inverse.DeviceData;
using UnityEngine;

namespace Haply.Samples.Tutorials._1_ForceAndPosition
{
/// <summary>
/// Demonstrates the application of force to maintain the cursor at its center position.
/// </summary>
public class ForceAndPosition : MonoBehaviour
{
public Inverse3Controller inverse3;

[Range(0, 400)]
// Stiffness of the force feedback.
public float stiffness = 100;

private void Awake()
{
inverse3 ??= FindObjectOfType<Inverse3Controller>();
}

/// <summary>
/// Subscribes to the DeviceStateChanged event when the component is enabled.
/// </summary>
protected void OnEnable()
{
inverse3.DeviceStateChanged += OnDeviceStateChanged;
}

/// <summary>
/// Unsubscribes from the DeviceStateChanged event and reset the force when the component is disabled.
/// </summary>
protected void OnDisable()
{
inverse3.DeviceStateChanged -= OnDeviceStateChanged;
inverse3.Release();
}

/// <summary>
/// Event handler that calculates and send the force to the device when the cursor's position changes.
/// </summary>
/// <param name="sender">The Inverse3 data object.</param>
/// <param name="args">The event arguments containing the device data.</param>
private void OnDeviceStateChanged(object sender, Inverse3EventArgs args)
{
var inverse3 = args.DeviceController;

// Calculate the force.
var force = (inverse3.WorkspaceCenterLocalPosition - inverse3.CursorLocalPosition) * stiffness;

// Apply the force to the cursor.
inverse3.SetCursorLocalForce(force);
}
}
}