本文へスキップ
バージョン: 3.1.3

基本的な力と位置のチュートリアル

このガイドでは、力を加えてInverse3 カーソルの動きを視覚化する簡単なデモンストレーションを行います。 最後には、Inverse3 、カーソルの位置を球体のGameObjectで表示しながら、仮想のゴムバンドで開始位置に縛られている感覚をシミュレートします。

はじめに

クイック・スタート・ガイドではInverse3オブジェクトとその機能、そして一定の力を生成する方法を紹介しました。 ここでの目的は、カーソルのラバーバンド効果をシミュレートすることです。輪ゴムはバネと同じように動作し、その力は剛性と2つの端点間の距離の両方に影響されます。 したがって、位置と剛性が与えられ、カーソルが原点から離れる動きに抵抗する力を生成する関数を考案することを目的とします。

シーン設定

GameObjects >HaplyメニューからHaptic Rig(片手)を作成して開始します。

ForceAndPosition コンポーネント

を選択する。 ハプティック・オリジン という名前の新しいスクリプトを追加する。 ForceAndPosition.cs を入力し ForceAndPosition クラスに以下のコードを追加した:

[SerializeField]
private Inverse3 inverse3 = null;

[SerializeField, Range(0, 400)]
private float stiffness = 100;

private void OnDeviceStateChanged(object sender, Inverse3EventArgs args)
{
var inverse3 = args.DeviceController;

// Calculate the force.
var force = (inverse3.WorkspaceCenterLocalPosition - inverse3.CursorLocalPosition) * stiffness;

// Apply the force to the cursor.
inverse3.SetCursorLocalForce(force);
}

このセグメントでは、剛性を100ニュートン/メートル(N/m)に設定し、比較的柔らかいスプリングをシミュレートしています。 また inverse3.WorkspaceCenterLocalPositionワークスペースの中心を返すプロパティです。 このメソッドは、ワークスペースの中心からカーソルの位置を引くことによって力を計算し、その結果に剛性を掛けます。 最後に、力は inverse3.SetCursorLocalForce.

を組み込む。 OnDeviceStateChanged コールバックは OnEnable そして OnDisable に詳述されている。 クイックスタートガイド:

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

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

ゲームプレイ

Inverse3 、プレイモードを起動し、デバイスを操作してみる。カーソルを動かすと力が発生するのがわかるだろう。 カーソルが開始位置から遠ざかれば遠ざかるほど、この力は顕著になる。

カーソル移動

ソースファイル

この例で使用する最終的なシーンとすべての関連ファイルは、Unity のパッケージマネージャでTutorialsサンプルからインポートできます。

ForceAndPosition.cs

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

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

namespace Haply.Samples.Tutorials._1_ForceAndPosition
{
/// <summary>
/// Demonstrates the application of force to maintain the cursor at its center position.
/// </summary>
public class ForceAndPosition : MonoBehaviour
{
public Inverse3Controller inverse3;

[Range(0, 400)]
// Stiffness of the force feedback.
public float stiffness = 100;

private void Awake()
{
inverse3 ??= FindObjectOfType<Inverse3Controller>();
}

/// <summary>
/// Subscribes to the DeviceStateChanged event when the component is enabled.
/// </summary>
protected void OnEnable()
{
inverse3.DeviceStateChanged += OnDeviceStateChanged;
}

/// <summary>
/// Unsubscribes from the DeviceStateChanged event and reset the force when the component is disabled.
/// </summary>
protected void OnDisable()
{
inverse3.DeviceStateChanged -= OnDeviceStateChanged;
inverse3.Release();
}

/// <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 force.
var force = (inverse3.WorkspaceCenterLocalPosition - inverse3.CursorLocalPosition) * stiffness;

// Apply the force to the cursor.
inverse3.SetCursorLocalForce(force);
}
}
}