跳转至主要内容

设备工作区转换教程

本教程是对 基础力反馈教程 通过演示如何调整设备的位移、旋转和缩放 Inverse3 借助其空间变换的属性和方法。

导言​

在配置了基础力反馈场景后,您可能会发现对“力反馈原点” 或“力反馈控制器”所做的调整(例如平移、缩放或旋转)并未如预期般影响力反馈效果。 力反馈始终对应于设备前方的一个不可见球体,而该球体不受这些变换的影响。

工作区转换失败

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

场景设置​

请从“基础力反馈”教程场景开始,并确保Inverse3 Controller的左右方向与您的设备一致。 将Haptic Controller旋转至Haply 标志朝向摄像头的方向,然后根据需要调整Haptic Origin的缩放比例,以扩大光标范围或更改其位置。

inverse3-右手设置

工作区--右侧变换

反作用力脚本的修改​

复制该脚本 SphereForceFeedback.cs 参考基础力反馈教程,并在回调函数中进行以下修改 OnDeviceStateChanged :

  • 替换 device.CursorLocalPosition par device.CursorPosition.
  • 替换 device.CursorLocalVelocity par device.CursorVelocity.
  • 替换 device.CursorSetLocalForce(force) par 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 ”上,并切换至“Play Mode”。试着像前一个示例中那样触摸该球体。 此时,您应该能感受到精确的触觉反馈,这反映了应用于“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);
}
}
}