本文へスキップ

フォースフィードバックの基礎チュートリアル

このチュートリアルでは、球体のような静的オブジェクトと接触した際の物理的特性をシミュレートし、剛性(stiffness)と減衰(damping)の両方を反映した基本的なハプティックシミュレーションを作成する手順を解説します。 このチュートリアルを完了すると、球体の存在を感じることができ、さまざまなハプティック体験に合わせて剛性と減衰の特性を調整できるシミュレーションが完成します。

はじめに​

このチュートリアルの主な課題は、剛性と減衰の両方を備えた球体と接触した際に生じる力を計算する関数を開発することです。 この文脈において、剛性はバネのように作用し、圧縮されるほど大きな力が発生します。 一方、減衰は物体の動きに対する抵抗を表しており、物体が速く動くほど大きな抵抗が生じます。

シーンの設定​

はじめにガイド説明に従って、まずHaptic Rigの設定から始め、 ハプティック・オリジン位置、回転、スケールがそれぞれ (0, 0, 0)と (1, 1, 1)に設定されているか確認してください。

次に、以下のプロパティを持つ Sphereという名前の球体を作成してください:

  • 場所: (0, 0, -0.1) (装置の前面から約10cm)
  • スケール: (0.2, 0.2, 0.2) (直径20cmの球体に相当)

フォースフィードバックスクリプト​

スフィア GameObjectに SphereForceFeedback.csという名前の新しい C# スクリプトを追加してください。 このスクリプトは、球体と接触した際に「Inverse3 」カーサーに加わる力を、剛性と減衰の両方を考慮して計算します。 スクリプトを次のプロパティで初期化してください:

[SerializeField]
private Inverse3 inverse3;

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

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

力の計算は、カーソルが球体に侵入したときにのみ行われるべきであり、これにより、定義された剛性と減衰を持つ実際の物体に触れているような感覚がシミュレートされます。 これを担当するのは ForceCalculation これはメソッドであり、カーソルの位置と速度の両方を考慮します:

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;
}

private void OnDeviceStateChanged(object sender, Inverse3EventArgs args)
{
var inverse3 = args.DeviceController;
// Calculate the ball force
var force = ForceCalculation(inverse3.CursorLocalPosition, inverse3.CursorLocalVelocity,
_cursorRadius, _ballPosition, _ballRadius);

inverse3.SetCursorLocalForce(force);
}

Awake メソッド内でシーンデータを設定するために _ballPosition, _ballRadius, _cursorRadiusを初期化してください:

private void SaveSceneData()
{
var t = transform;
_ballPosition = t.position;
_ballRadius = t.lossyScale.x / 2f;

_cursorRadius = inverse3.Cursor.Radius;
}

private void Awake()
{
SaveSceneData();
}

インタラクション中にフォースフィードバックを適切に処理するために OnEnable および OnDisable 各メソッドにおいて OnDeviceStateChanged コールバックを登録および解除する必要があります:

protected void OnEnable()
{
inverse3.DeviceStateChanged += OnDeviceStateChanged;
}

protected void OnDisable()
{
inverse3.DeviceStateChanged -= OnDeviceStateChanged;
}

ゲームプレイ体験​

Inverse3 カーソルを操作した状態で「Play Mode」に入り、球体を触ってみてください。 球体の存在感を感じることができるはずです。また、Unityのインスペクターを通じて球体の剛性や減衰特性を調整できるため、仮想オブジェクトとのリアルなインタラクションを体験できます。

カーソルが球体に接触している様子

ソースファイル​

この例のシーン全体および関連するすべてのファイルは、Unity Package Managerの「Tutorials」サンプルから入手できます。

スフィアフォースフィードバック.cs​

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

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

namespace Haply.Samples.Tutorials._2_BasicForceFeedback
{
public class SphereForceFeedback : MonoBehaviour
{
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 ??= FindObjectOfType<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
var force = ForceCalculation(inverse3.CursorLocalPosition, inverse3.CursorLocalVelocity,
_cursorRadius, _ballPosition, _ballRadius);

inverse3.SetCursorLocalForce(force);
}
}
}