본문으로 건너뛰기

디바이스 워크스페이스 변환 튜토리얼

이 튜토리얼은기본적인 힘 감각 피드백튜토리얼을 발전시킨 것으로, 공간 변환의 속성과 메서드를 사용하여 Inverse3 장치의 위치, 회전, 크기를 조정하는 방법을 설명합니다.

서문​

기본적인 힘 피드백 장면을 설정한 후,Haptic Origin이나 Haptic Controller를 이동하거나 확대·축소, 회전시켜도 힘 피드백에 예상대로 반영되지 않는다는 것을 알게 될 수도 있습니다. 피드백은 여전히 장치 전방에 위치한 보이지 않는 구체에 대응하며, 이러한 변환의 영향을 받지 않습니다.

작업 공간 변환이 적용되지 않는 예시

이러한 불일치는 힘 계산에 장치의 커서가 변환되지 않은 실제 좌표가 사용되기 때문에 발생합니다. 이를 해결하기 위해,‘Inverse3 Controller’ 컴포넌트가제공하는 스레드 안전 캐시된 변환 행렬을 활용하여, 힘 피드백 계산에 월드 공간 변환을 적용할 수 있도록 합니다.

장면 설정​

기본 힘 감각 피드백 튜토리얼의 장면부터 시작하여,‘Inverse3 ’ 컨트롤러의 주 손 설정이 사용 중인 기기와 일치하는지 확인하십시오. Haptic Controller를 회전시켜 ‘ Haply ’ 로고가 카메라를 향하도록 하고, 필요에 따라 ‘Haptic Origin’의 크기를 조정하여 커서의 이동 범위를 넓히거나 위치를 조정하십시오.

Inverse3 오른손 계열의 설정

작업 공간 변환 (오른손 좌표계)

힘 감지 피드백 스크립트의 변경 사항​

기본적인 힘 감각 피드백 튜토리얼부터 SphereForceFeedback.cs 스크립트를 복사한 다음,OnDeviceStateChanged 콜백 내에서 다음과 같이 수정해 주세요.

  • device.CursorLocalPosition 를 device.CursorPosition 로 대체합니다.
  • device.CursorLocalVelocity 를 device.CursorVelocity 로 대체합니다.
  • device.CursorSetLocalForce(force) 를 device.CursorSetForce(force) 로 대체합니다.
private void OnDeviceStateChanged(object sender, Inverse3EventArgs args)
{
var inverse3 = args.DeviceController;
var force = ForceCalculation(inverse3.CursorPosition, inverse3.CursorVelocity,
_cursorRadius, _ballPosition, _ballRadius);

inverse3.SetCursorForce(force);
}

게임 플레이 체험​

Inverse3 커서를 누른 상태로 ‘Play Mode’로 들어갑니다. 앞의 예시와 마찬가지로 구체를 터치해 보세요. ‘Haptic Origin ’과 ‘Haptic Controller’에 적용된 변환이 반영된, 정확한 힘 감각 피드백을 경험할 수 있을 것입니다.

확대된 커서가 구체에 닿는 모습

소스 파일​

이 예제의 전체 장면과 관련 파일은 Unity의 패키지 관리자(Package Manager)에 있는 Tutorials 샘플에서 가져올 수 있습니다.

이 샘플 씬에는 실행 시 Haptic Controller와 Haptic Origin을 조정하기 위한 추가 스크립트가 포함되어 있습니다.

SphereForceFeedback.cs​

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

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

namespace Haply.Samples.Tutorials._3_DeviceSpaceTransform
{
public class SphereForceFeedback : MonoBehaviour
{
// must assign in inspector
public Inverse3Controller inverse3;

[Range(0, 800)]
// Stiffness of the force feedback.
public float stiffness = 300f;

[Range(0, 3)]
public float damping = 1f;

private Vector3 _ballPosition;
private float _ballRadius;
private float _cursorRadius;

/// <summary>
/// Stores the cursor and sphere transform data for access by the haptic thread.
/// </summary>
private void SaveSceneData()
{
var t = transform;
_ballPosition = t.position;
_ballRadius = t.lossyScale.x / 2f;

_cursorRadius = inverse3.Cursor.Radius;
}

/// <summary>
/// Saves the initial scene data cache.
/// </summary>
private void Awake()
{
inverse3 ??= FindFirstObjectByType<Inverse3Controller>();
inverse3.Ready.AddListener((device, args) => SaveSceneData());
}

/// <summary>
/// Subscribes to the DeviceStateChanged event.
/// </summary>
private void OnEnable()
{
inverse3.DeviceStateChanged += OnDeviceStateChanged;
}

/// <summary>
/// Unsubscribes from the DeviceStateChanged event.
/// </summary>
private void OnDisable()
{
inverse3.DeviceStateChanged -= OnDeviceStateChanged;
inverse3.Release();
}

/// <summary>
/// Calculates the force based on the cursor's position and another sphere position.
/// </summary>
/// <param name="cursorPosition">The position of the cursor.</param>
/// <param name="cursorVelocity">The velocity of the cursor.</param>
/// <param name="cursorRadius">The radius of the cursor.</param>
/// <param name="otherPosition">The position of the other sphere (e.g., ball).</param>
/// <param name="otherRadius">The radius of the other sphere.</param>
/// <returns>The calculated force vector.</returns>
private Vector3 ForceCalculation(Vector3 cursorPosition, Vector3 cursorVelocity, float cursorRadius,
Vector3 otherPosition, float otherRadius)
{
var force = Vector3.zero;

var distanceVector = cursorPosition - otherPosition;
var distance = distanceVector.magnitude;
var penetration = otherRadius + cursorRadius - distance;

if (penetration > 0)
{
// Normalize the distance vector to get the direction of the force
var normal = distanceVector.normalized;

// Calculate the force based on penetration
force = normal * penetration * stiffness;

// Apply damping based on the cursor velocity
force -= cursorVelocity * damping;
}

return force;
}

/// <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 ball force. Using 'device.Position' instead of 'device.LocalPosition'
// ensures the force calculation considers the device's offset and rotation in world space.
var force = ForceCalculation(inverse3.CursorPosition, inverse3.CursorVelocity,
_cursorRadius, _ballPosition, _ballRadius);

// Apply the calculated force to the cursor. Using 'device.CursorSetForce' instead of
// 'device.CursorSetLocalForce' ensures that the force vector is correctly converted
// from world space to the device's local space.
inverse3.SetCursorForce(force);
}
}
}