力反馈入门教程
本教程将引导您创建一个简单的触觉模拟,该模拟同时考虑了刚度和阻尼,并模拟了与静态物体(例如一个球体)接触时的物理特性。 完成本教程后,您将获得一个模拟,通过它您可以感知球体的存在,并调整其刚度和阻尼特性以获得不同的触觉体验。
引言
本教程的核心挑战在于开发一个函数,用于计算与同时 具有刚度和阻尼特性的球体接触时产生的力。 在此背景下,刚度的作用类似于弹簧:压缩得越强,产生的力就越大。 而阻尼则描述了物体抵抗运动的能力:运动速度越快,这种阻力就越大。
场景构图
请先按照 《入门指南》 如上所述,并确保该对象的位置、旋转和缩放 触觉起源 auf简体中文(大陆) (0, 0, 0) 或 (1, 1, 1) 已设定。
随后创建一个名为 Sphere 具有以下特点:
- 职位:
(0, 0, -0.1)(距离设备约10厘米) - 缩放:
(0.2, 0.2, 0.2)(相当于一个直径为20厘米的球体)
力反馈脚本
将该 GameObject 添加到 球体 一个名为 SphereForceFeedback.cs hinzu。
该脚本用于计算球体与Inverse3 光标接触时作用于光标上的力,并考虑了刚度和阻尼。
请为该脚本设置以下属性:
[SerializeField]
private Inverse3 inverse3;
[Range(0, 800)]
public float stiffness = 300f;
[Range(0, 3)]
public float damping = 1f;
private Vector3 _ballPosition;
private float _ballRadius;
private float _cursorRadius;
只有当光标穿透球体时,才应进行力计算,从而模拟出触摸具有既定刚度和阻尼的物理物体的感觉。
该方法 ForceCalculation 负责此项操作,并同时考虑光标的位置和速度:
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;
}
private void OnDeviceStateChanged(object sender, Inverse3EventArgs args)
{
var inverse3 = args.DeviceController;
// Calculate the ball force
var force = ForceCalculation(inverse3.CursorLocalPosition, inverse3.CursorLocalVelocity,
_cursorRadius, _ballPosition, _ballRadius);
inverse3.SetCursorLocalForce(force);
}
请在该方法中初始化 Awake _ballPosition, _ballRadius und _cursorRadius, 以配置场景数据:
private void SaveSceneData()
{
var t = transform;
_ballPosition = t.position;
_ballRadius = t.lossyScale.x / 2f;
_cursorRadius = inverse3.Cursor.Radius;
}
private void Awake()
{
SaveSceneData();
}
请确保回调函数 OnDeviceStateChanged 在 OnEnable 已注册并位于 OnDisable 需要再次注销,以便在交互过程中正确处理力反馈:
protected void OnEnable()
{
inverse3.DeviceStateChanged += OnDeviceStateChanged;
}
protected void OnDisable()
{
inverse3.DeviceStateChanged -= OnDeviceStateChanged;
}
游戏体验
在按住Inverse3 光标的同时,启动“播放模式”,并尝试触碰该球体。 您应该能够感知到球体的存在,并可通过Unity检查器调整其刚度和阻尼特性。这样便能产生一种与虚拟物体进行交互的切实感受。

源文件
该示例的完整场景及所有相关文件均可从 Unity 包管理器的“示例教程”中导入。
SphereForceFeedback.cs
/*
* Copyright 2024 Haply Robotics Inc. All rights reserved.
*/
using Haply.Inverse.DeviceControllers;
using Haply.Inverse.DeviceData;
using UnityEngine;
namespace Haply.Samples.Tutorials._2_BasicForceFeedback
{
public class SphereForceFeedback : MonoBehaviour
{
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 ??= FindObjectOfType<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
var force = ForceCalculation(inverse3.CursorLocalPosition, inverse3.CursorLocalVelocity,
_cursorRadius, _ballPosition, _ballRadius);
inverse3.SetCursorLocalForce(force);
}
}
}