本文へスキップ

VerseGrip 位置制御チュートリアル

このチュートリアルでは、VerseGripの回転値を使用して、Inverse3 デバイスのカーソル位置を直接制御する方法について説明します。この方法では、高頻度での更新を行うためにUnityのメインスレッドをバイパスします。

はじめに​

はじめにガイドで紹介された方式は標準的な更新周期(60Hz)で動作するのに対し、このチュートリアルでは、より高い周波数(1~4kHz)において、VerseGripデバイスの回転に応じてInverse3 のカーソル位置を動的に調整することを目的としています。 これは、ハプティックスレッドによってトリガーされる DeviceStateChanged イベントを利用して実装されます。

シーン設定​

まず、Haptic Rigを作成します:GameObject >Haply > Haptic Rig (one hand)。詳細については、スタートガイドを参照してください。

VerseGripPositionControl コンポーネント​

VerseGripPositionControl.csという名前の新しい C# スクリプトを作成し、これを ハプティック・オリジン ゲームオブジェクトに接続します。 VerseGripPositionControl クラス内に以下のプロパティを定義します:

public Inverse3 inverse3;
public VerseGrip verseGrip;

[Range(0, 1)]
public float speed = 0.5f;

[Range(0, 0.2f)]
public float movementLimitRadius = 0.2f;

private Vector3 _targetPosition;
  • inverse3: インスペクタを通じて設定されるInverse3 デバイスへの参照です。
  • verseGrip:カーソル制御に使用されるVerseGripデバイスに関するリファレンスです。
  • 速度:カーソルの移動速度です。
  • movementLimitRadius: カーソルが初期位置から移動できる最大距離です。
  • _targetPosition: カーソルが移動する目標位置です。

Awake メソッド​

Awake メソッド内で Ready イベントを登録して、Inverse3 デバイスのワークスペースの中心を基準に、カーソルの目標位置を初期化します:

private void Awake()
{
inverse3.Ready.AddListener((inverse3Controller, args) =>
{
_targetPosition = inverse3Controller.WorkspaceCenterLocalPosition;
});
}

デバイスの準備が整うまでは、手の向きがまだ決定されていないため、作業領域の中心を使用することはできません。

VerseGripの回転およびボタン操作に基づいて、カーソルの目標位置を計算するように OnDeviceStateChanged メソッドを実装します:

private void OnDeviceStateChanged(object sender, VerseGripEventArgs args)
{
var verseGrip = args.DeviceController;
// Calculate the direction based on the VerseGrip's rotation
var direction = verseGrip.Orientation * Vector3.forward;

// Check if the VerseGrip button is pressed down
if (verseGrip.GetButtonDown())
{
// Initialize target position
_targetPosition = inverse3.CursorLocalPosition;
}

// Check if the VerseGrip button is being held down
if (verseGrip.GetButton())
{
// Move the target position toward the grip direction
_targetPosition += direction * (0.0001f * speed);

// Clamp the target position within the movement limit radius
var workspaceCenter = inverse3.WorkspaceCenterLocalPosition;
_targetPosition = Vector3.ClampMagnitude(_targetPosition - workspaceCenter, movementLimitRadius)
+ workspaceCenter;
}
// Move cursor to new position
inverse3.SetCursorLocalPosition(_targetPosition);
}

OnEnableと OnDisableから DeviceStateChanged イベントを登録および解除します。

/// Subscribes to the DeviceStateChanged event.
private void OnEnable()
{
verseGrip.DeviceStateChanged += OnDeviceStateChanged;
}

/// Unsubscribes from the DeviceStateChanged event.
private void OnDisable()
{
verseGrip.DeviceStateChanged -= OnDeviceStateChanged;
}

ゲームプレイ​

  • Inverse3 装置を固定し、動かすのに十分なスペースを確保します。
  • プレイモードに入ったら、Inverse3 のカーソルを選択します。
  • VerseGripを回転させて、Unityのシーン内でカーソルの動きを確認します。この動きは、VerseGripの向きと直接対応しています。
  • VerseGripのボタンを押すと、Inverse3 のカーソルがVerseGripの回転方向に沿って動き、リアルタイムでの制御状況が表示されます。

versegripがボールを動かす様子

この画像は、より見やすくするために前方の軸を強調表示したカーソルモデルを示しています。 カーソルモデルのカスタマイズに関する詳細については、カーソルのドキュメントを参照してください。

ソースファイル​

このサンプルで使用している最終的なシーンおよび関連ファイルは、すべてUnityのパッケージマネージャーにある「Tutorials」サンプルから入手できます。

VerseGripPositionControl.cs​

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

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

namespace Haply.Samples.Tutorials._6_VerseGripPositionControl
{
/// <summary>
/// Demonstrates how to control the device cursor position using the VerseGrip.
/// </summary>
public class VerseGripPositionControl : MonoBehaviour
{
public Inverse3Controller inverse3;
public VerseGripController verseGrip;

[Tooltip("Cursor moving speed")]
[Range(0, 1)]
public float speed = 0.5f;

[Tooltip("Maximum radius for cursor movement")]
[Range(0, 0.1f)]
public float movementLimitRadius = 0.075f;

private Vector3 _targetPosition; // Target position for the cursor

private void Awake()
{
inverse3.Ready.AddListener((inverse3Controller, args) =>
{
_targetPosition = inverse3Controller.WorkspaceCenterLocalPosition;
});
}

/// <summary>
/// Subscribes to the DeviceStateChanged event.
/// </summary>
private void OnEnable()
{
verseGrip.DeviceStateChanged += OnDeviceStateChanged;
}

/// <summary>
/// Unsubscribes from the DeviceStateChanged event.
/// </summary>
private void OnDisable()
{
verseGrip.DeviceStateChanged -= OnDeviceStateChanged;
inverse3.Release();
}

private void OnDeviceStateChanged(object sender, VerseGripEventArgs args)
{
var verseGrip = args.DeviceController;
// Calculate the direction based on the VerseGrip's rotation
var direction = verseGrip.Orientation * Vector3.forward;

// Check if the VerseGrip button is pressed down
if (verseGrip.GetButtonDown())
{
// Initialize target position
_targetPosition = inverse3.CursorLocalPosition;
}

// Check if the VerseGrip button is being held down
if (verseGrip.GetButton())
{
// Move the target position toward the grip direction
_targetPosition += direction * (0.0001f * speed);

// Clamp the target position within the movement limit radius
var workspaceCenter = inverse3.WorkspaceCenterLocalPosition;
_targetPosition = Vector3.ClampMagnitude(_targetPosition - workspaceCenter, movementLimitRadius)
+ workspaceCenter;
}
// Move cursor to new position
inverse3.SetCursorLocalPosition(_targetPosition);
}
}
}