メインコンテンツへスキップ

デバイスのワークスペース変換に関するチュートリアル

このチュートリアルは、 フォースフィードバックの基礎チュートリアル …を開き、デバイスの位置、回転、拡大縮小の設定方法を示します Inverse3 その空間変換の特性や手法を活用して調整する。

はじめに​

「フォースフィードバックの基礎」のシーンを設定した後、ハプティック原点 やハプティックコントローラーの調整(移動、拡大縮小、回転など)を行っても、ハプティックフィードバックが期待どおりに変化しないことに気づくかもしれません。 フィードバックは、デバイスの前にある目に見えない球体に基づいており、これらの変形の影響を受けないままです。

ワークスペースの変換に失敗しました

このずれが生じるのは、力の計算にカーソルの実際の(変換前の)座標が使用されているためです。 これを解決するために、Inverse3 コントローラーコンポーネントが提供するスレッドセーフなキャッシュ済み変換行列を利用します。これにより、ハプティックフィードバックの計算にワールド座標変換を適用することができます。

シーン構成​

フォースフィードバックに関する基礎チュートリアルのシーンから始め、Inverse3 コントローラーの利き手がご使用のデバイスと一致していることを確認してください。Haply のロゴがカメラの方を向くようにハプティックコントローラーを回転させ、必要に応じてハプティックオリジンのスケールを調整して、カーソルの範囲を広げたり位置を調整したりしてください。

inverse3 右利き用セットアップ

ワークスペースの変換(右利き用)

フォースフィードバックスクリプトの変更点​

スクリプトをコピーしてください SphereForceFeedback.cs 「フォースフィードバック」の基礎チュートリアルから引用し、コールバック内で OnDeviceStateChanged 以下の調整を行いました:

  • 「」を置き換えてください device.CursorLocalPosition durch device.CursorPosition.
  • 「」を置き換えてください device.CursorLocalVelocity durch device.CursorVelocity.
  • 「」を置き換えてください device.CursorSetLocalForce(force) durch 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のパッケージマネージャーにある「サンプルチュートリアル」からインポートできます。

このサンプルシーンには、ハプティックコントローラーおよびハプティックオリジンの実行時調整を行うための追加スクリプトが含まれています。

スフィアフォースフィードバック.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);
}
}
}