メインコンテンツまでスキップ

基本的な力覚フィードバックのチュートリアル

このチュートリアルでは、球体などの静的オブジェクトとの接触がもつ物理特性を再現するため、剛性と減衰の両方を組み込んだ基本的な力覚シミュレーションを作成します。 このチュートリアルを終えると、球体の存在を感じ取り、異なる力覚体験に応じて剛性と減衰の特性を調整できるシミュレーションが完成します。

はじめに​

このチュートリアルの中心的な課題は、剛性と減衰の両方をもつ球体との接触によって生じる力を計算する関数を開発することです。 この文脈における剛性はばねのように働き、圧縮されるほど大きな力を生み出します。 一方、減衰はオブジェクトの動きに対する抵抗を表し、速く動かされるほど大きな抵抗を与えます。

シーンのセットアップ​

入門ガイドで説明しているとおりに Haptic Rig をセットアップします。その際、Haptic Origin の位置、回転、スケールがそれぞれ (0, 0, 0)、(1, 1, 1) に設定されていることを確認してください。

次に、以下のプロパティをもつ Sphere という名前の球体を作成します。

  • Position: (0, 0, -0.1)(デバイスの前方約10cm)
  • Scale: (0.2, 0.2, 0.2)(直径20cmの球体に相当)

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

Sphere 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 の Inspector を通じて剛性と減衰の特性を調整できます。これにより、仮想オブジェクトとのインタラクションを実感として得られます。

カーソルが球体に接触する様子

ソースファイル​

この例の完全なシーンと関連ファイルはすべて、Unity の Package Manager にある Tutorials サンプルからインポートできます。

SphereForceFeedback.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);
}
}
}