基本力与位置教程
本指南提供了一个简单的示例,演示如何对Inverse3 中的光标施加力并可视化其运动轨迹。 最后,Inverse3 设备模拟出一种仿佛被虚拟橡皮筋绑在起始位置的感觉,同时一个球形 GameObject 用于显示光标的位置。
简介
在入门指南中, Inverse3 介绍了对象、其功能以及生成恒定力的方法。 本节的目标是模拟光标上的橡皮筋效果。由于橡皮筋的工作原理与弹簧相似,因此其力既受刚度影响,也受两端点之间距离的影响。 因此,我们的目标是设计一个函数,该函数以位置和刚度作为输入参数,从而生成一种能够抵抗光标远离原点运动的力。
场景设置
首先通过“GameObjects >Haply”菜单创建 **Haptic Rig (one hand)**。
ForceAndPosition 组件
触觉起源 选择 GameObject,然后 ForceAndPosition.cs添加名为 的新脚本后,使用以下代码 ForceAndPosition 编写类:
[SerializeField]
private Inverse3 inverse3 = null;
[SerializeField, Range(0, 400)]
private float stiffness = 100;
private void OnDeviceStateChanged(object sender, Inverse3EventArgs args)
{
var inverse3 = args.DeviceController;
// Calculate the force.
var force = (inverse3.WorkspaceCenterLocalPosition - inverse3.CursorLocalPosition) * stiffness;
// Apply the force to the cursor.
inverse3.SetCursorLocalForce(force);
}
该代码片段将刚度设置为 100 牛顿/米(N/m),以模拟一个相对较软的弹簧。
此外,还有一个属性用于返回工作空间的中心,即 inverse3.WorkspaceCenterLocalPosition引入该方法。
该方法先从工作空间原点减去光标位置,再将结果乘以刚度来计算力。
最后 inverse3.SetCursorLocalForce使用 向光标施加力。
入门指南正如其中详细所述, OnEnable 以及 OnDisable 在方法中 OnDeviceStateChanged 请包含回调函数:
protected void OnEnable()
{
inverse3.DeviceStateChanged += OnDeviceStateChanged;
}
protected void OnDisable()
{
inverse3.DeviceStateChanged -= OnDeviceStateChanged;
}
游戏玩法
Inverse3 请在选中光标的状态下启用“播放模式”,然后尝试移动设备。移动光标时,您会发现会产生一种力。 光标离起始位置越远,这种力就越明显。

源文件
本示例中使用的最终场景及所有相关文件,均可通过 Unity 的包管理器中的“Tutorials”示例获取。
ForceAndPosition.cs
/*
* Copyright 2024 Haply Robotics Inc. All rights reserved.
*/
using Haply.Inverse.DeviceControllers;
using Haply.Inverse.DeviceData;
using UnityEngine;
namespace Haply.Samples.Tutorials._1_ForceAndPosition
{
/// <summary>
/// Demonstrates the application of force to maintain the cursor at its center position.
/// </summary>
public class ForceAndPosition : MonoBehaviour
{
public Inverse3Controller inverse3;
[Range(0, 400)]
// Stiffness of the force feedback.
public float stiffness = 100;
private void Awake()
{
inverse3 ??= FindObjectOfType<Inverse3Controller>();
}
/// <summary>
/// Subscribes to the DeviceStateChanged event when the component is enabled.
/// </summary>
protected void OnEnable()
{
inverse3.DeviceStateChanged += OnDeviceStateChanged;
}
/// <summary>
/// Unsubscribes from the DeviceStateChanged event and reset the force when the component is disabled.
/// </summary>
protected void OnDisable()
{
inverse3.DeviceStateChanged -= OnDeviceStateChanged;
inverse3.Release();
}
/// <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 force.
var force = (inverse3.WorkspaceCenterLocalPosition - inverse3.CursorLocalPosition) * stiffness;
// Apply the force to the cursor.
inverse3.SetCursorLocalForce(force);
}
}
}