02 力反馈 OpenHaptics ConsoleExamples-FrictionlessSphere

本文目的,分析如何产生一个球面上的力(方向垂直于球面)

继续分析官方demo
在 FrictionlessSphere demo 中,创建了一个球面,由于球面各店的法向量均不相同,相对于上一个demo中平面的力计算要稍微复杂一点点。

力的方向

在这里插入图片描述

  • 首先判断装置位置与球心之间的距离,当距离小于半径时代表穿透
  • 穿透时计算球心指向装置位置的向量vec,单位化
  • 接着根据穿透的程度以及平面硬度(弹簧系数),由胡克定律得到最终的力

demo源码如下

其中 main 函数没有变化,只是回调函数的逻辑发生更改

HDCallbackCode HDCALLBACK FrictionlessSphereCallback(void *data)
{
   const double sphereRadius = 40.0;
   const hduVector3Dd spherePosition(0,0,0);
   // 硬度
   const double sphereStiffness = .25;

   hdBeginFrame(hdGetCurrentDevice());
  
   // device 位置.
   hduVector3Dd position;
   hdGetDoublev(HD_CURRENT_POSITION, position);
   
   // device 位置和球心距离
   double distance = (position-spherePosition).magnitude();

   // 当刺破球面时
   if (distance < sphereRadius)
   {
       // 计算穿刺距离
       double penetrationDistance = sphereRadius-distance;

       // 球心到位置方向向量
       hduVector3Dd forceDirection = (position-spherePosition)/distance;

       // 计算力
       double k = sphereStiffness;
       hduVector3Dd x = penetrationDistance*forceDirection;
       hduVector3Dd f = k*x;
       hdSetDoublev(HD_CURRENT_FORCE, f);
   }

   hdEndFrame(hdGetCurrentDevice());

   HDErrorInfo error;
   if (HD_DEVICE_ERROR(error = hdGetError()))
   {
       hduPrintError(stderr, &error, "Error during main scheduler callback\n");
       if (hduIsSchedulerError(&error))
       {
           return HD_CALLBACK_DONE;
       }        
   }
   return HD_CALLBACK_CONTINUE;
}
  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值