メインコンテンツへ移動

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

このガイドでは、Inverse3 を使用して、力を加え、カーソルの移動を可視化する簡単なデモを紹介します。 最後に、Inverse3 によって、仮想のゴムバンドによって開始位置に引き留められているような感覚が再現され、球体の GameObject がカーソルの位置を表示します。

はじめに​

「はじめに」ガイドでは、このオブジェクト Inverse3、その機能、および一定の力を生成する方法について紹介しました。 ここでの目的は、スライダーにゴムバンドの効果をシミュレートすることです。ゴムバンドはバネと同様に振る舞います。その力は、剛性と両端間の距離の両方に依存します。 したがって、位置と剛性に基づいて、カーソルが原点から離れないようにする力を生成する関数を設計することを目指します。

シーンの設定​

まず、[GameObjects] > [Haply] メニューから、Haptic Rig(片手用)を作成してください。

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.

リマインダーを組み込んでください OnDeviceStateChanged 「メソッド」の中で OnEnable et 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);
}
}
}