기본 힘과 위치 튜토리얼
이 가이드는 힘을 가하고 Inverse3 커서의 움직임을 시각화하는 간단한 데모를 제공합니다. 마지막으로 Inverse3 은 가상의 고무 밴드로 시작 위치에 묶여 있는 느낌을 시뮬레이션하고 구형 게임 오브젝트가 커서의 위치를 표시합니다.
소개
빠른 시작 가이드에서는 Inverse3 객체와 그 기능, 그리고 일정한 힘을 생성하는 방법을 소개했습니다. 여기서 우리의 목표는 커서에 고무줄 효과를 시뮬레이션하는 것입니다. 고무줄은 스프링과 유사하게 작동하므로 힘은 강성과 두 끝점 사이의 거리 모두에 영향을 받습니다. 따라서 위치와 강성이 주어지면 커서가 원점에서 멀어지지 않도록 저항하는 힘을 생성하는 함수를 고안하는 것이 목표입니다.
장면 설정
게임 오브젝트 > Haply 메뉴에서 햅틱 릭(한 손) 을 생성하여 시작합니다.
ForceAndPosition 컴포넌트
를 선택하고 햅틱 오리진 게임 오브젝트에서 새 스크립트를 추가합니다. 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);
}
이 세그먼트는 강성을 100N/m(뉴턴/미터)로 설정하여 비교적 부드러운 스프링을 시뮬레이션합니다.
또한 다음을 소개합니다. inverse3.WorkspaceCenterLocalPosition
를 사용하여 작업 영역의 중심을 반환하는 속성을 사용할 수 있습니다.
이 방법은 작업 영역 중심에서 커서의 위치를 빼서 힘을 계산한 다음 결과에 강성을 곱합니다.
마지막으로 커서에 힘을 가하기 위해 inverse3.SetCursorLocalForce
.
통합 OnDeviceStateChanged
콜백의 OnEnable
그리고 OnDisable
메서드에 설명된 대로 빠른 시작 가이드:
protected void OnEnable()
{
inverse3.DeviceStateChanged += OnDeviceStateChanged;
}
protected void OnDisable()
{
inverse3.DeviceStateChanged -= OnDeviceStateChanged;
}
게임 플레이
Inverse3 커서를 잡고 재생 모드를 활성화한 다음 기기를 조작해 봅니다. 커서를 움직이면 힘이 발생하는 것을 확인할 수 있습니다. 커서가 시작 위치에서 더 멀리 이동할수록 이 힘은 더 뚜렷해집니다.
소스 파일
이 예제에서 사용된 최종 씬과 모든 관련 파일은 Unity 패키지 관리자의 튜토리얼 샘플에서 임포트할 수 있습니다.
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);
}
}
}