フォースフィードバックの基礎チュートリアル
このチュートリアルでは、剛性と減衰の両方を考慮し、球体などの静的な物体との接触における物理的特性を再現する、シンプルなハプティックシミュレーションの作成手順を解説します。 このチュートリアルを完了すると、球体の存在を感知できるシミュレーションが完成し、その剛性や減衰特性を調整して、さまざまなハプティック体験を実現できるようになります。
はじめに
このチュートリアルの主な課題は、剛性と減衰の両方を備えた球体との接触時に生じる力を計算する関数を開発することです。 この文脈において、剛性はバネのように作用し、圧縮されるほど大きな力が発生します。 一方、減衰とは、物体が動くことに対する抵抗を指します。物体の動きが速いほど、この抵抗は大きくなります。
シーン構成
まず、以下の手順に従ってハプティック・リグの設定から始めてください。 「はじめに」のガイド について説明し、その位置、回転、および拡大縮小が ハプティック・オリジン auf (0, 0, 0) あるいは (1, 1, 1) が定められている。
その後、「」という名前の球を作成してください。 Sphere 以下の特徴を備えた:
- ポジション
(0, 0, -0.1)(本体の約10cm手前) - スケーリング:
(0.2, 0.2, 0.2)(直径20cmの球に相当する)
フォースフィードバックスクリプト
GameObjectに スフィア 「」という名前の新しいC#スクリプト SphereForceFeedback.cs hinzu.
このスクリプトは、球体との接触時に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 und _cursorRadius、シーンデータを設定するには:
private void SaveSceneData()
{
var t = transform;
_ballPosition = t.position;
_ballRadius = t.lossyScale.x / 2f;
_cursorRadius = inverse3.Cursor.Radius;
}
private void Awake()
{
SaveSceneData();
}
コールバックが OnDeviceStateChanged で OnEnable 登録され、かつ OnDisable インタラクション中にフォースフィードバックを正しく処理するために、再度ログアウトされます:
protected void OnEnable()
{
inverse3.DeviceStateChanged += OnDeviceStateChanged;
}
protected void OnDisable()
{
inverse3.DeviceStateChanged -= OnDeviceStateChanged;
}
ゲームプレイ体験
Inverse3 のカーソルを押し続けたまま、プレイモードを起動し、ボールに触れてみてください。 ボールの存在感を感じられるはずですし、Unityのインスペクターからボールの剛性や減衰特性を調整することもできます。これにより、仮想オブジェクトとのインタラクションをリアルに体感することができます。

ソースファイル
このサンプルに関するシーン全体および関連するすべてのファイルは、Unityのパッケージマネージャーにある「サンプルチュートリアル」からインポートできます。
スフィアフォースフィードバック.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);
}
}
}