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

基礎力と位置に関するチュートリアル

このガイドでは、Inverse3 のカーソルに力を加え、その動きを可視化する方法について簡単に説明します。 設定が完了すると、Inverse3 は、あたかも仮想の輪ゴムで開始位置に固定されているかのような感覚を再現し、同時に球形のGameObjectがカーソルの位置を表示します。

概要​

入門ガイドでは、 Inverse3 オブジェクト、その機能、および定常力を生成する方法について説明します。 このセクションの目的は、カーソルにゴムバンドが作用する効果をシミュレートすることです。ゴムバンドの挙動はバネに似ており、つまりその作用力は剛性と両端点間の距離の両方の影響を受けます。 したがって、与えられた位置と剛性に基づいて力を生成し、カーソルが原点から遠ざかる動きに抵抗するようにする関数を設計します。

シーン設定​

まず、「GameObjects」>「Haply 」メニューから、「Haptic Rig (one hand)」を作成します。

ForceAndPosition コンポーネント​

選択 ハプティック・オリジン GameObject に、名前を 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 その力をカーソルに加える。

に従って 入門ガイド の詳細な説明については、 OnEnable 和 OnDisable 方法中に追加 OnDeviceStateChanged コールバック:

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