设备工作区转换教程
本教程是 基本力反馈 在教程的基础上,利用空间转换的属性与方法来 Inverse3 演示了如何调整对象的位置、旋转和缩放。
简介
在设置好用于基本力反馈的场景后,您会发现,即使移动、缩放或旋转“Haptic Origin”或“Haptic Controller”,触觉反馈也不会按预期响应。 反馈仍然对应于位于设备前方的不可见球体,因此不受这些变换的影响。

这种不一致是由于力计算使用了光标实际的未变换坐标所导致的。 为了解决这个问题,我们将利用Inverse3 控制器组件提供的线程安全缓存变换矩阵,将世界空间变换应用于触觉反馈的计算中。
场景设置
从“基本力反馈教程”的场景开始,请确认“Inverse3 ”控制器的左右方向(handedness)是否与设备一致。 旋转“Haptic Controller”,使“Haply ”徽标朝向摄像头;若需扩大光标范围或调整位置,请按需调整“Haptic Origin”的缩放比例。


力反馈脚本的修改
《基础力反馈教程》的 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 请在选中光标的状态下进入“播放模式”。请像前面的示例一样触摸该球体。 现在,您可以体验到反映了“触觉原点 ”和“触觉控制器”所应用的变换的精确触觉反馈。

源文件
本示例的完整场景及相关文件可从 Unity 包管理器的“Tutorials”示例中获取。
示例场景中包含用于在运行时调整“触觉控制器”和“触觉原点”的附加脚本。
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);
}
}
}