跳到主要内容

设备工作空间变换教程

本教程在 基础力反馈 教程的基础上进行扩展,演示如何使用空间变换属性和方法来调整 Inverse3 设备的位置、旋转和缩放。

简介​

在为基础力反馈搭建场景之后,您可能会注意到,对 Haptic Origin 或 Haptic Controller 进行移动、缩放或旋转等调整时,触觉反馈并不会如预期般变化。 反馈仍然对应于位于设备前方的一个不可见球体,不受这些变换的影响。

工作空间变换失效

出现这种偏差的原因在于,力计算使用的是设备光标真实的、未经变换的坐标。 为解决此问题,我们将利用 Inverse3 Controller 组件提供的 线程安全的缓存变换矩阵,从而将世界空间变换应用于触觉反馈计算中。

场景设置​

从 基础力反馈教程 中的场景开始,并确保 Inverse3 Controller 的左右手设置与您的设备相匹配。 旋转 Haptic Controller,使 Haply 徽标朝向摄像机,并根据需要调整 Haptic Origin 的缩放,以扩大光标范围或调整其位置。

Inverse3 右手设置

右手工作空间变换

力反馈脚本修改​

从基础力反馈教程中复制 SphereForceFeedback.cs 脚本,并在 OnDeviceStateChanged 回调中进行以下调整:

  • 将 device.CursorLocalPosition 替换为 device.CursorPosition。
  • 将 device.CursorLocalVelocity 替换为 device.CursorVelocity。
  • 将 device.CursorSetLocalForce(force) 替换为 device.CursorSetForce(force)。
private void OnDeviceStateChanged(object sender, Inverse3EventArgs args)
{
var inverse3 = args.DeviceController;
var force = ForceCalculation(inverse3.CursorPosition, inverse3.CursorVelocity,
_cursorRadius, _ballPosition, _ballRadius);

inverse3.SetCursorForce(force);
}

游戏体验​

握住 Inverse3 光标并进入播放模式。与前一个示例相同,尝试触碰球体。 现在您应该能够体验到准确的触觉反馈,它反映了对 Haptic Origin 和 Haptic Controller 所施加的变换。

放大后的光标接触球体

源文件​

本示例的完整场景及相关文件,可以从 Unity 包管理器中的 Tutorials 示例导入。

示例场景包含用于在运行时调整 Haptic Controller 和 Haptic Origin 的额外脚本。

SphereForceFeedback.cs​

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

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

namespace Haply.Samples.Tutorials._3_DeviceSpaceTransform
{
public class SphereForceFeedback : MonoBehaviour
{
// must assign in inspector
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 ??= FindFirstObjectByType<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. Using 'device.Position' instead of 'device.LocalPosition'
// ensures the force calculation considers the device's offset and rotation in world space.
var force = ForceCalculation(inverse3.CursorPosition, inverse3.CursorVelocity,
_cursorRadius, _ballPosition, _ballRadius);

// Apply the calculated force to the cursor. Using 'device.CursorSetForce' instead of
// 'device.CursorSetLocalForce' ensures that the force vector is correctly converted
// from world space to the device's local space.
inverse3.SetCursorForce(force);
}
}
}