設備の作業空間変換チュートリアル
このチュートリアルは 基本のフォースフィードバック チュートリアルを基に内容を拡張し、空間変換のプロパティやメソッドを使用して調整する方法を解説します Inverse3 デバイスの位置、回転、および拡大・縮小。
概要
基本の力フィードバックのシーンを構築した後、Haptic OriginやHaptic Controllerを移動、拡大縮小、回転などの調整を行っても、触覚フィードバックが期待通りに変化しないことに気づくかもしれません。 フィードバックは依然として、デバイスの正面にある目に見えない球体に対応しており、これらの変換の影響を受けません。

このようなずれが生じる原因は、力の計算に、デバイスのカーソルの実際の(変換前の)座標が使用されていることにあります。 この問題を解決するために、Inverse3 Controllerコンポーネントが提供するスレッドセーフな変換行列のキャッシュを活用し、ワールド空間の変換を触覚フィードバックの計算に適用します。
シーン設定
「基本のフォースフィードバックチュートリアル」のシーンから始め、Inverse3 コントローラーの左右設定がご使用のデバイスと一致していることを確認してください。 Hapticコントローラーを回転させて、「Haply 」のロゴがカメラの方を向くようにし、必要に応じてHaptic Originのズームを調整して、カーソルの範囲を広げたり位置を調整したりしてください。


フォースフィードバックスクリプトの修正
「フォースフィードバックの基礎」チュートリアルからコピー 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 のカーソルを握り、再生モードに入ります。前の例と同様に、球体に触れてみてください。 これで、Haptic OriginおよびHaptic Controllerに適用された変換を反映した、正確な触覚フィードバックを体験できるはずです。

ソースファイル
この例の完全なシーンおよび関連ファイルは、Unityのパッケージマネージャーにある「Tutorials」サンプルからインポートできます。
サンプルシナリオには、実行時に「Haptic Controller」および「Haptic Origin」を調整するための追加スクリプトが含まれています。
スフィアフォースフィードバック.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);
}
}
}