기초 힘과 위치 튜토리얼
이 가이드에서는 Inverse3 커서의 움직임에 힘을 가하고 이를 시각화하는 방법을 간략하게 보여줍니다. 이 과정을 마치면,Inverse3 마치 커서가 시작 위치에 가상의 고무줄로 묶여 있는 듯한 효과가 나타나며, 동시에 구형 GameObject가 커서의 위치를 표시합니다.
소개
입문 가이드에서는 Inverse3 객체, 그 기능, 그리고 일정한 힘을 생성하는 방법을 설명합니다. 이 절의 목표는 고무줄이 커서에 미치는 효과를 시뮬레이션하는 것입니다. 고무줄의 거동은 스프링과 유사하며, 즉 그 힘은 강성과 양 끝점 사이의 거리의 영향을 동시에 받습니다. 따라서, 주어진 위치와 강성에 따라 힘을 생성하여 커서가 원점으로부터 멀어지는 움직임을 저항하도록 하는 함수를 설계해야 합니다.
장면 설정
먼저, ‘GameObjects > Haply ’ 메뉴를 통해 ‘Haptic Rig (one hand)’를 생성합니다.
ForceAndPosition 컴포넌트
선택 햅틱 오리진 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 의 커서를 잡고 재생 모드로 전환한 다음, 장치를 조작해 보세요. 커서를 움직이면 힘이 발생하는 것을 확인할 수 있습니다. 커서가 시작 위치에서 멀어질수록 이 힘은 더욱 뚜렷해집니다.

원본 파일
이 예제에서 사용하는 최종 씬과 모든 관련 파일은 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);
}
}
}